xref: /freebsd/sys/dev/dc/if_dc.c (revision d7e9ac7523dafb6f7f9df5328dfcb1b2850f9e3a)
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 *);
255*d7e9ac75SPyun YongHyeon static int dc_ifmedia_upd_locked(struct dc_softc *);
256e51a25f8SAlfred Perlstein static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
25796f2e892SBill Paul 
2585f14ee23SPyun YongHyeon static int dc_dma_alloc(struct dc_softc *);
2595f14ee23SPyun YongHyeon static void dc_dma_free(struct dc_softc *);
2605f14ee23SPyun YongHyeon static void dc_dma_map_addr(void *, bus_dma_segment_t *, int, int);
2615f14ee23SPyun YongHyeon 
262e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
263e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
264e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
265ee320f98SPyun YongHyeon static void dc_eeprom_getword(struct dc_softc *, int, uint16_t *);
266ee320f98SPyun YongHyeon static void dc_eeprom_getword_pnic(struct dc_softc *, int, uint16_t *);
267ee320f98SPyun YongHyeon static void dc_eeprom_getword_xircom(struct dc_softc *, int, uint16_t *);
2683097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
269e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
27096f2e892SBill Paul 
271e51a25f8SAlfred Perlstein static void dc_mii_writebit(struct dc_softc *, int);
272e51a25f8SAlfred Perlstein static int dc_mii_readbit(struct dc_softc *);
273e51a25f8SAlfred Perlstein static void dc_mii_sync(struct dc_softc *);
274ee320f98SPyun YongHyeon static void dc_mii_send(struct dc_softc *, uint32_t, int);
275e51a25f8SAlfred Perlstein static int dc_mii_readreg(struct dc_softc *, struct dc_mii_frame *);
276e51a25f8SAlfred Perlstein static int dc_mii_writereg(struct dc_softc *, struct dc_mii_frame *);
277e51a25f8SAlfred Perlstein static int dc_miibus_readreg(device_t, int, int);
278e51a25f8SAlfred Perlstein static int dc_miibus_writereg(device_t, int, int, int);
279e51a25f8SAlfred Perlstein static void dc_miibus_statchg(device_t);
280e51a25f8SAlfred Perlstein static void dc_miibus_mediainit(device_t);
28196f2e892SBill Paul 
282e51a25f8SAlfred Perlstein static void dc_setcfg(struct dc_softc *, int);
2831da7683aSPyun YongHyeon static void dc_netcfg_wait(struct dc_softc *);
2843373489bSWarner Losh static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
2853373489bSWarner Losh static uint32_t dc_mchash_be(const uint8_t *);
286e51a25f8SAlfred Perlstein static void dc_setfilt_21143(struct dc_softc *);
287e51a25f8SAlfred Perlstein static void dc_setfilt_asix(struct dc_softc *);
288e51a25f8SAlfred Perlstein static void dc_setfilt_admtek(struct dc_softc *);
289e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
29096f2e892SBill Paul 
291e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
29296f2e892SBill Paul 
293e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
294e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
295e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
29696f2e892SBill Paul 
297abe4e865SPyun YongHyeon static int dc_read_srom(struct dc_softc *, int);
298abe4e865SPyun YongHyeon static int dc_parse_21143_srom(struct dc_softc *);
299abe4e865SPyun YongHyeon static int dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
300abe4e865SPyun YongHyeon static int dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
301abe4e865SPyun YongHyeon static int dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
302e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
30339d76ed6SPyun YongHyeon static int dc_check_multiport(struct dc_softc *);
3045c1cfac4SBill Paul 
30596f2e892SBill Paul #ifdef DC_USEIOSPACE
30696f2e892SBill Paul #define	DC_RES			SYS_RES_IOPORT
30796f2e892SBill Paul #define	DC_RID			DC_PCI_CFBIO
30896f2e892SBill Paul #else
30996f2e892SBill Paul #define	DC_RES			SYS_RES_MEMORY
31096f2e892SBill Paul #define	DC_RID			DC_PCI_CFBMA
31196f2e892SBill Paul #endif
31296f2e892SBill Paul 
31396f2e892SBill Paul static device_method_t dc_methods[] = {
31496f2e892SBill Paul 	/* Device interface */
31596f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
31696f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
31796f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
318e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
319e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
32096f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
32196f2e892SBill Paul 
32296f2e892SBill Paul 	/* bus interface */
32396f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
32496f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
32596f2e892SBill Paul 
32696f2e892SBill Paul 	/* MII interface */
32796f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
32896f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
32996f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
330f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
33196f2e892SBill Paul 
33296f2e892SBill Paul 	{ 0, 0 }
33396f2e892SBill Paul };
33496f2e892SBill Paul 
33596f2e892SBill Paul static driver_t dc_driver = {
33696f2e892SBill Paul 	"dc",
33796f2e892SBill Paul 	dc_methods,
33896f2e892SBill Paul 	sizeof(struct dc_softc)
33996f2e892SBill Paul };
34096f2e892SBill Paul 
34196f2e892SBill Paul static devclass_t dc_devclass;
34296f2e892SBill Paul 
343f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
34496f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
34596f2e892SBill Paul 
34696f2e892SBill Paul #define	DC_SETBIT(sc, reg, x)				\
34796f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
34896f2e892SBill Paul 
34996f2e892SBill Paul #define	DC_CLRBIT(sc, reg, x)				\
35096f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
35196f2e892SBill Paul 
35296f2e892SBill Paul #define	SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
35396f2e892SBill Paul #define	SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
35496f2e892SBill Paul 
355e3d2833aSAlfred Perlstein static void
3560934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
35796f2e892SBill Paul {
35896f2e892SBill Paul 	int idx;
35996f2e892SBill Paul 
36096f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
36196f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
36296f2e892SBill Paul }
36396f2e892SBill Paul 
3642c876e15SPoul-Henning Kamp static void
3650934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3663097aa70SWarner Losh {
3673097aa70SWarner Losh 	int i;
3683097aa70SWarner Losh 
3693097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3703097aa70SWarner Losh 	dc_eeprom_idle(sc);
3713097aa70SWarner Losh 
3723097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3733097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3743097aa70SWarner Losh 	dc_delay(sc);
3753097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3763097aa70SWarner Losh 	dc_delay(sc);
3773097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3783097aa70SWarner Losh 	dc_delay(sc);
3793097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3803097aa70SWarner Losh 	dc_delay(sc);
3813097aa70SWarner Losh 
3823097aa70SWarner Losh 	for (i = 3; i--;) {
3833097aa70SWarner Losh 		if (6 & (1 << i))
3843097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3853097aa70SWarner Losh 		else
3863097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3873097aa70SWarner Losh 		dc_delay(sc);
3883097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3893097aa70SWarner Losh 		dc_delay(sc);
3903097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3913097aa70SWarner Losh 		dc_delay(sc);
3923097aa70SWarner Losh 	}
3933097aa70SWarner Losh 
3943097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
3953097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3963097aa70SWarner Losh 		dc_delay(sc);
3973097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
3983097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3993097aa70SWarner Losh 			dc_delay(sc);
4003097aa70SWarner Losh 			break;
4013097aa70SWarner Losh 		}
4023097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4033097aa70SWarner Losh 		dc_delay(sc);
4043097aa70SWarner Losh 	}
4053097aa70SWarner Losh 
4063097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4073097aa70SWarner Losh 	dc_eeprom_idle(sc);
4083097aa70SWarner Losh 
4093097aa70SWarner Losh 	if (i < 4 || i > 12)
4103097aa70SWarner Losh 		sc->dc_romwidth = 6;
4113097aa70SWarner Losh 	else
4123097aa70SWarner Losh 		sc->dc_romwidth = i;
4133097aa70SWarner Losh 
4143097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4153097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4163097aa70SWarner Losh 	dc_delay(sc);
4173097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4183097aa70SWarner Losh 	dc_delay(sc);
4193097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4203097aa70SWarner Losh 	dc_delay(sc);
4213097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4223097aa70SWarner Losh 	dc_delay(sc);
4233097aa70SWarner Losh 
4243097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4253097aa70SWarner Losh 	dc_eeprom_idle(sc);
4263097aa70SWarner Losh }
4273097aa70SWarner Losh 
428e3d2833aSAlfred Perlstein static void
4290934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
43096f2e892SBill Paul {
4310934f18aSMaxime Henrion 	int i;
43296f2e892SBill Paul 
43396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
43496f2e892SBill Paul 	dc_delay(sc);
43596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
43696f2e892SBill Paul 	dc_delay(sc);
43796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43896f2e892SBill Paul 	dc_delay(sc);
43996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
44096f2e892SBill Paul 	dc_delay(sc);
44196f2e892SBill Paul 
44296f2e892SBill Paul 	for (i = 0; i < 25; i++) {
44396f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44496f2e892SBill Paul 		dc_delay(sc);
44596f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44696f2e892SBill Paul 		dc_delay(sc);
44796f2e892SBill Paul 	}
44896f2e892SBill Paul 
44996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45096f2e892SBill Paul 	dc_delay(sc);
45196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
45296f2e892SBill Paul 	dc_delay(sc);
45396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
45496f2e892SBill Paul }
45596f2e892SBill Paul 
45696f2e892SBill Paul /*
45796f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
45896f2e892SBill Paul  */
459e3d2833aSAlfred Perlstein static void
4600934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
46196f2e892SBill Paul {
4620934f18aSMaxime Henrion 	int d, i;
46396f2e892SBill Paul 
4643097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4653097aa70SWarner Losh 	for (i = 3; i--; ) {
4663097aa70SWarner Losh 		if (d & (1 << i))
4673097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
46896f2e892SBill Paul 		else
4693097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4703097aa70SWarner Losh 		dc_delay(sc);
4713097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4723097aa70SWarner Losh 		dc_delay(sc);
4733097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4743097aa70SWarner Losh 		dc_delay(sc);
4753097aa70SWarner Losh 	}
47696f2e892SBill Paul 
47796f2e892SBill Paul 	/*
47896f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
47996f2e892SBill Paul 	 */
4803097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4813097aa70SWarner Losh 		if (addr & (1 << i)) {
48296f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
48396f2e892SBill Paul 		} else {
48496f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
48596f2e892SBill Paul 		}
48696f2e892SBill Paul 		dc_delay(sc);
48796f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
48896f2e892SBill Paul 		dc_delay(sc);
48996f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
49096f2e892SBill Paul 		dc_delay(sc);
49196f2e892SBill Paul 	}
49296f2e892SBill Paul }
49396f2e892SBill Paul 
49496f2e892SBill Paul /*
49596f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
49696f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
49796f2e892SBill Paul  * the EEPROM.
49896f2e892SBill Paul  */
499e3d2833aSAlfred Perlstein static void
500ee320f98SPyun YongHyeon dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, uint16_t *dest)
50196f2e892SBill Paul {
5020934f18aSMaxime Henrion 	int i;
503ee320f98SPyun YongHyeon 	uint32_t r;
50496f2e892SBill Paul 
50596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
50696f2e892SBill Paul 
50796f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
50896f2e892SBill Paul 		DELAY(1);
50996f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
51096f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
511ee320f98SPyun YongHyeon 			*dest = (uint16_t)(r & 0xFFFF);
51296f2e892SBill Paul 			return;
51396f2e892SBill Paul 		}
51496f2e892SBill Paul 	}
51596f2e892SBill Paul }
51696f2e892SBill Paul 
51796f2e892SBill Paul /*
51896f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
519feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
520feb78939SJonathan Chen  * the EEPROM, too.
521feb78939SJonathan Chen  */
522e3d2833aSAlfred Perlstein static void
523ee320f98SPyun YongHyeon dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, uint16_t *dest)
524feb78939SJonathan Chen {
5250934f18aSMaxime Henrion 
526feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
527feb78939SJonathan Chen 
528feb78939SJonathan Chen 	addr *= 2;
529feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
530ee320f98SPyun YongHyeon 	*dest = (uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
531feb78939SJonathan Chen 	addr += 1;
532feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
533ee320f98SPyun YongHyeon 	*dest |= ((uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
534feb78939SJonathan Chen 
535feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
536feb78939SJonathan Chen }
537feb78939SJonathan Chen 
538feb78939SJonathan Chen /*
539feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
54096f2e892SBill Paul  */
541e3d2833aSAlfred Perlstein static void
542ee320f98SPyun YongHyeon dc_eeprom_getword(struct dc_softc *sc, int addr, uint16_t *dest)
54396f2e892SBill Paul {
5440934f18aSMaxime Henrion 	int i;
545ee320f98SPyun YongHyeon 	uint16_t word = 0;
54696f2e892SBill Paul 
54796f2e892SBill Paul 	/* Force EEPROM to idle state. */
54896f2e892SBill Paul 	dc_eeprom_idle(sc);
54996f2e892SBill Paul 
55096f2e892SBill Paul 	/* Enter EEPROM access mode. */
55196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
55296f2e892SBill Paul 	dc_delay(sc);
55396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
55496f2e892SBill Paul 	dc_delay(sc);
55596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
55696f2e892SBill Paul 	dc_delay(sc);
55796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
55896f2e892SBill Paul 	dc_delay(sc);
55996f2e892SBill Paul 
56096f2e892SBill Paul 	/*
56196f2e892SBill Paul 	 * Send address of word we want to read.
56296f2e892SBill Paul 	 */
56396f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
56496f2e892SBill Paul 
56596f2e892SBill Paul 	/*
56696f2e892SBill Paul 	 * Start reading bits from EEPROM.
56796f2e892SBill Paul 	 */
56896f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
56996f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
57096f2e892SBill Paul 		dc_delay(sc);
57196f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
57296f2e892SBill Paul 			word |= i;
57396f2e892SBill Paul 		dc_delay(sc);
57496f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
57596f2e892SBill Paul 		dc_delay(sc);
57696f2e892SBill Paul 	}
57796f2e892SBill Paul 
57896f2e892SBill Paul 	/* Turn off EEPROM access mode. */
57996f2e892SBill Paul 	dc_eeprom_idle(sc);
58096f2e892SBill Paul 
58196f2e892SBill Paul 	*dest = word;
58296f2e892SBill Paul }
58396f2e892SBill Paul 
58496f2e892SBill Paul /*
58596f2e892SBill Paul  * Read a sequence of words from the EEPROM.
58696f2e892SBill Paul  */
587e3d2833aSAlfred Perlstein static void
5888c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
58996f2e892SBill Paul {
59096f2e892SBill Paul 	int i;
591ee320f98SPyun YongHyeon 	uint16_t word = 0, *ptr;
59296f2e892SBill Paul 
59396f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
59496f2e892SBill Paul 		if (DC_IS_PNIC(sc))
59596f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
596feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
597feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
59896f2e892SBill Paul 		else
59996f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
600ee320f98SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
6018c7ff1f3SMaxime Henrion 		if (be)
6028c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
60396f2e892SBill Paul 		else
6048c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
60596f2e892SBill Paul 	}
60696f2e892SBill Paul }
60796f2e892SBill Paul 
60896f2e892SBill Paul /*
60996f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
61096f2e892SBill Paul  * Application Notes pp.19-21.
61196f2e892SBill Paul  */
61296f2e892SBill Paul /*
61396f2e892SBill Paul  * Write a bit to the MII bus.
61496f2e892SBill Paul  */
615e3d2833aSAlfred Perlstein static void
6160934f18aSMaxime Henrion dc_mii_writebit(struct dc_softc *sc, int bit)
61796f2e892SBill Paul {
61815578119SMarius Strobl 	uint32_t reg;
6190934f18aSMaxime Henrion 
62015578119SMarius Strobl 	reg = DC_SIO_ROMCTL_WRITE | (bit != 0 ? DC_SIO_MII_DATAOUT : 0);
62115578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg);
62215578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
62315578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
62415578119SMarius Strobl 	DELAY(1);
62596f2e892SBill Paul 
62615578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg | DC_SIO_MII_CLK);
62715578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
62815578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
62915578119SMarius Strobl 	DELAY(1);
63015578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg);
63115578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
63215578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
63315578119SMarius Strobl 	DELAY(1);
63496f2e892SBill Paul }
63596f2e892SBill Paul 
63696f2e892SBill Paul /*
63796f2e892SBill Paul  * Read a bit from the MII bus.
63896f2e892SBill Paul  */
639e3d2833aSAlfred Perlstein static int
6400934f18aSMaxime Henrion dc_mii_readbit(struct dc_softc *sc)
64196f2e892SBill Paul {
64215578119SMarius Strobl 	uint32_t reg;
6430934f18aSMaxime Henrion 
64415578119SMarius Strobl 	reg = DC_SIO_ROMCTL_READ | DC_SIO_MII_DIR;
64515578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg);
64615578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
64715578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
64815578119SMarius Strobl 	DELAY(1);
64915578119SMarius Strobl 	(void)CSR_READ_4(sc, DC_SIO);
65015578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg | DC_SIO_MII_CLK);
65115578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
65215578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
65315578119SMarius Strobl 	DELAY(1);
65415578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg);
65515578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
65615578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
65715578119SMarius Strobl 	DELAY(1);
65896f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
65996f2e892SBill Paul 		return (1);
66096f2e892SBill Paul 
66196f2e892SBill Paul 	return (0);
66296f2e892SBill Paul }
66396f2e892SBill Paul 
66496f2e892SBill Paul /*
66596f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
66696f2e892SBill Paul  */
667e3d2833aSAlfred Perlstein static void
6680934f18aSMaxime Henrion dc_mii_sync(struct dc_softc *sc)
66996f2e892SBill Paul {
6700934f18aSMaxime Henrion 	int i;
67196f2e892SBill Paul 
67296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
67315578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
67415578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
67515578119SMarius Strobl 	DELAY(1);
67696f2e892SBill Paul 
67796f2e892SBill Paul 	for (i = 0; i < 32; i++)
67896f2e892SBill Paul 		dc_mii_writebit(sc, 1);
67996f2e892SBill Paul }
68096f2e892SBill Paul 
68196f2e892SBill Paul /*
68296f2e892SBill Paul  * Clock a series of bits through the MII.
68396f2e892SBill Paul  */
684e3d2833aSAlfred Perlstein static void
685ee320f98SPyun YongHyeon dc_mii_send(struct dc_softc *sc, uint32_t bits, int cnt)
68696f2e892SBill Paul {
68796f2e892SBill Paul 	int i;
68896f2e892SBill Paul 
68996f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
69096f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
69196f2e892SBill Paul }
69296f2e892SBill Paul 
69396f2e892SBill Paul /*
69496f2e892SBill Paul  * Read an PHY register through the MII.
69596f2e892SBill Paul  */
696e3d2833aSAlfred Perlstein static int
6970934f18aSMaxime Henrion dc_mii_readreg(struct dc_softc *sc, struct dc_mii_frame *frame)
69896f2e892SBill Paul {
69915578119SMarius Strobl 	int i;
70096f2e892SBill Paul 
70196f2e892SBill Paul 	/*
70296f2e892SBill Paul 	 * Set up frame for RX.
70396f2e892SBill Paul 	 */
70496f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
70596f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
70696f2e892SBill Paul 
70796f2e892SBill Paul 	/*
70896f2e892SBill Paul 	 * Sync the PHYs.
70996f2e892SBill Paul 	 */
71096f2e892SBill Paul 	dc_mii_sync(sc);
71196f2e892SBill Paul 
71296f2e892SBill Paul 	/*
71396f2e892SBill Paul 	 * Send command/address info.
71496f2e892SBill Paul 	 */
71596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
71696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
71796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
71896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
71996f2e892SBill Paul 
72096f2e892SBill Paul 	/*
72115578119SMarius Strobl 	 * Now try reading data bits.  If the turnaround failed, we still
72296f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
72396f2e892SBill Paul 	 */
72415578119SMarius Strobl 	frame->mii_turnaround = dc_mii_readbit(sc);
72515578119SMarius Strobl 	if (frame->mii_turnaround != 0) {
7260934f18aSMaxime Henrion 		for (i = 0; i < 16; i++)
72796f2e892SBill Paul 			dc_mii_readbit(sc);
72896f2e892SBill Paul 		goto fail;
72996f2e892SBill Paul 	}
73096f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
73196f2e892SBill Paul 		if (dc_mii_readbit(sc))
73296f2e892SBill Paul 			frame->mii_data |= i;
73396f2e892SBill Paul 	}
73496f2e892SBill Paul 
73596f2e892SBill Paul fail:
73696f2e892SBill Paul 
73715578119SMarius Strobl 	/* Clock the idle bits. */
73896f2e892SBill Paul 	dc_mii_writebit(sc, 0);
73996f2e892SBill Paul 	dc_mii_writebit(sc, 0);
74096f2e892SBill Paul 
74115578119SMarius Strobl 	if (frame->mii_turnaround != 0)
74296f2e892SBill Paul 		return (1);
74396f2e892SBill Paul 	return (0);
74496f2e892SBill Paul }
74596f2e892SBill Paul 
74696f2e892SBill Paul /*
74796f2e892SBill Paul  * Write to a PHY register through the MII.
74896f2e892SBill Paul  */
749e3d2833aSAlfred Perlstein static int
7500934f18aSMaxime Henrion dc_mii_writereg(struct dc_softc *sc, struct dc_mii_frame *frame)
75196f2e892SBill Paul {
7520934f18aSMaxime Henrion 
75396f2e892SBill Paul 	/*
75496f2e892SBill Paul 	 * Set up frame for TX.
75596f2e892SBill Paul 	 */
75696f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
75796f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
75896f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
75996f2e892SBill Paul 
76096f2e892SBill Paul 	/*
76196f2e892SBill Paul 	 * Sync the PHYs.
76296f2e892SBill Paul 	 */
76396f2e892SBill Paul 	dc_mii_sync(sc);
76496f2e892SBill Paul 
76596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
76696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
76796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
76896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
76996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
77096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
77196f2e892SBill Paul 
77215578119SMarius Strobl 	/* Clock the idle bits. */
77396f2e892SBill Paul 	dc_mii_writebit(sc, 0);
77496f2e892SBill Paul 	dc_mii_writebit(sc, 0);
77596f2e892SBill Paul 
77696f2e892SBill Paul 	return (0);
77796f2e892SBill Paul }
77896f2e892SBill Paul 
779e3d2833aSAlfred Perlstein static int
7800934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
78196f2e892SBill Paul {
78296f2e892SBill Paul 	struct dc_mii_frame frame;
78396f2e892SBill Paul 	struct dc_softc	 *sc;
784c85c4667SBill Paul 	int i, rval, phy_reg = 0;
78596f2e892SBill Paul 
78696f2e892SBill Paul 	sc = device_get_softc(dev);
7870934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
78896f2e892SBill Paul 
7895c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
79096f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
79196f2e892SBill Paul 			switch (reg) {
79296f2e892SBill Paul 			case MII_BMSR:
79396f2e892SBill Paul 			/*
79496f2e892SBill Paul 			 * Fake something to make the probe
79596f2e892SBill Paul 			 * code think there's a PHY here.
79696f2e892SBill Paul 			 */
79796f2e892SBill Paul 				return (BMSR_MEDIAMASK);
79896f2e892SBill Paul 				break;
79996f2e892SBill Paul 			case MII_PHYIDR1:
80096f2e892SBill Paul 				if (DC_IS_PNIC(sc))
80196f2e892SBill Paul 					return (DC_VENDORID_LO);
80296f2e892SBill Paul 				return (DC_VENDORID_DEC);
80396f2e892SBill Paul 				break;
80496f2e892SBill Paul 			case MII_PHYIDR2:
80596f2e892SBill Paul 				if (DC_IS_PNIC(sc))
80696f2e892SBill Paul 					return (DC_DEVICEID_82C168);
80796f2e892SBill Paul 				return (DC_DEVICEID_21143);
80896f2e892SBill Paul 				break;
80996f2e892SBill Paul 			default:
81096f2e892SBill Paul 				return (0);
81196f2e892SBill Paul 				break;
81296f2e892SBill Paul 			}
81396f2e892SBill Paul 		} else
81496f2e892SBill Paul 			return (0);
81596f2e892SBill Paul 	}
81696f2e892SBill Paul 
81796f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
81896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
81996f2e892SBill Paul 		    (phy << 23) | (reg << 18));
82096f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
82196f2e892SBill Paul 			DELAY(1);
82296f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
82396f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
82496f2e892SBill Paul 				rval &= 0xFFFF;
82596f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
82696f2e892SBill Paul 			}
82796f2e892SBill Paul 		}
82896f2e892SBill Paul 		return (0);
82996f2e892SBill Paul 	}
83096f2e892SBill Paul 
83196f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
83296f2e892SBill Paul 		switch (reg) {
83396f2e892SBill Paul 		case MII_BMCR:
83496f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
83596f2e892SBill Paul 			break;
83696f2e892SBill Paul 		case MII_BMSR:
83796f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
83896f2e892SBill Paul 			break;
83996f2e892SBill Paul 		case MII_PHYIDR1:
84096f2e892SBill Paul 			phy_reg = DC_AL_VENID;
84196f2e892SBill Paul 			break;
84296f2e892SBill Paul 		case MII_PHYIDR2:
84396f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
84496f2e892SBill Paul 			break;
84596f2e892SBill Paul 		case MII_ANAR:
84696f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
84796f2e892SBill Paul 			break;
84896f2e892SBill Paul 		case MII_ANLPAR:
84996f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
85096f2e892SBill Paul 			break;
85196f2e892SBill Paul 		case MII_ANER:
85296f2e892SBill Paul 			phy_reg = DC_AL_ANER;
85396f2e892SBill Paul 			break;
85496f2e892SBill Paul 		default:
85522f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
85622f6205dSJohn Baldwin 			    reg);
85796f2e892SBill Paul 			return (0);
85896f2e892SBill Paul 			break;
85996f2e892SBill Paul 		}
86096f2e892SBill Paul 
86196f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
86296f2e892SBill Paul 
86396f2e892SBill Paul 		if (rval == 0xFFFF)
86496f2e892SBill Paul 			return (0);
86596f2e892SBill Paul 		return (rval);
86696f2e892SBill Paul 	}
86796f2e892SBill Paul 
86896f2e892SBill Paul 	frame.mii_phyaddr = phy;
86996f2e892SBill Paul 	frame.mii_regaddr = reg;
870419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
871f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
872f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
873419146d9SBill Paul 	}
87496f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
875419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
876f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
87796f2e892SBill Paul 
87896f2e892SBill Paul 	return (frame.mii_data);
87996f2e892SBill Paul }
88096f2e892SBill Paul 
881e3d2833aSAlfred Perlstein static int
8820934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
88396f2e892SBill Paul {
88496f2e892SBill Paul 	struct dc_softc *sc;
88596f2e892SBill Paul 	struct dc_mii_frame frame;
886c85c4667SBill Paul 	int i, phy_reg = 0;
88796f2e892SBill Paul 
88896f2e892SBill Paul 	sc = device_get_softc(dev);
8890934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
89096f2e892SBill Paul 
89196f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
89296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
89396f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
89496f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
89596f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
89696f2e892SBill Paul 				break;
89796f2e892SBill Paul 		}
89896f2e892SBill Paul 		return (0);
89996f2e892SBill Paul 	}
90096f2e892SBill Paul 
90196f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
90296f2e892SBill Paul 		switch (reg) {
90396f2e892SBill Paul 		case MII_BMCR:
90496f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
90596f2e892SBill Paul 			break;
90696f2e892SBill Paul 		case MII_BMSR:
90796f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
90896f2e892SBill Paul 			break;
90996f2e892SBill Paul 		case MII_PHYIDR1:
91096f2e892SBill Paul 			phy_reg = DC_AL_VENID;
91196f2e892SBill Paul 			break;
91296f2e892SBill Paul 		case MII_PHYIDR2:
91396f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
91496f2e892SBill Paul 			break;
91596f2e892SBill Paul 		case MII_ANAR:
91696f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
91796f2e892SBill Paul 			break;
91896f2e892SBill Paul 		case MII_ANLPAR:
91996f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
92096f2e892SBill Paul 			break;
92196f2e892SBill Paul 		case MII_ANER:
92296f2e892SBill Paul 			phy_reg = DC_AL_ANER;
92396f2e892SBill Paul 			break;
92496f2e892SBill Paul 		default:
92522f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
92622f6205dSJohn Baldwin 			    reg);
92796f2e892SBill Paul 			return (0);
92896f2e892SBill Paul 			break;
92996f2e892SBill Paul 		}
93096f2e892SBill Paul 
93196f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
93296f2e892SBill Paul 		return (0);
93396f2e892SBill Paul 	}
93496f2e892SBill Paul 
93596f2e892SBill Paul 	frame.mii_phyaddr = phy;
93696f2e892SBill Paul 	frame.mii_regaddr = reg;
93796f2e892SBill Paul 	frame.mii_data = data;
93896f2e892SBill Paul 
939419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
940f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
941f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
942419146d9SBill Paul 	}
94396f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
944419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
945f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
94696f2e892SBill Paul 
94796f2e892SBill Paul 	return (0);
94896f2e892SBill Paul }
94996f2e892SBill Paul 
950e3d2833aSAlfred Perlstein static void
9510934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
95296f2e892SBill Paul {
95396f2e892SBill Paul 	struct dc_softc *sc;
954d314ebf5SPyun YongHyeon 	struct ifnet *ifp;
95596f2e892SBill Paul 	struct mii_data *mii;
956f43d9309SBill Paul 	struct ifmedia *ifm;
95796f2e892SBill Paul 
95896f2e892SBill Paul 	sc = device_get_softc(dev);
9595c1cfac4SBill Paul 
96096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
961d314ebf5SPyun YongHyeon 	ifp = sc->dc_ifp;
962d314ebf5SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
963d314ebf5SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
964d314ebf5SPyun YongHyeon 		return;
965d314ebf5SPyun YongHyeon 
966f43d9309SBill Paul 	ifm = &mii->mii_media;
967f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
96845521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
969f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
970f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
971d314ebf5SPyun YongHyeon 		return;
972f43d9309SBill Paul 	}
973d314ebf5SPyun YongHyeon 
974d314ebf5SPyun YongHyeon 	sc->dc_link = 0;
975d314ebf5SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
976d314ebf5SPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
977d314ebf5SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
978d314ebf5SPyun YongHyeon 		case IFM_10_T:
979d314ebf5SPyun YongHyeon 		case IFM_100_TX:
980d314ebf5SPyun YongHyeon 			sc->dc_link = 1;
981d314ebf5SPyun YongHyeon 			break;
982d314ebf5SPyun YongHyeon 		default:
983d314ebf5SPyun YongHyeon 			break;
984d314ebf5SPyun YongHyeon 		}
985d314ebf5SPyun YongHyeon 	}
986d314ebf5SPyun YongHyeon 	if (sc->dc_link == 0)
987d314ebf5SPyun YongHyeon 		return;
988d314ebf5SPyun YongHyeon 
989d314ebf5SPyun YongHyeon 	sc->dc_if_media = mii->mii_media_active;
990d314ebf5SPyun YongHyeon 	if (DC_IS_ADMTEK(sc))
991d314ebf5SPyun YongHyeon 		return;
992d314ebf5SPyun YongHyeon 	dc_setcfg(sc, mii->mii_media_active);
993f43d9309SBill Paul }
994f43d9309SBill Paul 
995f43d9309SBill Paul /*
996f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
997f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
998f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
999f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
1000f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
1001f43d9309SBill Paul  * with it itself. *sigh*
1002f43d9309SBill Paul  */
1003e3d2833aSAlfred Perlstein static void
10040934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
1005f43d9309SBill Paul {
1006f43d9309SBill Paul 	struct dc_softc *sc;
1007f43d9309SBill Paul 	struct mii_data *mii;
1008f43d9309SBill Paul 	struct ifmedia *ifm;
1009f43d9309SBill Paul 	int rev;
1010f43d9309SBill Paul 
10111e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
1012f43d9309SBill Paul 
1013f43d9309SBill Paul 	sc = device_get_softc(dev);
1014f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
1015f43d9309SBill Paul 	ifm = &mii->mii_media;
1016f43d9309SBill Paul 
1017f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
101845521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
101996f2e892SBill Paul }
102096f2e892SBill Paul 
102179d11e09SBill Paul #define	DC_BITS_512	9
102279d11e09SBill Paul #define	DC_BITS_128	7
102379d11e09SBill Paul #define	DC_BITS_64	6
102496f2e892SBill Paul 
10253373489bSWarner Losh static uint32_t
10263373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
102796f2e892SBill Paul {
10283373489bSWarner Losh 	uint32_t crc;
102996f2e892SBill Paul 
103096f2e892SBill Paul 	/* Compute CRC for the address value. */
10310e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
103296f2e892SBill Paul 
103379d11e09SBill Paul 	/*
103479d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
103579d11e09SBill Paul 	 * chips is only 128 bits wide.
103679d11e09SBill Paul 	 */
103779d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
103879d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
103996f2e892SBill Paul 
104079d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
104179d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
104279d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
104379d11e09SBill Paul 
1044feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1045feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1046feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1047feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
10480934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
1049feb78939SJonathan Chen 		else
10500934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
10510934f18aSMaxime Henrion 			    (12 << 4));
1052feb78939SJonathan Chen 	}
1053feb78939SJonathan Chen 
105479d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
105596f2e892SBill Paul }
105696f2e892SBill Paul 
105796f2e892SBill Paul /*
105896f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
105996f2e892SBill Paul  */
10603373489bSWarner Losh static uint32_t
10613373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
106296f2e892SBill Paul {
10630e939c0cSChristian Weisgerber 	uint32_t crc;
106496f2e892SBill Paul 
106596f2e892SBill Paul 	/* Compute CRC for the address value. */
10660e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
106796f2e892SBill Paul 
10680934f18aSMaxime Henrion 	/* Return the filter bit position. */
106996f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
107096f2e892SBill Paul }
107196f2e892SBill Paul 
107296f2e892SBill Paul /*
107396f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
107496f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
107596f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
107696f2e892SBill Paul  *
107796f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
107896f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
107996f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
108096f2e892SBill Paul  * we need that too.
108196f2e892SBill Paul  */
10822c876e15SPoul-Henning Kamp static void
10830934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
108496f2e892SBill Paul {
10858df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
108696f2e892SBill Paul 	struct dc_desc *sframe;
1087ee320f98SPyun YongHyeon 	uint32_t h, *sp;
108896f2e892SBill Paul 	struct ifmultiaddr *ifma;
108996f2e892SBill Paul 	struct ifnet *ifp;
109096f2e892SBill Paul 	int i;
109196f2e892SBill Paul 
1092fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
109396f2e892SBill Paul 
109496f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
109596f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
109696f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
10975f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
109856e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
10990934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
110096f2e892SBill Paul 
11015f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1102af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1103af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
110496f2e892SBill Paul 
110556e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
110696f2e892SBill Paul 
110796f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
110896f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
110996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
111096f2e892SBill Paul 	else
111196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
111296f2e892SBill Paul 
111396f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
111496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111596f2e892SBill Paul 	else
111696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111796f2e892SBill Paul 
1118eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
11196817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
112096f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
112196f2e892SBill Paul 			continue;
1122aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
112396f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1124af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
112596f2e892SBill Paul 	}
1126eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
112796f2e892SBill Paul 
112896f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1129aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1130af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
113196f2e892SBill Paul 	}
113296f2e892SBill Paul 
11338df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
11348df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
11358df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
11368df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
11378df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
113896f2e892SBill Paul 
1139af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
11408c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
11418c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
11425f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
114396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
114496f2e892SBill Paul 
114596f2e892SBill Paul 	/*
114696f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
114796f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
114896f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
114996f2e892SBill Paul 	 * medicine.
115096f2e892SBill Paul 	 */
115196f2e892SBill Paul 	DELAY(10000);
115296f2e892SBill Paul 
1153b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
115496f2e892SBill Paul }
115596f2e892SBill Paul 
11562c876e15SPoul-Henning Kamp static void
11570934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
115896f2e892SBill Paul {
11592e3d4b79SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
116096f2e892SBill Paul 	struct ifnet *ifp;
11610934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
116296f2e892SBill Paul 	int h = 0;
1163ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
116496f2e892SBill Paul 
1165fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
116696f2e892SBill Paul 
11670934f18aSMaxime Henrion 	/* Init our MAC address. */
11688df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
11692e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
11702e3d4b79SPyun YongHyeon 	    eaddr[1] << 8 | eaddr[0]);
11712e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
117296f2e892SBill Paul 
117396f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
117496f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
117596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
117696f2e892SBill Paul 	else
117796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
117896f2e892SBill Paul 
117996f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
118096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
118196f2e892SBill Paul 	else
118296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
118396f2e892SBill Paul 
11840934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
118596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
118696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
118796f2e892SBill Paul 
118896f2e892SBill Paul 	/*
118996f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
119096f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
119196f2e892SBill Paul 	 */
119296f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
119396f2e892SBill Paul 		return;
119496f2e892SBill Paul 
11950934f18aSMaxime Henrion 	/* Now program new ones. */
1196eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
11976817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
119896f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
119996f2e892SBill Paul 			continue;
1200acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1201aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1202aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1203acc1bcccSMartin Blapp 		else
1204aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1205aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
120696f2e892SBill Paul 		if (h < 32)
120796f2e892SBill Paul 			hashes[0] |= (1 << h);
120896f2e892SBill Paul 		else
120996f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
121096f2e892SBill Paul 	}
1211eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
121296f2e892SBill Paul 
121396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
121496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
121596f2e892SBill Paul }
121696f2e892SBill Paul 
12172c876e15SPoul-Henning Kamp static void
12180934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
121996f2e892SBill Paul {
12208df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
122196f2e892SBill Paul 	struct ifnet *ifp;
12220934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
122396f2e892SBill Paul 	int h = 0;
1224ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
122596f2e892SBill Paul 
1226fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
122796f2e892SBill Paul 
12288df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
12298df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
123096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
12318df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
123296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
12338df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
123496f2e892SBill Paul 
123596f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
123696f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
123796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
123896f2e892SBill Paul 	else
123996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
124096f2e892SBill Paul 
124196f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
124296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
124396f2e892SBill Paul 	else
124496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
124596f2e892SBill Paul 
124696f2e892SBill Paul 	/*
124796f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
124896f2e892SBill Paul 	 * of broadcast frames.
124996f2e892SBill Paul 	 */
125096f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
125196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
125296f2e892SBill Paul 	else
125396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
125496f2e892SBill Paul 
125596f2e892SBill Paul 	/* first, zot all the existing hash bits */
125696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
125796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
125896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
125996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
126096f2e892SBill Paul 
126196f2e892SBill Paul 	/*
126296f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
126396f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
126496f2e892SBill Paul 	 */
126596f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
126696f2e892SBill Paul 		return;
126796f2e892SBill Paul 
126896f2e892SBill Paul 	/* now program new ones */
1269eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
12706817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
127196f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
127296f2e892SBill Paul 			continue;
1273aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
127496f2e892SBill Paul 		if (h < 32)
127596f2e892SBill Paul 			hashes[0] |= (1 << h);
127696f2e892SBill Paul 		else
127796f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
127896f2e892SBill Paul 	}
1279eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
128096f2e892SBill Paul 
128196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
128296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
128396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
128496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
128596f2e892SBill Paul }
128696f2e892SBill Paul 
12872c876e15SPoul-Henning Kamp static void
12880934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1289feb78939SJonathan Chen {
12908df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
12910934f18aSMaxime Henrion 	struct ifnet *ifp;
12920934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1293feb78939SJonathan Chen 	struct dc_desc *sframe;
1294ee320f98SPyun YongHyeon 	uint32_t h, *sp;
1295feb78939SJonathan Chen 	int i;
1296feb78939SJonathan Chen 
1297fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1298feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1299feb78939SJonathan Chen 
1300feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1301feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1302feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
13035f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
130456e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
13050934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1306feb78939SJonathan Chen 
13075f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1308af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1309af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1310feb78939SJonathan Chen 
131156e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1312feb78939SJonathan Chen 
1313feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1314feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1315feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1316feb78939SJonathan Chen 	else
1317feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1318feb78939SJonathan Chen 
1319feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1320feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1321feb78939SJonathan Chen 	else
1322feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1323feb78939SJonathan Chen 
1324eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
13256817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1326feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1327feb78939SJonathan Chen 			continue;
1328aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13291d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1330af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1331feb78939SJonathan Chen 	}
1332eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
1333feb78939SJonathan Chen 
1334feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1335aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1336af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1337feb78939SJonathan Chen 	}
1338feb78939SJonathan Chen 
13398df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
13408df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13418df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13428df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13438df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1344feb78939SJonathan Chen 
1345feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1346feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1347af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
13488c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
13498c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
13505f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1351feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1352feb78939SJonathan Chen 
1353feb78939SJonathan Chen 	/*
13540934f18aSMaxime Henrion 	 * Wait some time...
1355feb78939SJonathan Chen 	 */
1356feb78939SJonathan Chen 	DELAY(1000);
1357feb78939SJonathan Chen 
1358b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
1359feb78939SJonathan Chen }
1360feb78939SJonathan Chen 
1361e3d2833aSAlfred Perlstein static void
13620934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
136396f2e892SBill Paul {
13640934f18aSMaxime Henrion 
136596f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13661af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
136796f2e892SBill Paul 		dc_setfilt_21143(sc);
136896f2e892SBill Paul 
136996f2e892SBill Paul 	if (DC_IS_ASIX(sc))
137096f2e892SBill Paul 		dc_setfilt_asix(sc);
137196f2e892SBill Paul 
137296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
137396f2e892SBill Paul 		dc_setfilt_admtek(sc);
137496f2e892SBill Paul 
1375feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1376feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
137796f2e892SBill Paul }
137896f2e892SBill Paul 
1379e3d2833aSAlfred Perlstein static void
13801da7683aSPyun YongHyeon dc_netcfg_wait(struct dc_softc *sc)
138196f2e892SBill Paul {
1382ee320f98SPyun YongHyeon 	uint32_t isr;
13831da7683aSPyun YongHyeon 	int i;
138496f2e892SBill Paul 
138596f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
138696f2e892SBill Paul 		isr = CSR_READ_4(sc, DC_ISR);
1387d467c136SBill Paul 		if (isr & DC_ISR_TX_IDLE &&
1388351267c1SMartin Blapp 		    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1389351267c1SMartin Blapp 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
139096f2e892SBill Paul 			break;
1391d467c136SBill Paul 		DELAY(10);
139296f2e892SBill Paul 	}
13937a6fab66SWarner Losh 	if (i == DC_TIMEOUT && bus_child_present(sc->dc_dev)) {
1394432120f2SMarius Strobl 		if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13956b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
13961da7683aSPyun YongHyeon 			    "%s: failed to force tx to idle state\n", __func__);
1397432120f2SMarius Strobl 		if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1398432120f2SMarius Strobl 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1399d0d67284SMarius Strobl 		    !DC_HAS_BROKEN_RXSTATE(sc))
1400432120f2SMarius Strobl 			device_printf(sc->dc_dev,
14011da7683aSPyun YongHyeon 			    "%s: failed to force rx to idle state\n", __func__);
1402432120f2SMarius Strobl 	}
140396f2e892SBill Paul }
140496f2e892SBill Paul 
14051da7683aSPyun YongHyeon /*
14061da7683aSPyun YongHyeon  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
14071da7683aSPyun YongHyeon  * the netconfig register, we first have to put the transmit and/or
14081da7683aSPyun YongHyeon  * receive logic in the idle state.
14091da7683aSPyun YongHyeon  */
14101da7683aSPyun YongHyeon static void
14111da7683aSPyun YongHyeon dc_setcfg(struct dc_softc *sc, int media)
14121da7683aSPyun YongHyeon {
14131da7683aSPyun YongHyeon 	int restart = 0, watchdogreg;
14141da7683aSPyun YongHyeon 
14151da7683aSPyun YongHyeon 	if (IFM_SUBTYPE(media) == IFM_NONE)
14161da7683aSPyun YongHyeon 		return;
14171da7683aSPyun YongHyeon 
14181da7683aSPyun YongHyeon 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
14191da7683aSPyun YongHyeon 		restart = 1;
14201da7683aSPyun YongHyeon 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
14211da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
14221da7683aSPyun YongHyeon 	}
14231da7683aSPyun YongHyeon 
142496f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1425042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1426042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
142796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1428bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14290934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14308273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14318273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14328273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14334c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1434bf645417SBill Paul 			} else {
1435bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1436bf645417SBill Paul 			}
143796f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
143896f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
143996f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
144096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
144196f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
144288d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
144396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
144496f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
144596f2e892SBill Paul 		} else {
144696f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
144796f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
144896f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
144996f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
145096f2e892SBill Paul 			}
1451318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1452318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1453318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
145496f2e892SBill Paul 		}
145596f2e892SBill Paul 	}
145696f2e892SBill Paul 
145796f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1458042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1459042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
146096f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14610934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14624c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14638273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14648273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14658273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14668273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14674c2efe27SBill Paul 			} else {
14684c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14694c2efe27SBill Paul 			}
147096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
147196f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
147296f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
147396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
147488d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
147596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
147696f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
147796f2e892SBill Paul 		} else {
147896f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
147996f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
148096f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
148196f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
148296f2e892SBill Paul 			}
148396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1484318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
148596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14865c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14875c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14885c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14895c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14905c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14915c1cfac4SBill Paul 				else
14925c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14935c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14945c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14955c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14965c1cfac4SBill Paul 				DELAY(20000);
14975c1cfac4SBill Paul 			}
149896f2e892SBill Paul 		}
149996f2e892SBill Paul 	}
150096f2e892SBill Paul 
1501f43d9309SBill Paul 	/*
1502f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1503f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1504f43d9309SBill Paul 	 * on the external MII port.
1505f43d9309SBill Paul 	 */
1506f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
150745521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1508f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1509f43d9309SBill Paul 			sc->dc_link = 1;
1510f43d9309SBill Paul 		} else {
1511f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1512f43d9309SBill Paul 		}
1513f43d9309SBill Paul 	}
1514f43d9309SBill Paul 
151596f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
151696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
151796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
151896f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
151996f2e892SBill Paul 	} else {
152096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
152196f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
152296f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
152396f2e892SBill Paul 	}
152496f2e892SBill Paul 
152596f2e892SBill Paul 	if (restart)
152696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
152796f2e892SBill Paul }
152896f2e892SBill Paul 
1529e3d2833aSAlfred Perlstein static void
15300934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
153196f2e892SBill Paul {
15320934f18aSMaxime Henrion 	int i;
153396f2e892SBill Paul 
153496f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
153596f2e892SBill Paul 
153696f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
153796f2e892SBill Paul 		DELAY(10);
153896f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
153996f2e892SBill Paul 			break;
154096f2e892SBill Paul 	}
154196f2e892SBill Paul 
15421af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
15431d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
154496f2e892SBill Paul 		DELAY(10000);
154596f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
154696f2e892SBill Paul 		i = 0;
154796f2e892SBill Paul 	}
154896f2e892SBill Paul 
154996f2e892SBill Paul 	if (i == DC_TIMEOUT)
15506b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev, "reset never completed!\n");
155196f2e892SBill Paul 
155296f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
155396f2e892SBill Paul 	DELAY(1000);
155496f2e892SBill Paul 
155596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
155696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
155796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
155896f2e892SBill Paul 
155991cc2adbSBill Paul 	/*
156091cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
156191cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
156291cc2adbSBill Paul 	 * into a state where it will never come out of reset
156391cc2adbSBill Paul 	 * until we reset the whole chip again.
156491cc2adbSBill Paul 	 */
15655c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
156691cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1567d314ebf5SPyun YongHyeon 		CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFFFFFF);
15685c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15695c1cfac4SBill Paul 	}
157096f2e892SBill Paul }
157196f2e892SBill Paul 
1572ebc284ccSMarius Strobl static const struct dc_type *
15730934f18aSMaxime Henrion dc_devtype(device_t dev)
157496f2e892SBill Paul {
1575ebc284ccSMarius Strobl 	const struct dc_type *t;
1576ee320f98SPyun YongHyeon 	uint32_t devid;
1577ee320f98SPyun YongHyeon 	uint8_t rev;
157896f2e892SBill Paul 
157996f2e892SBill Paul 	t = dc_devs;
15801e2e70b1SJohn Baldwin 	devid = pci_get_devid(dev);
15811e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
158296f2e892SBill Paul 
158396f2e892SBill Paul 	while (t->dc_name != NULL) {
15841e2e70b1SJohn Baldwin 		if (devid == t->dc_devid && rev >= t->dc_minrev)
158596f2e892SBill Paul 			return (t);
158696f2e892SBill Paul 		t++;
158796f2e892SBill Paul 	}
158896f2e892SBill Paul 
158996f2e892SBill Paul 	return (NULL);
159096f2e892SBill Paul }
159196f2e892SBill Paul 
159296f2e892SBill Paul /*
159396f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
159496f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
159596f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
159696f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
159796f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
159896f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
159996f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
160096f2e892SBill Paul  */
1601e3d2833aSAlfred Perlstein static int
16020934f18aSMaxime Henrion dc_probe(device_t dev)
160396f2e892SBill Paul {
1604ebc284ccSMarius Strobl 	const struct dc_type *t;
160596f2e892SBill Paul 
160696f2e892SBill Paul 	t = dc_devtype(dev);
160796f2e892SBill Paul 
160896f2e892SBill Paul 	if (t != NULL) {
160996f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1610d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
161196f2e892SBill Paul 	}
161296f2e892SBill Paul 
161396f2e892SBill Paul 	return (ENXIO);
161496f2e892SBill Paul }
161596f2e892SBill Paul 
1616e3d2833aSAlfred Perlstein static void
16170934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
16185c1cfac4SBill Paul {
16195c1cfac4SBill Paul 	struct dc_mediainfo *m;
1620ee320f98SPyun YongHyeon 	uint8_t *p;
16215c1cfac4SBill Paul 	int i;
1622ee320f98SPyun YongHyeon 	uint32_t reg;
16235c1cfac4SBill Paul 
16245c1cfac4SBill Paul 	m = sc->dc_mi;
16255c1cfac4SBill Paul 
16265c1cfac4SBill Paul 	while (m != NULL) {
16275c1cfac4SBill Paul 		if (m->dc_media == media)
16285c1cfac4SBill Paul 			break;
16295c1cfac4SBill Paul 		m = m->dc_next;
16305c1cfac4SBill Paul 	}
16315c1cfac4SBill Paul 
16325c1cfac4SBill Paul 	if (m == NULL)
16335c1cfac4SBill Paul 		return;
16345c1cfac4SBill Paul 
16355c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_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 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16415c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16425c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16435c1cfac4SBill Paul 	}
16445c1cfac4SBill Paul }
16455c1cfac4SBill Paul 
1646abe4e865SPyun YongHyeon static int
16470934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16485c1cfac4SBill Paul {
16495c1cfac4SBill Paul 	struct dc_mediainfo *m;
16505c1cfac4SBill Paul 
16510934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1652abe4e865SPyun YongHyeon 	if (m == NULL) {
1653abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1654abe4e865SPyun YongHyeon 		return (ENOMEM);
1655abe4e865SPyun YongHyeon 	}
165687f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
165787f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16585c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
165987f4fa15SMartin Blapp 		break;
166087f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16615c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
166287f4fa15SMartin Blapp 		break;
166387f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16645c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
166587f4fa15SMartin Blapp 		break;
166687f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16675c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
166887f4fa15SMartin Blapp 		break;
166987f4fa15SMartin Blapp 	default:
167087f4fa15SMartin Blapp 		break;
167187f4fa15SMartin Blapp 	}
16725c1cfac4SBill Paul 
167387f4fa15SMartin Blapp 	/*
167487f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
167587f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
167687f4fa15SMartin Blapp 	 * supply Media Specific Data.
167787f4fa15SMartin Blapp 	 */
167887f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16795c1cfac4SBill Paul 		m->dc_gp_len = 2;
168087f4fa15SMartin Blapp 		m->dc_gp_ptr =
1681ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
168287f4fa15SMartin Blapp 	} else {
168387f4fa15SMartin Blapp 		m->dc_gp_len = 2;
168487f4fa15SMartin Blapp 		m->dc_gp_ptr =
1685ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
168687f4fa15SMartin Blapp 	}
16875c1cfac4SBill Paul 
16885c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16895c1cfac4SBill Paul 	sc->dc_mi = m;
16905c1cfac4SBill Paul 
16915c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
1692abe4e865SPyun YongHyeon 	return (0);
16935c1cfac4SBill Paul }
16945c1cfac4SBill Paul 
1695abe4e865SPyun YongHyeon static int
16960934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16975c1cfac4SBill Paul {
16985c1cfac4SBill Paul 	struct dc_mediainfo *m;
16995c1cfac4SBill Paul 
17000934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1701abe4e865SPyun YongHyeon 	if (m == NULL) {
1702abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1703abe4e865SPyun YongHyeon 		return (ENOMEM);
1704abe4e865SPyun YongHyeon 	}
17055c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
17065c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
17075c1cfac4SBill Paul 
17085c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
17095c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
17105c1cfac4SBill Paul 
17115c1cfac4SBill Paul 	m->dc_gp_len = 2;
1712ee320f98SPyun YongHyeon 	m->dc_gp_ptr = (uint8_t *)&l->dc_sym_gpio_ctl;
17135c1cfac4SBill Paul 
17145c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17155c1cfac4SBill Paul 	sc->dc_mi = m;
17165c1cfac4SBill Paul 
17175c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
1718abe4e865SPyun YongHyeon 	return (0);
17195c1cfac4SBill Paul }
17205c1cfac4SBill Paul 
1721abe4e865SPyun YongHyeon static int
17220934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17235c1cfac4SBill Paul {
17245c1cfac4SBill Paul 	struct dc_mediainfo *m;
1725ee320f98SPyun YongHyeon 	uint8_t *p;
17265c1cfac4SBill Paul 
17270934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1728abe4e865SPyun YongHyeon 	if (m == NULL) {
1729abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1730abe4e865SPyun YongHyeon 		return (ENOMEM);
1731abe4e865SPyun YongHyeon 	}
17325c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17335c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17345c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17355c1cfac4SBill Paul 
1736ee320f98SPyun YongHyeon 	p = (uint8_t *)l;
17375c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17385c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17395c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17405c1cfac4SBill Paul 	m->dc_reset_len = *p;
17415c1cfac4SBill Paul 	p++;
17425c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17435c1cfac4SBill Paul 
17445c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17455c1cfac4SBill Paul 	sc->dc_mi = m;
1746abe4e865SPyun YongHyeon 	return (0);
17475c1cfac4SBill Paul }
17485c1cfac4SBill Paul 
1749abe4e865SPyun YongHyeon static int
17500934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17513097aa70SWarner Losh {
17523097aa70SWarner Losh 	int size;
17533097aa70SWarner Losh 
1754abe4e865SPyun YongHyeon 	size = DC_ROM_SIZE(bits);
17553097aa70SWarner Losh 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
1756abe4e865SPyun YongHyeon 	if (sc->dc_srom == NULL) {
1757abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate SROM buffer\n");
1758abe4e865SPyun YongHyeon 		return (ENOMEM);
1759abe4e865SPyun YongHyeon 	}
17603097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1761abe4e865SPyun YongHyeon 	return (0);
17623097aa70SWarner Losh }
17633097aa70SWarner Losh 
1764abe4e865SPyun YongHyeon static int
17650934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17665c1cfac4SBill Paul {
17675c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17685c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
1769abe4e865SPyun YongHyeon 	int error, have_mii, i, loff;
17705c1cfac4SBill Paul 	char *ptr;
17715c1cfac4SBill Paul 
1772f956e0b3SMartin Blapp 	have_mii = 0;
17735c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17745c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17755c1cfac4SBill Paul 
17765c1cfac4SBill Paul 	ptr = (char *)lhdr;
17775c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1778f956e0b3SMartin Blapp 	/*
1779f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1780f956e0b3SMartin Blapp 	 */
1781f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1782f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1783f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1784f956e0b3SMartin Blapp 		    have_mii++;
1785f956e0b3SMartin Blapp 
1786f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1787f956e0b3SMartin Blapp 		ptr++;
1788f956e0b3SMartin Blapp 	}
1789f956e0b3SMartin Blapp 
1790f956e0b3SMartin Blapp 	/*
1791f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1792f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1793f956e0b3SMartin Blapp 	 */
1794f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1795f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1796abe4e865SPyun YongHyeon 	error = 0;
17975c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17985c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17995c1cfac4SBill Paul 		switch (hdr->dc_type) {
18005c1cfac4SBill Paul 		case DC_EBLOCK_MII:
1801abe4e865SPyun YongHyeon 			error = dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
18025c1cfac4SBill Paul 			break;
18035c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1804f956e0b3SMartin Blapp 			if (! have_mii)
1805abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sia(sc,
1806f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
18075c1cfac4SBill Paul 			break;
18085c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1809f956e0b3SMartin Blapp 			if (! have_mii)
1810abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sym(sc,
1811f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
18125c1cfac4SBill Paul 			break;
18135c1cfac4SBill Paul 		default:
18145c1cfac4SBill Paul 			/* Don't care. Yet. */
18155c1cfac4SBill Paul 			break;
18165c1cfac4SBill Paul 		}
18175c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
18185c1cfac4SBill Paul 		ptr++;
18195c1cfac4SBill Paul 	}
1820abe4e865SPyun YongHyeon 	return (error);
18215c1cfac4SBill Paul }
18225c1cfac4SBill Paul 
182356e5e7aeSMaxime Henrion static void
182456e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
182556e5e7aeSMaxime Henrion {
18265f14ee23SPyun YongHyeon 	bus_addr_t *paddr;
182756e5e7aeSMaxime Henrion 
1828ebc284ccSMarius Strobl 	KASSERT(nseg == 1,
1829ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
183056e5e7aeSMaxime Henrion 	paddr = arg;
183156e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
183256e5e7aeSMaxime Henrion }
183356e5e7aeSMaxime Henrion 
18345f14ee23SPyun YongHyeon static int
18355f14ee23SPyun YongHyeon dc_dma_alloc(struct dc_softc *sc)
18365f14ee23SPyun YongHyeon {
18375f14ee23SPyun YongHyeon 	int error, i;
18385f14ee23SPyun YongHyeon 
18395f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dc_dev), 1, 0,
18405f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
18415f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
18425f14ee23SPyun YongHyeon 	    NULL, NULL, &sc->dc_ptag);
18435f14ee23SPyun YongHyeon 	if (error) {
18445f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18455f14ee23SPyun YongHyeon 		    "failed to allocate parent DMA tag\n");
18465f14ee23SPyun YongHyeon 		goto fail;
18475f14ee23SPyun YongHyeon 	}
18485f14ee23SPyun YongHyeon 
18495f14ee23SPyun YongHyeon 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
18505f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18515f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_RX_LIST_SZ, 1,
18525f14ee23SPyun YongHyeon 	    DC_RX_LIST_SZ, 0, NULL, NULL, &sc->dc_rx_ltag);
18535f14ee23SPyun YongHyeon 	if (error) {
18545f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX list DMA tag\n");
18555f14ee23SPyun YongHyeon 		goto fail;
18565f14ee23SPyun YongHyeon 	}
18575f14ee23SPyun YongHyeon 
18585f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18595f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_TX_LIST_SZ, 1,
18605f14ee23SPyun YongHyeon 	    DC_TX_LIST_SZ, 0, NULL, NULL, &sc->dc_tx_ltag);
18615f14ee23SPyun YongHyeon 	if (error) {
18625f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX list DMA tag\n");
18635f14ee23SPyun YongHyeon 		goto fail;
18645f14ee23SPyun YongHyeon 	}
18655f14ee23SPyun YongHyeon 
18665f14ee23SPyun YongHyeon 	/* RX descriptor list. */
18675f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_rx_ltag,
18685f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_rx_list, BUS_DMA_NOWAIT |
18695f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_rx_lmap);
18705f14ee23SPyun YongHyeon 	if (error) {
18715f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18725f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for RX list\n");
18735f14ee23SPyun YongHyeon 		goto fail;
18745f14ee23SPyun YongHyeon 	}
18755f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_rx_ltag, sc->dc_rx_lmap,
18765f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_rx_list, DC_RX_LIST_SZ, dc_dma_map_addr,
18775f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_rx_list_paddr, BUS_DMA_NOWAIT);
18785f14ee23SPyun YongHyeon 	if (error) {
18795f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18805f14ee23SPyun YongHyeon 		    "failed to load DMA'able memory for RX list\n");
18815f14ee23SPyun YongHyeon 		goto fail;
18825f14ee23SPyun YongHyeon 	}
18835f14ee23SPyun YongHyeon 	/* TX descriptor list. */
18845f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_tx_ltag,
18855f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_tx_list, BUS_DMA_NOWAIT |
18865f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_tx_lmap);
18875f14ee23SPyun YongHyeon 	if (error) {
18885f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18895f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for TX list\n");
18905f14ee23SPyun YongHyeon 		goto fail;
18915f14ee23SPyun YongHyeon 	}
18925f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_tx_ltag, sc->dc_tx_lmap,
18935f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_tx_list, DC_TX_LIST_SZ, dc_dma_map_addr,
18945f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_tx_list_paddr, BUS_DMA_NOWAIT);
18955f14ee23SPyun YongHyeon 	if (error) {
18965f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18975f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for TX list\n");
18985f14ee23SPyun YongHyeon 		goto fail;
18995f14ee23SPyun YongHyeon 	}
19005f14ee23SPyun YongHyeon 
19015f14ee23SPyun YongHyeon 	/*
19025f14ee23SPyun YongHyeon 	 * Allocate a busdma tag and DMA safe memory for the multicast
19035f14ee23SPyun YongHyeon 	 * setup frame.
19045f14ee23SPyun YongHyeon 	 */
19055f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
19065f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19075f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
19085f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_stag);
19095f14ee23SPyun YongHyeon 	if (error) {
19105f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19115f14ee23SPyun YongHyeon 		    "failed to create DMA tag for setup frame\n");
19125f14ee23SPyun YongHyeon 		goto fail;
19135f14ee23SPyun YongHyeon 	}
19145f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
19155f14ee23SPyun YongHyeon 	    BUS_DMA_NOWAIT, &sc->dc_smap);
19165f14ee23SPyun YongHyeon 	if (error) {
19175f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19185f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for setup frame\n");
19195f14ee23SPyun YongHyeon 		goto fail;
19205f14ee23SPyun YongHyeon 	}
19215f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
19225f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
19235f14ee23SPyun YongHyeon 	if (error) {
19245f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19255f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for setup frame\n");
19265f14ee23SPyun YongHyeon 		goto fail;
19275f14ee23SPyun YongHyeon 	}
19285f14ee23SPyun YongHyeon 
19295f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for RX mbufs. */
19305f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_RXBUF_ALIGN, 0,
19315f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19325f14ee23SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->dc_rx_mtag);
19335f14ee23SPyun YongHyeon 	if (error) {
19345f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX mbuf tag\n");
19355f14ee23SPyun YongHyeon 		goto fail;
19365f14ee23SPyun YongHyeon 	}
19375f14ee23SPyun YongHyeon 
19385f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for TX mbufs. */
19395f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, 1, 0,
19405f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19415f14ee23SPyun YongHyeon 	    MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
19425f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_tx_mtag);
19435f14ee23SPyun YongHyeon 	if (error) {
19445f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX mbuf tag\n");
19455f14ee23SPyun YongHyeon 		goto fail;
19465f14ee23SPyun YongHyeon 	}
19475f14ee23SPyun YongHyeon 
19485f14ee23SPyun YongHyeon 	/* Create the TX/RX busdma maps. */
19495f14ee23SPyun YongHyeon 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
19505f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_tx_mtag, 0,
19515f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_tx_map[i]);
19525f14ee23SPyun YongHyeon 		if (error) {
19535f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19545f14ee23SPyun YongHyeon 			    "failed to create TX mbuf dmamap\n");
19555f14ee23SPyun YongHyeon 			goto fail;
19565f14ee23SPyun YongHyeon 		}
19575f14ee23SPyun YongHyeon 	}
19585f14ee23SPyun YongHyeon 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
19595f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_rx_mtag, 0,
19605f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_rx_map[i]);
19615f14ee23SPyun YongHyeon 		if (error) {
19625f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19635f14ee23SPyun YongHyeon 			    "failed to create RX mbuf dmamap\n");
19645f14ee23SPyun YongHyeon 			goto fail;
19655f14ee23SPyun YongHyeon 		}
19665f14ee23SPyun YongHyeon 	}
19675f14ee23SPyun YongHyeon 	error = bus_dmamap_create(sc->dc_rx_mtag, 0, &sc->dc_sparemap);
19685f14ee23SPyun YongHyeon 	if (error) {
19695f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19705f14ee23SPyun YongHyeon 		    "failed to create spare RX mbuf dmamap\n");
19715f14ee23SPyun YongHyeon 		goto fail;
19725f14ee23SPyun YongHyeon 	}
19735f14ee23SPyun YongHyeon 
19745f14ee23SPyun YongHyeon fail:
19755f14ee23SPyun YongHyeon 	return (error);
19765f14ee23SPyun YongHyeon }
19775f14ee23SPyun YongHyeon 
19785f14ee23SPyun YongHyeon static void
19795f14ee23SPyun YongHyeon dc_dma_free(struct dc_softc *sc)
19805f14ee23SPyun YongHyeon {
19815f14ee23SPyun YongHyeon 	int i;
19825f14ee23SPyun YongHyeon 
19835f14ee23SPyun YongHyeon 	/* RX buffers. */
19845f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19855f14ee23SPyun YongHyeon 		for (i = 0; i < DC_RX_LIST_CNT; i++) {
19865f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
19875f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_rx_mtag,
19885f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_rx_map[i]);
19895f14ee23SPyun YongHyeon 		}
19905f14ee23SPyun YongHyeon 		if (sc->dc_sparemap != NULL)
19915f14ee23SPyun YongHyeon 			bus_dmamap_destroy(sc->dc_rx_mtag, sc->dc_sparemap);
19925f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_mtag);
19935f14ee23SPyun YongHyeon 	}
19945f14ee23SPyun YongHyeon 
19955f14ee23SPyun YongHyeon 	/* TX buffers. */
19965f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19975f14ee23SPyun YongHyeon 		for (i = 0; i < DC_TX_LIST_CNT; i++) {
19985f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
19995f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_tx_mtag,
20005f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_tx_map[i]);
20015f14ee23SPyun YongHyeon 		}
20025f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_mtag);
20035f14ee23SPyun YongHyeon 	}
20045f14ee23SPyun YongHyeon 
20055f14ee23SPyun YongHyeon 	/* RX descriptor list. */
20065f14ee23SPyun YongHyeon 	if (sc->dc_rx_ltag) {
20075f14ee23SPyun YongHyeon 		if (sc->dc_rx_lmap != NULL)
20085f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_ltag, sc->dc_rx_lmap);
20095f14ee23SPyun YongHyeon 		if (sc->dc_rx_lmap != NULL && sc->dc_ldata.dc_rx_list != NULL)
20105f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_rx_ltag, sc->dc_ldata.dc_rx_list,
20115f14ee23SPyun YongHyeon 			    sc->dc_rx_lmap);
20125f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_ltag);
20135f14ee23SPyun YongHyeon 	}
20145f14ee23SPyun YongHyeon 
20155f14ee23SPyun YongHyeon 	/* TX descriptor list. */
20165f14ee23SPyun YongHyeon 	if (sc->dc_tx_ltag) {
20175f14ee23SPyun YongHyeon 		if (sc->dc_tx_lmap != NULL)
20185f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_ltag, sc->dc_tx_lmap);
20195f14ee23SPyun YongHyeon 		if (sc->dc_tx_lmap != NULL && sc->dc_ldata.dc_tx_list != NULL)
20205f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_tx_ltag, sc->dc_ldata.dc_tx_list,
20215f14ee23SPyun YongHyeon 			    sc->dc_tx_lmap);
20225f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_ltag);
20235f14ee23SPyun YongHyeon 	}
20245f14ee23SPyun YongHyeon 
20255f14ee23SPyun YongHyeon 	/* multicast setup frame. */
20265f14ee23SPyun YongHyeon 	if (sc->dc_stag) {
20275f14ee23SPyun YongHyeon 		if (sc->dc_smap != NULL)
20285f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_stag, sc->dc_smap);
20295f14ee23SPyun YongHyeon 		if (sc->dc_smap != NULL && sc->dc_cdata.dc_sbuf != NULL)
20305f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf,
20315f14ee23SPyun YongHyeon 			    sc->dc_smap);
20325f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_stag);
20335f14ee23SPyun YongHyeon 	}
20345f14ee23SPyun YongHyeon }
20355f14ee23SPyun YongHyeon 
203696f2e892SBill Paul /*
203796f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
203896f2e892SBill Paul  * setup and ethernet/BPF attach.
203996f2e892SBill Paul  */
2040e3d2833aSAlfred Perlstein static int
20410934f18aSMaxime Henrion dc_attach(device_t dev)
204296f2e892SBill Paul {
20438df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
2044ee320f98SPyun YongHyeon 	uint32_t command;
204596f2e892SBill Paul 	struct dc_softc *sc;
204696f2e892SBill Paul 	struct ifnet *ifp;
2047b289c607SPyun YongHyeon 	struct dc_mediainfo *m;
2048ee320f98SPyun YongHyeon 	uint32_t reg, revision;
20495f14ee23SPyun YongHyeon 	int error, mac_offset, phy, rid, tmp;
2050ee320f98SPyun YongHyeon 	uint8_t *mac;
205196f2e892SBill Paul 
205296f2e892SBill Paul 	sc = device_get_softc(dev);
20536b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
205496f2e892SBill Paul 
20556008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2056c8b27acaSJohn Baldwin 	    MTX_DEF);
2057c3e7434fSWarner Losh 
205896f2e892SBill Paul 	/*
205996f2e892SBill Paul 	 * Map control/status registers.
206096f2e892SBill Paul 	 */
206107f65363SBill Paul 	pci_enable_busmaster(dev);
206296f2e892SBill Paul 
206396f2e892SBill Paul 	rid = DC_RID;
20645f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
206596f2e892SBill Paul 
206696f2e892SBill Paul 	if (sc->dc_res == NULL) {
206722f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
206896f2e892SBill Paul 		error = ENXIO;
2069608654d4SNate Lawson 		goto fail;
207096f2e892SBill Paul 	}
207196f2e892SBill Paul 
207296f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
207396f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
207496f2e892SBill Paul 
20750934f18aSMaxime Henrion 	/* Allocate interrupt. */
207654f1f1d1SNate Lawson 	rid = 0;
20775f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
207854f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
207954f1f1d1SNate Lawson 
208054f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
208122f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
208254f1f1d1SNate Lawson 		error = ENXIO;
208354f1f1d1SNate Lawson 		goto fail;
208454f1f1d1SNate Lawson 	}
208554f1f1d1SNate Lawson 
208696f2e892SBill Paul 	/* Need this info to decide on a chip type. */
208796f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
20881e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
208996f2e892SBill Paul 
2090abe4e865SPyun YongHyeon 	error = 0;
20916d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
20921e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
20931e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
20941e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
20951e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
2096eecb3844SMartin Blapp 		dc_eeprom_width(sc);
2097eecb3844SMartin Blapp 
20981e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
20991e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
210096f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
210196f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2102042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
21035c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
2104abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2105abe4e865SPyun YongHyeon 		if (error != 0)
2106abe4e865SPyun YongHyeon 			goto fail;
210796f2e892SBill Paul 		break;
21081e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
21091e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
21101e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
211196f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
2112318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
2113318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
21147dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
21154a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
21161e2e70b1SJohn Baldwin 
21170a46b1dcSBill Paul 		/* Increase the latency timer value. */
21181e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
211996f2e892SBill Paul 		break;
21201e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
212196f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
212296f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
212396f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
212496f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2125abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2126abe4e865SPyun YongHyeon 		if (error != 0)
2127abe4e865SPyun YongHyeon 			goto fail;
212896f2e892SBill Paul 		break;
2129593a1aeaSMartin Blapp 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983):
21301e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
21311e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
21321e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
21331e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
21341e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
21351e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
21361e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
21371e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
21381e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
21391e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
21401e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
21411e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
214217762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
214317762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
2144593a1aeaSMartin Blapp 		sc->dc_type = DC_TYPE_AN983;
2145acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
214696f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
214796f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
214896f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2149129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
215096f2e892SBill Paul 		break;
21511e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
21521e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
215396f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
215496f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
215596f2e892SBill Paul 		}
2156318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
215796f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
2158318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
2159318b02fdSBill Paul 		}
2160318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
216196f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
216296f2e892SBill Paul 		break;
21631e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
21641e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
216579d11e09SBill Paul 		/*
216679d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
216779d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
216879d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
216979d11e09SBill Paul 		 * get the right number of bits out of the
217079d11e09SBill Paul 		 * CRC routine.
217179d11e09SBill Paul 		 */
217279d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
217379d11e09SBill Paul 		    revision < DC_REVISION_98725)
217479d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
217596f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
217696f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2177318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
217896f2e892SBill Paul 		break;
21791e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
2180ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
2181ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2182ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2183ead7cde9SBill Paul 		break;
21841e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
218596f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
218679d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
2187318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
218896f2e892SBill Paul 		break;
21891e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
219096f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
219191cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
219296f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
219396f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
2194abe4e865SPyun YongHyeon 		if (sc->dc_pnic_rx_buf == NULL) {
2195abe4e865SPyun YongHyeon 			device_printf(sc->dc_dev,
2196abe4e865SPyun YongHyeon 			    "Could not allocate PNIC RX buffer\n");
2197abe4e865SPyun YongHyeon 			error = ENOMEM;
2198abe4e865SPyun YongHyeon 			goto fail;
2199abe4e865SPyun YongHyeon 		}
220096f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
220196f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
220296f2e892SBill Paul 		break;
22031e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
220496f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
220596f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
220696f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
220796f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
220896f2e892SBill Paul 		break;
22091e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
2210feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
22112dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
22122dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2213feb78939SJonathan Chen 		/*
2214feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2215feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
22162dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2217feb78939SJonathan Chen 		 */
22183097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2219feb78939SJonathan Chen 		break;
22201e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
22211af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
22221af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
22231af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
22241af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2225abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2226abe4e865SPyun YongHyeon 		if (error != 0)
2227abe4e865SPyun YongHyeon 			goto fail;
22281af8bec7SBill Paul 		break;
222996f2e892SBill Paul 	default:
22301e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
22311e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
223296f2e892SBill Paul 		break;
223396f2e892SBill Paul 	}
223496f2e892SBill Paul 
223596f2e892SBill Paul 	/* Save the cache line size. */
223688d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
223788d739dcSBill Paul 		sc->dc_cachesize = 0;
223888d739dcSBill Paul 	else
22391e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
224096f2e892SBill Paul 
224196f2e892SBill Paul 	/* Reset the adapter. */
224296f2e892SBill Paul 	dc_reset(sc);
224396f2e892SBill Paul 
224496f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2245feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
224696f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
224796f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
224896f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
224996f2e892SBill Paul 	}
225096f2e892SBill Paul 
225196f2e892SBill Paul 	/*
225296f2e892SBill Paul 	 * Try to learn something about the supported media.
225396f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
225496f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
225596f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
225696f2e892SBill Paul 	 * Intel 21143.
225796f2e892SBill Paul 	 */
2258abe4e865SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
2259abe4e865SPyun YongHyeon 		error = dc_parse_21143_srom(sc);
2260abe4e865SPyun YongHyeon 		if (error != 0)
2261abe4e865SPyun YongHyeon 			goto fail;
2262abe4e865SPyun YongHyeon 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
226396f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
226496f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
226596f2e892SBill Paul 		else
226696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
226796f2e892SBill Paul 	} else if (!sc->dc_pmode)
226896f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
226996f2e892SBill Paul 
227096f2e892SBill Paul 	/*
227196f2e892SBill Paul 	 * Get station address from the EEPROM.
227296f2e892SBill Paul 	 */
227396f2e892SBill Paul 	switch(sc->dc_type) {
227496f2e892SBill Paul 	case DC_TYPE_98713:
227596f2e892SBill Paul 	case DC_TYPE_98713A:
227696f2e892SBill Paul 	case DC_TYPE_987x5:
227796f2e892SBill Paul 	case DC_TYPE_PNICII:
227896f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
227996f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
228096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
228196f2e892SBill Paul 		break;
228296f2e892SBill Paul 	case DC_TYPE_PNIC:
228396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
228496f2e892SBill Paul 		break;
228596f2e892SBill Paul 	case DC_TYPE_DM9102:
2286ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2287ec6a7299SMaxime Henrion #ifdef __sparc64__
2288ec6a7299SMaxime Henrion 		/*
2289ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2290802cab03SMarius Strobl 		 * the EEPROM is all zero and we have to get it from the FCode.
2291ec6a7299SMaxime Henrion 		 */
2292802cab03SMarius Strobl 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
22938069c79dSRuslan Ermilov 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2294ec6a7299SMaxime Henrion #endif
2295ec6a7299SMaxime Henrion 		break;
229696f2e892SBill Paul 	case DC_TYPE_21143:
229796f2e892SBill Paul 	case DC_TYPE_ASIX:
229896f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
229996f2e892SBill Paul 		break;
230096f2e892SBill Paul 	case DC_TYPE_AL981:
2301593a1aeaSMartin Blapp 	case DC_TYPE_AN983:
23022e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR0);
23032e3d4b79SPyun YongHyeon 		mac = (uint8_t *)&eaddr[0];
23042e3d4b79SPyun YongHyeon 		mac[0] = (reg >> 0) & 0xff;
23052e3d4b79SPyun YongHyeon 		mac[1] = (reg >> 8) & 0xff;
23062e3d4b79SPyun YongHyeon 		mac[2] = (reg >> 16) & 0xff;
23072e3d4b79SPyun YongHyeon 		mac[3] = (reg >> 24) & 0xff;
23082e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR1);
23092e3d4b79SPyun YongHyeon 		mac[4] = (reg >> 0) & 0xff;
23102e3d4b79SPyun YongHyeon 		mac[5] = (reg >> 8) & 0xff;
231196f2e892SBill Paul 		break;
23121af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
23130934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
23140934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
23151af8bec7SBill Paul 		break;
2316feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
23170934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2318e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2319e7b01d07SWarner Losh 		if (!mac) {
2320e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2321608654d4SNate Lawson 			error = ENXIO;
2322e7b01d07SWarner Losh 			goto fail;
2323e7b01d07SWarner Losh 		}
2324e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2325feb78939SJonathan Chen 		break;
232696f2e892SBill Paul 	default:
232796f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
232896f2e892SBill Paul 		break;
232996f2e892SBill Paul 	}
233096f2e892SBill Paul 
233139d76ed6SPyun YongHyeon 	bcopy(eaddr, sc->dc_eaddr, sizeof(eaddr));
233239d76ed6SPyun YongHyeon 	/*
233339d76ed6SPyun YongHyeon 	 * If we still have invalid station address, see whether we can
233439d76ed6SPyun YongHyeon 	 * find station address for chip 0.  Some multi-port controllers
233539d76ed6SPyun YongHyeon 	 * just store station address for chip 0 if they have a shared
233639d76ed6SPyun YongHyeon 	 * SROM.
233739d76ed6SPyun YongHyeon 	 */
233839d76ed6SPyun YongHyeon 	if ((sc->dc_eaddr[0] == 0 && (sc->dc_eaddr[1] & ~0xffff) == 0) ||
233939d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[0] == 0xffffffff &&
234039d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[1] & 0xffff) == 0xffff)) {
2341b289c607SPyun YongHyeon 		error = dc_check_multiport(sc);
2342b289c607SPyun YongHyeon 		if (error == 0) {
234339d76ed6SPyun YongHyeon 			bcopy(sc->dc_eaddr, eaddr, sizeof(eaddr));
2344b289c607SPyun YongHyeon 			/* Extract media information. */
2345b289c607SPyun YongHyeon 			if (DC_IS_INTEL(sc) && sc->dc_srom != NULL) {
2346b289c607SPyun YongHyeon 				while (sc->dc_mi != NULL) {
2347b289c607SPyun YongHyeon 					m = sc->dc_mi->dc_next;
2348b289c607SPyun YongHyeon 					free(sc->dc_mi, M_DEVBUF);
2349b289c607SPyun YongHyeon 					sc->dc_mi = m;
2350b289c607SPyun YongHyeon 				}
2351b289c607SPyun YongHyeon 				error = dc_parse_21143_srom(sc);
2352b289c607SPyun YongHyeon 				if (error != 0)
2353b289c607SPyun YongHyeon 					goto fail;
2354b289c607SPyun YongHyeon 			}
2355b289c607SPyun YongHyeon 		} else if (error == ENOMEM)
2356b289c607SPyun YongHyeon 			goto fail;
2357b289c607SPyun YongHyeon 		else
2358b289c607SPyun YongHyeon 			error = 0;
235939d76ed6SPyun YongHyeon 	}
236039d76ed6SPyun YongHyeon 
23615f14ee23SPyun YongHyeon 	if ((error = dc_dma_alloc(sc)) != 0)
236256e5e7aeSMaxime Henrion 		goto fail;
236396f2e892SBill Paul 
2364fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2365fc74a9f9SBrooks Davis 	if (ifp == NULL) {
236622f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2367fc74a9f9SBrooks Davis 		error = ENOSPC;
2368fc74a9f9SBrooks Davis 		goto fail;
2369fc74a9f9SBrooks Davis 	}
237096f2e892SBill Paul 	ifp->if_softc = sc;
23719bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
23723d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
237396f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
237496f2e892SBill Paul 	ifp->if_start = dc_start;
237596f2e892SBill Paul 	ifp->if_init = dc_init;
2376cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2377cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2378cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
237996f2e892SBill Paul 
238096f2e892SBill Paul 	/*
23815c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
23825c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
23835c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
23845c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
23855c1cfac4SBill Paul 	 * driver instead.
238696f2e892SBill Paul 	 */
23878e5d93dbSMarius Strobl 	tmp = 0;
23885c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
23895c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
23905c1cfac4SBill Paul 		tmp = sc->dc_pmode;
23915c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
23925c1cfac4SBill Paul 	}
23935c1cfac4SBill Paul 
23946d431b17SWarner Losh 	/*
23956d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
23968e5d93dbSMarius Strobl 	 * to the MII.  This needs to be done before mii_attach so that
23976d431b17SWarner Losh 	 * we can actually see them.
23986d431b17SWarner Losh 	 */
23996d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
24006d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
24016d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24026d431b17SWarner Losh 		DELAY(10);
24036d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
24046d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24056d431b17SWarner Losh 		DELAY(10);
24066d431b17SWarner Losh 	}
24076d431b17SWarner Losh 
24088e5d93dbSMarius Strobl 	phy = MII_PHY_ANY;
24098e5d93dbSMarius Strobl 	/*
24108e5d93dbSMarius Strobl 	 * Note: both the AL981 and AN983 have internal PHYs, however the
24118e5d93dbSMarius Strobl 	 * AL981 provides direct access to the PHY registers while the AN983
24128e5d93dbSMarius Strobl 	 * uses a serial MII interface. The AN983's MII interface is also
24138e5d93dbSMarius Strobl 	 * buggy in that you can read from any MII address (0 to 31), but
24148e5d93dbSMarius Strobl 	 * only address 1 behaves normally. To deal with both cases, we
24158e5d93dbSMarius Strobl 	 * pretend that the PHY is at MII address 1.
24168e5d93dbSMarius Strobl 	 */
24178e5d93dbSMarius Strobl 	if (DC_IS_ADMTEK(sc))
24188e5d93dbSMarius Strobl 		phy = DC_ADMTEK_PHYADDR;
24198e5d93dbSMarius Strobl 
24208e5d93dbSMarius Strobl 	/*
24218e5d93dbSMarius Strobl 	 * Note: the ukphy probes of the RS7112 report a PHY at MII address
24228e5d93dbSMarius Strobl 	 * 0 (possibly HomePNA?) and 1 (ethernet) so we only respond to the
24238e5d93dbSMarius Strobl 	 * correct one.
24248e5d93dbSMarius Strobl 	 */
24258e5d93dbSMarius Strobl 	if (DC_IS_CONEXANT(sc))
24268e5d93dbSMarius Strobl 		phy = DC_CONEXANT_PHYADDR;
24278e5d93dbSMarius Strobl 
24288e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24298e5d93dbSMarius Strobl 	    dc_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
243096f2e892SBill Paul 
243196f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
24325c1cfac4SBill Paul 		sc->dc_pmode = tmp;
24335c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
243496f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2435042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
24368e5d93dbSMarius Strobl 		mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24378e5d93dbSMarius Strobl 		    dc_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
24388e5d93dbSMarius Strobl 		    MII_OFFSET_ANY, 0);
243978999dd1SBill Paul 		/*
244078999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
244178999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
244278999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
244378999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
244478999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
244578999dd1SBill Paul 		 */
24461e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
24471e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
244878999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
244996f2e892SBill Paul 		error = 0;
245096f2e892SBill Paul 	}
245196f2e892SBill Paul 
245296f2e892SBill Paul 	if (error) {
24538e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
245496f2e892SBill Paul 		goto fail;
245596f2e892SBill Paul 	}
245696f2e892SBill Paul 
2457028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2458028a8491SMartin Blapp 		/*
2459028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2460028a8491SMartin Blapp 		 */
2461028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2462028a8491SMartin Blapp 	}
2463028a8491SMartin Blapp 
246496f2e892SBill Paul 	/*
2465db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2466db40c1aeSDoug Ambrisko 	 */
2467db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
24689ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
246940929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2470e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2471e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2472e695984eSRuslan Ermilov #endif
2473db40c1aeSDoug Ambrisko 
2474c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2475b1d16143SMarius Strobl 	callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
247696f2e892SBill Paul 
2477608654d4SNate Lawson 	/*
2478608654d4SNate Lawson 	 * Call MI attach routine.
2479608654d4SNate Lawson 	 */
24808df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2481608654d4SNate Lawson 
248254f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2483c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2484ef544f63SPaolo Pisati 	    NULL, dc_intr, sc, &sc->dc_intrhand);
2485608654d4SNate Lawson 
2486608654d4SNate Lawson 	if (error) {
248722f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2488693f4477SNate Lawson 		ether_ifdetach(ifp);
248954f1f1d1SNate Lawson 		goto fail;
2490608654d4SNate Lawson 	}
2491510a809eSMike Smith 
249296f2e892SBill Paul fail:
249354f1f1d1SNate Lawson 	if (error)
249454f1f1d1SNate Lawson 		dc_detach(dev);
249596f2e892SBill Paul 	return (error);
249696f2e892SBill Paul }
249796f2e892SBill Paul 
2498693f4477SNate Lawson /*
2499693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2500693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2501693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2502693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2503693f4477SNate Lawson  * allocated.
2504693f4477SNate Lawson  */
2505e3d2833aSAlfred Perlstein static int
25060934f18aSMaxime Henrion dc_detach(device_t dev)
250796f2e892SBill Paul {
250896f2e892SBill Paul 	struct dc_softc *sc;
250996f2e892SBill Paul 	struct ifnet *ifp;
25105c1cfac4SBill Paul 	struct dc_mediainfo *m;
251196f2e892SBill Paul 
251296f2e892SBill Paul 	sc = device_get_softc(dev);
251359f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2514d1ce9105SBill Paul 
2515fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
251696f2e892SBill Paul 
251740929967SGleb Smirnoff #ifdef DEVICE_POLLING
251840929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
251940929967SGleb Smirnoff 		ether_poll_deregister(ifp);
252040929967SGleb Smirnoff #endif
252140929967SGleb Smirnoff 
2522693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2523214073e5SWarner Losh 	if (device_is_attached(dev)) {
2524c8b27acaSJohn Baldwin 		DC_LOCK(sc);
252596f2e892SBill Paul 		dc_stop(sc);
2526c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2527c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
2528b1d16143SMarius Strobl 		callout_drain(&sc->dc_wdog_ch);
25299ef8b520SSam Leffler 		ether_ifdetach(ifp);
2530693f4477SNate Lawson 	}
2531693f4477SNate Lawson 	if (sc->dc_miibus)
253296f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
253354f1f1d1SNate Lawson 	bus_generic_detach(dev);
253496f2e892SBill Paul 
253554f1f1d1SNate Lawson 	if (sc->dc_intrhand)
253696f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
253754f1f1d1SNate Lawson 	if (sc->dc_irq)
253896f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
253954f1f1d1SNate Lawson 	if (sc->dc_res)
254096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
254196f2e892SBill Paul 
25426a3033a8SWarner Losh 	if (ifp)
25436a3033a8SWarner Losh 		if_free(ifp);
25446a3033a8SWarner Losh 
25455f14ee23SPyun YongHyeon 	dc_dma_free(sc);
254656e5e7aeSMaxime Henrion 
254796f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
254896f2e892SBill Paul 
25495c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
25505c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
25515c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
25525c1cfac4SBill Paul 		sc->dc_mi = m;
25535c1cfac4SBill Paul 	}
25547efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
25555c1cfac4SBill Paul 
2556d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
255796f2e892SBill Paul 
255896f2e892SBill Paul 	return (0);
255996f2e892SBill Paul }
256096f2e892SBill Paul 
256196f2e892SBill Paul /*
256296f2e892SBill Paul  * Initialize the transmit descriptors.
256396f2e892SBill Paul  */
2564e3d2833aSAlfred Perlstein static int
25650934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
256696f2e892SBill Paul {
256796f2e892SBill Paul 	struct dc_chain_data *cd;
256896f2e892SBill Paul 	struct dc_list_data *ld;
256901faf54bSLuigi Rizzo 	int i, nexti;
257096f2e892SBill Paul 
257196f2e892SBill Paul 	cd = &sc->dc_cdata;
25725f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
257396f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2574b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2575b3811c95SMaxime Henrion 			nexti = 0;
2576b3811c95SMaxime Henrion 		else
2577b3811c95SMaxime Henrion 			nexti = i + 1;
257852c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_status = 0;
257952c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_ctl = 0;
258052c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_data = 0;
2581af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
258296f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
258396f2e892SBill Paul 	}
258496f2e892SBill Paul 
258596f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
258606d23883SPyun YongHyeon 	cd->dc_tx_pkts = 0;
25875f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
258856e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
258996f2e892SBill Paul 	return (0);
259096f2e892SBill Paul }
259196f2e892SBill Paul 
259296f2e892SBill Paul 
259396f2e892SBill Paul /*
259496f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
259596f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
259696f2e892SBill Paul  * points back to the first.
259796f2e892SBill Paul  */
2598e3d2833aSAlfred Perlstein static int
25990934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
260096f2e892SBill Paul {
260196f2e892SBill Paul 	struct dc_chain_data *cd;
260296f2e892SBill Paul 	struct dc_list_data *ld;
260301faf54bSLuigi Rizzo 	int i, nexti;
260496f2e892SBill Paul 
260596f2e892SBill Paul 	cd = &sc->dc_cdata;
26065f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
260796f2e892SBill Paul 
260896f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
26095f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0)
261096f2e892SBill Paul 			return (ENOBUFS);
2611b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2612b3811c95SMaxime Henrion 			nexti = 0;
2613b3811c95SMaxime Henrion 		else
2614b3811c95SMaxime Henrion 			nexti = i + 1;
2615af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
261696f2e892SBill Paul 	}
261796f2e892SBill Paul 
261896f2e892SBill Paul 	cd->dc_rx_prod = 0;
26195f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
262056e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
262196f2e892SBill Paul 	return (0);
262296f2e892SBill Paul }
262396f2e892SBill Paul 
262496f2e892SBill Paul /*
262596f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
262696f2e892SBill Paul  */
2627e3d2833aSAlfred Perlstein static int
26285f14ee23SPyun YongHyeon dc_newbuf(struct dc_softc *sc, int i)
262996f2e892SBill Paul {
26305f14ee23SPyun YongHyeon 	struct mbuf *m;
26315f14ee23SPyun YongHyeon 	bus_dmamap_t map;
263282a67a70SMarius Strobl 	bus_dma_segment_t segs[1];
263382a67a70SMarius Strobl 	int error, nseg;
263496f2e892SBill Paul 
26355f14ee23SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
26365f14ee23SPyun YongHyeon 	if (m == NULL)
263796f2e892SBill Paul 		return (ENOBUFS);
26385f14ee23SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
26395f14ee23SPyun YongHyeon 	m_adj(m, sizeof(u_int64_t));
264096f2e892SBill Paul 
264196f2e892SBill Paul 	/*
264296f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
264396f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
264496f2e892SBill Paul 	 * 82c169 chips.
264596f2e892SBill Paul 	 */
264696f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
26475f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
264896f2e892SBill Paul 
26495f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_rx_mtag, sc->dc_sparemap,
26505f14ee23SPyun YongHyeon 	    m, segs, &nseg, 0);
265156e5e7aeSMaxime Henrion 	if (error) {
26525f14ee23SPyun YongHyeon 		m_freem(m);
265356e5e7aeSMaxime Henrion 		return (error);
265456e5e7aeSMaxime Henrion 	}
26555f14ee23SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: wrong number of segments (%d)", __func__,
26565f14ee23SPyun YongHyeon 	    nseg));
26575f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_rx_chain[i] != NULL)
26585f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i]);
265996f2e892SBill Paul 
26605f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_rx_map[i];
26615f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
26625f14ee23SPyun YongHyeon 	sc->dc_sparemap = map;
26635f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_chain[i] = m;
26645f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
266556e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
26665f14ee23SPyun YongHyeon 
26675f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
26685f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_data =
26695f14ee23SPyun YongHyeon 	    htole32(DC_ADDR_LO(segs[0].ds_addr));
26705f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
26715f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
267256e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
267396f2e892SBill Paul 	return (0);
267496f2e892SBill Paul }
267596f2e892SBill Paul 
267696f2e892SBill Paul /*
267796f2e892SBill Paul  * Grrrrr.
267896f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
267996f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
268096f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
268196f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
268296f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
268396f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
268496f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
268596f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
268696f2e892SBill Paul  *
268796f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
268896f2e892SBill Paul  * Here's what we know:
268996f2e892SBill Paul  *
269096f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
269196f2e892SBill Paul  *   descriptors uploaded.
269296f2e892SBill Paul  *
269396f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
269496f2e892SBill Paul  *   total data upload.
269596f2e892SBill Paul  *
269696f2e892SBill Paul  * - We know the size of the desired received frame because it will be
269796f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
269896f2e892SBill Paul  *
269996f2e892SBill Paul  * Here's what we do:
270096f2e892SBill Paul  *
270196f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
270296f2e892SBill Paul  *   This means that we know that the buffer contents should be all
270396f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
270496f2e892SBill Paul  *
270596f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
270696f2e892SBill Paul  *   ethernet CRC at the end.
270796f2e892SBill Paul  *
270896f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
270996f2e892SBill Paul  *
271096f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
271196f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
271296f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
271396f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
271496f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
271596f2e892SBill Paul  *   we won't be fooled.
271696f2e892SBill Paul  *
271796f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
271896f2e892SBill Paul  *   that value from the current pointer location. This brings us
271996f2e892SBill Paul  *   to the start of the actual received packet.
272096f2e892SBill Paul  *
272196f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
272296f2e892SBill Paul  *   frame length.
272396f2e892SBill Paul  *
272496f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
272596f2e892SBill Paul  * the time.
272696f2e892SBill Paul  */
272796f2e892SBill Paul 
272896f2e892SBill Paul #define	DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2729e3d2833aSAlfred Perlstein static void
27300934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
273196f2e892SBill Paul {
273296f2e892SBill Paul 	struct dc_desc *cur_rx;
273396f2e892SBill Paul 	struct dc_desc *c = NULL;
273496f2e892SBill Paul 	struct mbuf *m = NULL;
273596f2e892SBill Paul 	unsigned char *ptr;
273696f2e892SBill Paul 	int i, total_len;
2737ee320f98SPyun YongHyeon 	uint32_t rxstat = 0;
273896f2e892SBill Paul 
273996f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
27405f14ee23SPyun YongHyeon 	cur_rx = &sc->dc_ldata.dc_rx_list[idx];
274196f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
27421edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
274396f2e892SBill Paul 
274496f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
274596f2e892SBill Paul 	while (1) {
27465f14ee23SPyun YongHyeon 		c = &sc->dc_ldata.dc_rx_list[i];
2747af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
274896f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
274996f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
275096f2e892SBill Paul 		ptr += DC_RXLEN;
275196f2e892SBill Paul 		/* If this is the last buffer, break out. */
275296f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
275396f2e892SBill Paul 			break;
27545f14ee23SPyun YongHyeon 		dc_discard_rxbuf(sc, i);
275596f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
275696f2e892SBill Paul 	}
275796f2e892SBill Paul 
275896f2e892SBill Paul 	/* Find the length of the actual receive frame. */
275996f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
276096f2e892SBill Paul 
276196f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
276296f2e892SBill Paul 	while (*ptr == 0x00)
276396f2e892SBill Paul 		ptr--;
276496f2e892SBill Paul 
276596f2e892SBill Paul 	/* Round off. */
276696f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
276796f2e892SBill Paul 		ptr -= 1;
276896f2e892SBill Paul 
276996f2e892SBill Paul 	/* Now find the start of the frame. */
277096f2e892SBill Paul 	ptr -= total_len;
277196f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
277296f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
277396f2e892SBill Paul 
277496f2e892SBill Paul 	/*
277596f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
277696f2e892SBill Paul 	 * the status word to make it look like a successful
277796f2e892SBill Paul 	 * frame reception.
277896f2e892SBill Paul 	 */
277996f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2780af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
278196f2e892SBill Paul }
278296f2e892SBill Paul 
278396f2e892SBill Paul /*
278473bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
278573bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
278673bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
278773bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
278873bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
278973bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
279073bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
279173bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
279273bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
279373bf949cSBill Paul  */
2794e3d2833aSAlfred Perlstein static int
27950934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
279673bf949cSBill Paul {
279773bf949cSBill Paul 	struct dc_desc *cur_rx;
27980934f18aSMaxime Henrion 	int i, pos;
279973bf949cSBill Paul 
280073bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
280173bf949cSBill Paul 
280273bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
28035f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[pos];
2804af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
280573bf949cSBill Paul 			break;
280673bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
280773bf949cSBill Paul 	}
280873bf949cSBill Paul 
280973bf949cSBill Paul 	/* If the ring really is empty, then just return. */
281073bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
281173bf949cSBill Paul 		return (0);
281273bf949cSBill Paul 
281373bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
281473bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
281573bf949cSBill Paul 
281673bf949cSBill Paul 	return (EAGAIN);
281773bf949cSBill Paul }
281873bf949cSBill Paul 
28195f14ee23SPyun YongHyeon static void
28205f14ee23SPyun YongHyeon dc_discard_rxbuf(struct dc_softc *sc, int i)
28215f14ee23SPyun YongHyeon {
28225f14ee23SPyun YongHyeon 	struct mbuf *m;
28235f14ee23SPyun YongHyeon 
28245f14ee23SPyun YongHyeon 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
28255f14ee23SPyun YongHyeon 		m = sc->dc_cdata.dc_rx_chain[i];
28265f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
28275f14ee23SPyun YongHyeon 	}
28285f14ee23SPyun YongHyeon 
28295f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
28305f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
28315f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_PREREAD |
28325f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
28335f14ee23SPyun YongHyeon }
28345f14ee23SPyun YongHyeon 
283573bf949cSBill Paul /*
283696f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
283796f2e892SBill Paul  * the higher level protocols.
283896f2e892SBill Paul  */
28391abcdbd1SAttilio Rao static int
28400934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
284196f2e892SBill Paul {
28425f14ee23SPyun YongHyeon 	struct mbuf *m;
284396f2e892SBill Paul 	struct ifnet *ifp;
284496f2e892SBill Paul 	struct dc_desc *cur_rx;
28451abcdbd1SAttilio Rao 	int i, total_len, rx_npkts;
2846ee320f98SPyun YongHyeon 	uint32_t rxstat;
284796f2e892SBill Paul 
28485120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
28495120abbfSSam Leffler 
2850fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
28511abcdbd1SAttilio Rao 	rx_npkts = 0;
285296f2e892SBill Paul 
28535f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_POSTREAD |
28545f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
28555f14ee23SPyun YongHyeon 	for (i = sc->dc_cdata.dc_rx_prod;
28565f14ee23SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
28575f14ee23SPyun YongHyeon 	    DC_INC(i, DC_RX_LIST_CNT)) {
2858e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
285940929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2860e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2861e4fc250cSLuigi Rizzo 				break;
2862e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2863e4fc250cSLuigi Rizzo 		}
28640934f18aSMaxime Henrion #endif
28655f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[i];
2866af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
28675f14ee23SPyun YongHyeon 		if ((rxstat & DC_RXSTAT_OWN) != 0)
28685f14ee23SPyun YongHyeon 			break;
286996f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
28705f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
287156e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
287296f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
2873848a02fcSPyun YongHyeon 		rx_npkts++;
287496f2e892SBill Paul 
287596f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
287696f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
287796f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
287896f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
28795f14ee23SPyun YongHyeon 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0)
288096f2e892SBill Paul 					continue;
288196f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2882af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
288396f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
288496f2e892SBill Paul 			}
288596f2e892SBill Paul 		}
288696f2e892SBill Paul 
288796f2e892SBill Paul 		/*
288896f2e892SBill Paul 		 * If an error occurs, update stats, clear the
288996f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
289096f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2891db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
28920934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
289396f2e892SBill Paul 		 */
2894db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2895db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2896db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2897db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2898db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
289996f2e892SBill Paul 				ifp->if_ierrors++;
290096f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
290196f2e892SBill Paul 					ifp->if_collisions++;
29025f14ee23SPyun YongHyeon 				dc_discard_rxbuf(sc, i);
29035f14ee23SPyun YongHyeon 				if (rxstat & DC_RXSTAT_CRCERR)
290496f2e892SBill Paul 					continue;
29055f14ee23SPyun YongHyeon 				else {
29068f382a1fSPyun YongHyeon 					ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2907c8b27acaSJohn Baldwin 					dc_init_locked(sc);
29081abcdbd1SAttilio Rao 					return (rx_npkts);
290996f2e892SBill Paul 				}
291096f2e892SBill Paul 			}
2911db40c1aeSDoug Ambrisko 		}
291296f2e892SBill Paul 
291396f2e892SBill Paul 		/* No errors; receive the packet. */
291496f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2915432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
291601faf54bSLuigi Rizzo 		/*
2917432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
291801faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
291901faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
292001faf54bSLuigi Rizzo 		 * copy done in m_devget().
292101faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
292201faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
292301faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
292401faf54bSLuigi Rizzo 		 */
29255f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0) {
29265f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
29275f14ee23SPyun YongHyeon 			ifp->if_iqdrops++;
29285f14ee23SPyun YongHyeon 			continue;
29295f14ee23SPyun YongHyeon 		}
293001faf54bSLuigi Rizzo 		m->m_pkthdr.rcvif = ifp;
293101faf54bSLuigi Rizzo 		m->m_pkthdr.len = m->m_len = total_len;
29325f14ee23SPyun YongHyeon #else
293301faf54bSLuigi Rizzo 		{
29345f14ee23SPyun YongHyeon 			struct mbuf *m0;
29355f14ee23SPyun YongHyeon 
293601faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
293701faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
29385f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
293996f2e892SBill Paul 			if (m0 == NULL) {
29405f14ee23SPyun YongHyeon 				ifp->if_iqdrops++;
294196f2e892SBill Paul 				continue;
294296f2e892SBill Paul 			}
294396f2e892SBill Paul 			m = m0;
294401faf54bSLuigi Rizzo 		}
29455f14ee23SPyun YongHyeon #endif
294696f2e892SBill Paul 
294796f2e892SBill Paul 		ifp->if_ipackets++;
29485120abbfSSam Leffler 		DC_UNLOCK(sc);
29499ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
29505120abbfSSam Leffler 		DC_LOCK(sc);
295196f2e892SBill Paul 	}
295296f2e892SBill Paul 
295396f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
29541abcdbd1SAttilio Rao 	return (rx_npkts);
295596f2e892SBill Paul }
295696f2e892SBill Paul 
295796f2e892SBill Paul /*
295896f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
295996f2e892SBill Paul  * the list buffers.
296096f2e892SBill Paul  */
2961e3d2833aSAlfred Perlstein static void
29620934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
296396f2e892SBill Paul {
29645f14ee23SPyun YongHyeon 	struct dc_desc *cur_tx;
296596f2e892SBill Paul 	struct ifnet *ifp;
29665f14ee23SPyun YongHyeon 	int idx, setup;
2967ee320f98SPyun YongHyeon 	uint32_t ctl, txstat;
296896f2e892SBill Paul 
296906d23883SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt == 0)
297006d23883SPyun YongHyeon 		return;
297106d23883SPyun YongHyeon 
2972fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
297396f2e892SBill Paul 
297496f2e892SBill Paul 	/*
297596f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
297696f2e892SBill Paul 	 * frames that have been transmitted.
297796f2e892SBill Paul 	 */
2978cb94db27SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_POSTREAD |
29795f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
29805f14ee23SPyun YongHyeon 	setup = 0;
29815f14ee23SPyun YongHyeon 	for (idx = sc->dc_cdata.dc_tx_cons; idx != sc->dc_cdata.dc_tx_prod;
29825f14ee23SPyun YongHyeon 	    DC_INC(idx, DC_TX_LIST_CNT), sc->dc_cdata.dc_tx_cnt--) {
29835f14ee23SPyun YongHyeon 		cur_tx = &sc->dc_ldata.dc_tx_list[idx];
2984af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
2985af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
298696f2e892SBill Paul 
298796f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
298896f2e892SBill Paul 			break;
298996f2e892SBill Paul 
29905f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_chain[idx] == NULL)
29915f14ee23SPyun YongHyeon 			continue;
29925f14ee23SPyun YongHyeon 
2993af4358c7SMaxime Henrion 		if (ctl & DC_TXCTL_SETUP) {
29945f14ee23SPyun YongHyeon 			cur_tx->dc_ctl = htole32(ctl & ~DC_TXCTL_SETUP);
29955f14ee23SPyun YongHyeon 			setup++;
29965f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
29975f14ee23SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
299896f2e892SBill Paul 			/*
299996f2e892SBill Paul 			 * Yes, the PNIC is so brain damaged
300096f2e892SBill Paul 			 * that it will sometimes generate a TX
300196f2e892SBill Paul 			 * underrun error while DMAing the RX
300296f2e892SBill Paul 			 * filter setup frame. If we detect this,
300396f2e892SBill Paul 			 * we have to send the setup frame again,
300496f2e892SBill Paul 			 * or else the filter won't be programmed
300596f2e892SBill Paul 			 * correctly.
300696f2e892SBill Paul 			 */
300796f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
300896f2e892SBill Paul 				if (txstat & DC_TXSTAT_ERRSUM)
300996f2e892SBill Paul 					dc_setfilt(sc);
301096f2e892SBill Paul 			}
301196f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
301296f2e892SBill Paul 			continue;
301396f2e892SBill Paul 		}
301496f2e892SBill Paul 
301529a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
3016feb78939SJonathan Chen 			/*
3017feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
3018feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
301929a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
302029a2220aSBill Paul 			 * Who knows, but Conexant chips have the
302129a2220aSBill Paul 			 * same problem. Maybe they took lessons
302229a2220aSBill Paul 			 * from Xircom.
302329a2220aSBill Paul 			 */
3024feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
3025feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
3026feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
3027feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
3028feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
3029feb78939SJonathan Chen 		} else {
303096f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
303196f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
303296f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
303396f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
303496f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
3035feb78939SJonathan Chen 		}
303696f2e892SBill Paul 
303796f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
303896f2e892SBill Paul 			ifp->if_oerrors++;
303996f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
304096f2e892SBill Paul 				ifp->if_collisions++;
304196f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
304296f2e892SBill Paul 				ifp->if_collisions++;
304396f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
30448f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3045c8b27acaSJohn Baldwin 				dc_init_locked(sc);
304696f2e892SBill Paul 				return;
304796f2e892SBill Paul 			}
304852c43a47SPyun YongHyeon 		} else
304952c43a47SPyun YongHyeon 			ifp->if_opackets++;
305096f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
305196f2e892SBill Paul 
30525f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
305356e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
30545f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
305596f2e892SBill Paul 		m_freem(sc->dc_cdata.dc_tx_chain[idx]);
305696f2e892SBill Paul 		sc->dc_cdata.dc_tx_chain[idx] = NULL;
305796f2e892SBill Paul 	}
305896f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
305982a67a70SMarius Strobl 
30605f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
306113f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
30623e0e6726SMarius Strobl 		if (sc->dc_cdata.dc_tx_cnt == 0)
30633e0e6726SMarius Strobl 			sc->dc_wdog_timer = 0;
306496f2e892SBill Paul 	}
30655f14ee23SPyun YongHyeon 	if (setup > 0)
30665f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
30675f14ee23SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
30685f14ee23SPyun YongHyeon }
306996f2e892SBill Paul 
3070e3d2833aSAlfred Perlstein static void
30710934f18aSMaxime Henrion dc_tick(void *xsc)
307296f2e892SBill Paul {
307396f2e892SBill Paul 	struct dc_softc *sc;
307496f2e892SBill Paul 	struct mii_data *mii;
307596f2e892SBill Paul 	struct ifnet *ifp;
3076ee320f98SPyun YongHyeon 	uint32_t r;
307796f2e892SBill Paul 
307896f2e892SBill Paul 	sc = xsc;
3079c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3080fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
308196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
308296f2e892SBill Paul 
308306d23883SPyun YongHyeon 	/*
308406d23883SPyun YongHyeon 	 * Reclaim transmitted frames for controllers that do
308506d23883SPyun YongHyeon 	 * not generate TX completion interrupt for every frame.
308606d23883SPyun YongHyeon 	 */
308706d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR)
308806d23883SPyun YongHyeon 		dc_txeof(sc);
308906d23883SPyun YongHyeon 
309096f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
3091318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
3092318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
3093318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3094318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
309596f2e892SBill Paul 				sc->dc_link = 0;
3096318b02fdSBill Paul 				mii_mediachg(mii);
3097318b02fdSBill Paul 			}
3098318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3099318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
3100318b02fdSBill Paul 				sc->dc_link = 0;
3101318b02fdSBill Paul 				mii_mediachg(mii);
3102318b02fdSBill Paul 			}
3103d675147eSBill Paul 			if (sc->dc_link == 0)
310496f2e892SBill Paul 				mii_tick(mii);
310596f2e892SBill Paul 		} else {
3106d0d67284SMarius Strobl 			/*
3107d0d67284SMarius Strobl 			 * For NICs which never report DC_RXSTATE_WAIT, we
3108d0d67284SMarius Strobl 			 * have to bite the bullet...
3109d0d67284SMarius Strobl 			 */
3110d0d67284SMarius Strobl 			if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
3111d0d67284SMarius Strobl 			    DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
3112d314ebf5SPyun YongHyeon 			    sc->dc_cdata.dc_tx_cnt == 0)
311396f2e892SBill Paul 				mii_tick(mii);
3114259b8d84SMartin Blapp 		}
311596f2e892SBill Paul 	} else
311696f2e892SBill Paul 		mii_tick(mii);
311796f2e892SBill Paul 
311896f2e892SBill Paul 	/*
311996f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
312096f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
312196f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
312296f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
312396f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
312496f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
312596f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
312696f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
312796f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
312896f2e892SBill Paul 	 * a screeching halt for several seconds.
312996f2e892SBill Paul 	 *
313096f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
313196f2e892SBill Paul 	 * any packets until a link has been established. After the
313296f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
313396f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
313496f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
313596f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
313696f2e892SBill Paul 	 */
3137d314ebf5SPyun YongHyeon 	if (sc->dc_link != 0 && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3138c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
313996f2e892SBill Paul 
3140318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
3141b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3142318b02fdSBill Paul 	else
3143b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
314496f2e892SBill Paul }
314596f2e892SBill Paul 
3146d467c136SBill Paul /*
3147d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
3148d467c136SBill Paul  * or switch to store and forward mode if we have to.
3149d467c136SBill Paul  */
3150e3d2833aSAlfred Perlstein static void
31510934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
3152d467c136SBill Paul {
3153d9efae03SPyun YongHyeon 	uint32_t netcfg, isr;
3154d9efae03SPyun YongHyeon 	int i, reinit;
3155d467c136SBill Paul 
3156d9efae03SPyun YongHyeon 	reinit = 0;
3157d9efae03SPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
3158d9efae03SPyun YongHyeon 	device_printf(sc->dc_dev, "TX underrun -- ");
3159d9efae03SPyun YongHyeon 	if ((sc->dc_flags & DC_TX_STORENFWD) == 0) {
3160d9efae03SPyun YongHyeon 		if (sc->dc_txthresh + DC_TXTHRESH_INC > DC_TXTHRESH_MAX) {
3161d9efae03SPyun YongHyeon 			printf("using store and forward mode\n");
3162d9efae03SPyun YongHyeon 			netcfg |= DC_NETCFG_STORENFWD;
3163d9efae03SPyun YongHyeon 		} else {
3164d9efae03SPyun YongHyeon 			printf("increasing TX threshold\n");
3165d9efae03SPyun YongHyeon 			sc->dc_txthresh += DC_TXTHRESH_INC;
3166d9efae03SPyun YongHyeon 			netcfg &= ~DC_NETCFG_TX_THRESH;
3167d9efae03SPyun YongHyeon 			netcfg |= sc->dc_txthresh;
31688f382a1fSPyun YongHyeon 		}
3169d467c136SBill Paul 
3170d467c136SBill Paul 		if (DC_IS_INTEL(sc)) {
3171d467c136SBill Paul 			/*
3172d467c136SBill Paul 			 * The real 21143 requires that the transmitter be idle
3173d467c136SBill Paul 			 * in order to change the transmit threshold or store
3174d467c136SBill Paul 			 * and forward state.
3175d467c136SBill Paul 			 */
3176d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg & ~DC_NETCFG_TX_ON);
3177d467c136SBill Paul 
3178d467c136SBill Paul 			for (i = 0; i < DC_TIMEOUT; i++) {
3179d467c136SBill Paul 				isr = CSR_READ_4(sc, DC_ISR);
3180d467c136SBill Paul 				if (isr & DC_ISR_TX_IDLE)
3181d467c136SBill Paul 					break;
3182d467c136SBill Paul 				DELAY(10);
3183d467c136SBill Paul 			}
3184d467c136SBill Paul 			if (i == DC_TIMEOUT) {
31856b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
3186432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
3187432120f2SMarius Strobl 				    __func__);
3188d9efae03SPyun YongHyeon 				reinit++;
3189d9efae03SPyun YongHyeon 			}
3190d9efae03SPyun YongHyeon 		}
3191d9efae03SPyun YongHyeon 	} else {
3192d9efae03SPyun YongHyeon 		printf("resetting\n");
3193d9efae03SPyun YongHyeon 		reinit++;
3194d9efae03SPyun YongHyeon 	}
3195d9efae03SPyun YongHyeon 
3196d9efae03SPyun YongHyeon 	if (reinit == 0) {
3197d9efae03SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, netcfg);
3198d9efae03SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3199d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg | DC_NETCFG_TX_ON);
3200d9efae03SPyun YongHyeon 	} else {
32018f382a1fSPyun YongHyeon 		sc->dc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3202c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3203d467c136SBill Paul 	}
3204d467c136SBill Paul }
3205d467c136SBill Paul 
3206e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3207e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3208e4fc250cSLuigi Rizzo 
32091abcdbd1SAttilio Rao static int
3210e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3211e4fc250cSLuigi Rizzo {
3212e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
32131abcdbd1SAttilio Rao 	int rx_npkts = 0;
3214e4fc250cSLuigi Rizzo 
321540929967SGleb Smirnoff 	DC_LOCK(sc);
321640929967SGleb Smirnoff 
321740929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
321840929967SGleb Smirnoff 		DC_UNLOCK(sc);
32191abcdbd1SAttilio Rao 		return (rx_npkts);
3220e4fc250cSLuigi Rizzo 	}
322140929967SGleb Smirnoff 
3222e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
32231abcdbd1SAttilio Rao 	rx_npkts = dc_rxeof(sc);
3224e4fc250cSLuigi Rizzo 	dc_txeof(sc);
322513f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
322613f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3227c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3228e4fc250cSLuigi Rizzo 
3229e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3230ee320f98SPyun YongHyeon 		uint32_t	status;
3231e4fc250cSLuigi Rizzo 
3232e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3233e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3234e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3235e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
32365120abbfSSam Leffler 		if (!status) {
32375120abbfSSam Leffler 			DC_UNLOCK(sc);
32381abcdbd1SAttilio Rao 			return (rx_npkts);
32395120abbfSSam Leffler 		}
3240e4fc250cSLuigi Rizzo 		/* ack what we have */
3241e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3242e4fc250cSLuigi Rizzo 
3243e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3244ee320f98SPyun YongHyeon 			uint32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3245e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3246e4fc250cSLuigi Rizzo 
3247e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3248e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3249e4fc250cSLuigi Rizzo 		}
3250e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3251e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3252e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3253e4fc250cSLuigi Rizzo 
3254e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3255e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3256e4fc250cSLuigi Rizzo 
3257e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
32586b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
32598f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3260c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3261e4fc250cSLuigi Rizzo 		}
3262e4fc250cSLuigi Rizzo 	}
32635120abbfSSam Leffler 	DC_UNLOCK(sc);
32641abcdbd1SAttilio Rao 	return (rx_npkts);
3265e4fc250cSLuigi Rizzo }
3266e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3267e4fc250cSLuigi Rizzo 
3268e3d2833aSAlfred Perlstein static void
32690934f18aSMaxime Henrion dc_intr(void *arg)
327096f2e892SBill Paul {
327196f2e892SBill Paul 	struct dc_softc *sc;
327296f2e892SBill Paul 	struct ifnet *ifp;
3273ee320f98SPyun YongHyeon 	uint32_t r, status;
3274848a02fcSPyun YongHyeon 	int n;
327596f2e892SBill Paul 
327696f2e892SBill Paul 	sc = arg;
3277d2a1864bSWarner Losh 
32780934f18aSMaxime Henrion 	if (sc->suspended)
3279e8388e14SMitsuru IWASAKI 		return;
3280e8388e14SMitsuru IWASAKI 
3281d1ce9105SBill Paul 	DC_LOCK(sc);
3282a84b4e80SPyun YongHyeon 	status = CSR_READ_4(sc, DC_ISR);
3283a84b4e80SPyun YongHyeon 	if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0) {
3284a84b4e80SPyun YongHyeon 		DC_UNLOCK(sc);
3285a84b4e80SPyun YongHyeon 		return;
3286a84b4e80SPyun YongHyeon 	}
3287fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3288e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
328940929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
329040929967SGleb Smirnoff 		DC_UNLOCK(sc);
329140929967SGleb Smirnoff 		return;
3292e4fc250cSLuigi Rizzo 	}
32930934f18aSMaxime Henrion #endif
329496f2e892SBill Paul 	/* Disable interrupts. */
329596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
329696f2e892SBill Paul 
3297a84b4e80SPyun YongHyeon 	for (n = 16; n > 0; n--) {
3298a84b4e80SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3299a84b4e80SPyun YongHyeon 			break;
3300a84b4e80SPyun YongHyeon 		/* Ack interrupts. */
330196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
330296f2e892SBill Paul 
330373bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
3304848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
330573bf949cSBill Paul 				while (dc_rx_resync(sc))
330673bf949cSBill Paul 					dc_rxeof(sc);
330773bf949cSBill Paul 			}
330873bf949cSBill Paul 		}
330996f2e892SBill Paul 
331096f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
331196f2e892SBill Paul 			dc_txeof(sc);
331296f2e892SBill Paul 
331396f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
331496f2e892SBill Paul 			dc_txeof(sc);
331596f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
331696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
331796f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
331896f2e892SBill Paul 			}
331996f2e892SBill Paul 		}
332096f2e892SBill Paul 
3321d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3322d467c136SBill Paul 			dc_tx_underrun(sc);
332396f2e892SBill Paul 
332496f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
332573bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
332626b40a65SPyun YongHyeon 			r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
332726b40a65SPyun YongHyeon 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3328848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
332973bf949cSBill Paul 				while (dc_rx_resync(sc))
333073bf949cSBill Paul 					dc_rxeof(sc);
333173bf949cSBill Paul 			}
333273bf949cSBill Paul 		}
333396f2e892SBill Paul 
3334a84b4e80SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3335a84b4e80SPyun YongHyeon 			dc_start_locked(ifp);
3336a84b4e80SPyun YongHyeon 
333796f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
33388f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3339c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3340a84b4e80SPyun YongHyeon 			DC_UNLOCK(sc);
3341a84b4e80SPyun YongHyeon 			return;
334296f2e892SBill Paul 		}
3343a84b4e80SPyun YongHyeon 		status = CSR_READ_4(sc, DC_ISR);
3344a84b4e80SPyun YongHyeon 		if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0)
3345a84b4e80SPyun YongHyeon 			break;
334696f2e892SBill Paul 	}
334796f2e892SBill Paul 
334896f2e892SBill Paul 	/* Re-enable interrupts. */
3349a84b4e80SPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
335096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
335196f2e892SBill Paul 
3352d1ce9105SBill Paul 	DC_UNLOCK(sc);
335396f2e892SBill Paul }
335496f2e892SBill Paul 
335596f2e892SBill Paul /*
335696f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
335796f2e892SBill Paul  * pointers to the fragment pointers.
335896f2e892SBill Paul  */
3359e3d2833aSAlfred Perlstein static int
3360a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
336196f2e892SBill Paul {
3362ebc284ccSMarius Strobl 	bus_dma_segment_t segs[DC_MAXFRAGS];
33635f14ee23SPyun YongHyeon 	bus_dmamap_t map;
3364ebc284ccSMarius Strobl 	struct dc_desc *f;
336596f2e892SBill Paul 	struct mbuf *m;
3366993a741aSMarius Strobl 	int cur, defragged, error, first, frag, i, idx, nseg;
3367cda97c50SMike Silbersack 
3368993a741aSMarius Strobl 	m = NULL;
3369993a741aSMarius Strobl 	defragged = 0;
3370993a741aSMarius Strobl 	if (sc->dc_flags & DC_TX_COALESCE &&
3371993a741aSMarius Strobl 	    ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3372993a741aSMarius Strobl 		m = m_defrag(*m_head, M_DONTWAIT);
3373993a741aSMarius Strobl 		defragged = 1;
3374993a741aSMarius Strobl 	} else {
3375cda97c50SMike Silbersack 		/*
3376993a741aSMarius Strobl 		 * Count the number of frags in this chain to see if we
3377993a741aSMarius Strobl 		 * need to m_collapse.  Since the descriptor list is shared
3378993a741aSMarius Strobl 		 * by all packets, we'll m_collapse long chains so that they
3379cda97c50SMike Silbersack 		 * do not use up the entire list, even if they would fit.
3380cda97c50SMike Silbersack 		 */
3381993a741aSMarius Strobl 		i = 0;
3382a10c0e45SMike Silbersack 		for (m = *m_head; m != NULL; m = m->m_next)
3383993a741aSMarius Strobl 			i++;
3384993a741aSMarius Strobl 		if (i > DC_TX_LIST_CNT / 4 ||
3385993a741aSMarius Strobl 		    DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3386993a741aSMarius Strobl 		    DC_TX_LIST_RSVD) {
3387993a741aSMarius Strobl 			m = m_collapse(*m_head, M_DONTWAIT, DC_MAXFRAGS);
3388993a741aSMarius Strobl 			defragged = 1;
3389993a741aSMarius Strobl 		}
3390993a741aSMarius Strobl 	}
3391993a741aSMarius Strobl 	if (defragged != 0) {
339282a67a70SMarius Strobl 		if (m == NULL) {
339382a67a70SMarius Strobl 			m_freem(*m_head);
339482a67a70SMarius Strobl 			*m_head = NULL;
3395cda97c50SMike Silbersack 			return (ENOBUFS);
339682a67a70SMarius Strobl 		}
3397a10c0e45SMike Silbersack 		*m_head = m;
3398cda97c50SMike Silbersack 	}
3399993a741aSMarius Strobl 
340056e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
34015f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3402ebc284ccSMarius Strobl 	    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3403ebc284ccSMarius Strobl 	if (error == EFBIG) {
3404993a741aSMarius Strobl 		if (defragged != 0 || (m = m_collapse(*m_head, M_DONTWAIT,
3405993a741aSMarius Strobl 		    DC_MAXFRAGS)) == NULL) {
3406ebc284ccSMarius Strobl 			m_freem(*m_head);
340782a67a70SMarius Strobl 			*m_head = NULL;
3408993a741aSMarius Strobl 			return (defragged != 0 ? error : ENOBUFS);
340982a67a70SMarius Strobl 		}
3410ebc284ccSMarius Strobl 		*m_head = m;
34115f14ee23SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3412ebc284ccSMarius Strobl 		    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3413ebc284ccSMarius Strobl 		if (error != 0) {
3414ebc284ccSMarius Strobl 			m_freem(*m_head);
3415ebc284ccSMarius Strobl 			*m_head = NULL;
3416ebc284ccSMarius Strobl 			return (error);
341782a67a70SMarius Strobl 		}
3418ebc284ccSMarius Strobl 	} else if (error != 0)
3419ebc284ccSMarius Strobl 		return (error);
3420ebc284ccSMarius Strobl 	KASSERT(nseg <= DC_MAXFRAGS,
3421ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
3422ebc284ccSMarius Strobl 	if (nseg == 0) {
3423ebc284ccSMarius Strobl 		m_freem(*m_head);
3424ebc284ccSMarius Strobl 		*m_head = NULL;
3425ebc284ccSMarius Strobl 		return (EIO);
3426ebc284ccSMarius Strobl 	}
3427ebc284ccSMarius Strobl 
34285f14ee23SPyun YongHyeon 	/* Check descriptor overruns. */
34295f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt + nseg > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
34305f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
34315f14ee23SPyun YongHyeon 		return (ENOBUFS);
34325f14ee23SPyun YongHyeon 	}
34335f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
34345f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
34355f14ee23SPyun YongHyeon 
3436ebc284ccSMarius Strobl 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3437ebc284ccSMarius Strobl 	for (i = 0; i < nseg; i++) {
3438ebc284ccSMarius Strobl 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3439ebc284ccSMarius Strobl 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3440ebc284ccSMarius Strobl 		    (first != sc->dc_cdata.dc_tx_first)) {
34415f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_mtag,
3442ebc284ccSMarius Strobl 			    sc->dc_cdata.dc_tx_map[first]);
3443ebc284ccSMarius Strobl 			m_freem(*m_head);
3444ebc284ccSMarius Strobl 			*m_head = NULL;
3445ebc284ccSMarius Strobl 			return (ENOBUFS);
3446ebc284ccSMarius Strobl 		}
3447ebc284ccSMarius Strobl 
34485f14ee23SPyun YongHyeon 		f = &sc->dc_ldata.dc_tx_list[frag];
3449ebc284ccSMarius Strobl 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3450ebc284ccSMarius Strobl 		if (i == 0) {
3451ebc284ccSMarius Strobl 			f->dc_status = 0;
3452ebc284ccSMarius Strobl 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3453ebc284ccSMarius Strobl 		} else
3454ebc284ccSMarius Strobl 			f->dc_status = htole32(DC_TXSTAT_OWN);
34555f14ee23SPyun YongHyeon 		f->dc_data = htole32(DC_ADDR_LO(segs[i].ds_addr));
3456ebc284ccSMarius Strobl 		cur = frag;
3457ebc284ccSMarius Strobl 		DC_INC(frag, DC_TX_LIST_CNT);
3458ebc284ccSMarius Strobl 	}
3459ebc284ccSMarius Strobl 
3460ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_prod = frag;
3461ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_cnt += nseg;
3462ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_chain[cur] = *m_head;
34635f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3464ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
34655f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[first].dc_ctl |=
3466ebc284ccSMarius Strobl 		    htole32(DC_TXCTL_FINT);
3467ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
34685f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
346906d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR &&
347006d23883SPyun YongHyeon 	    ++sc->dc_cdata.dc_tx_pkts >= 8) {
347106d23883SPyun YongHyeon 		sc->dc_cdata.dc_tx_pkts = 0;
34725f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
347306d23883SPyun YongHyeon 	}
34745f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3475ebc284ccSMarius Strobl 
34765f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
34775f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
34785f14ee23SPyun YongHyeon 
34795f14ee23SPyun YongHyeon 	/*
34805f14ee23SPyun YongHyeon 	 * Swap the last and the first dmamaps to ensure the map for
34815f14ee23SPyun YongHyeon 	 * this transmission is placed at the last descriptor.
34825f14ee23SPyun YongHyeon 	 */
34835f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_tx_map[cur];
34845f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[cur] = sc->dc_cdata.dc_tx_map[first];
34855f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[first] = map;
34865f14ee23SPyun YongHyeon 
348796f2e892SBill Paul 	return (0);
348896f2e892SBill Paul }
348996f2e892SBill Paul 
3490e3d2833aSAlfred Perlstein static void
34910934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
349296f2e892SBill Paul {
349396f2e892SBill Paul 	struct dc_softc *sc;
3494c8b27acaSJohn Baldwin 
3495c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3496c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3497c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3498c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3499c8b27acaSJohn Baldwin }
3500c8b27acaSJohn Baldwin 
3501ebc284ccSMarius Strobl /*
3502ebc284ccSMarius Strobl  * Main transmit routine
3503ebc284ccSMarius Strobl  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3504ebc284ccSMarius Strobl  * regions directly in the transmit lists.  We also save a copy of the
3505ebc284ccSMarius Strobl  * pointers since the transmit list fragment pointers are physical
3506ebc284ccSMarius Strobl  * addresses.
3507ebc284ccSMarius Strobl  */
3508c8b27acaSJohn Baldwin static void
3509c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3510c8b27acaSJohn Baldwin {
3511c8b27acaSJohn Baldwin 	struct dc_softc *sc;
35125f14ee23SPyun YongHyeon 	struct mbuf *m_head;
35135f14ee23SPyun YongHyeon 	int queued;
351496f2e892SBill Paul 
351596f2e892SBill Paul 	sc = ifp->if_softc;
351696f2e892SBill Paul 
3517c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
351896f2e892SBill Paul 
351976d40c85SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
352076d40c85SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->dc_link == 0)
3521d1ce9105SBill Paul 		return;
352296f2e892SBill Paul 
35235f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
352496f2e892SBill Paul 
35255f14ee23SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
35265f14ee23SPyun YongHyeon 		/*
35275f14ee23SPyun YongHyeon 		 * If there's no way we can send any packets, return now.
35285f14ee23SPyun YongHyeon 		 */
35295f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
35305f14ee23SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
35315f14ee23SPyun YongHyeon 			break;
35325f14ee23SPyun YongHyeon 		}
3533cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
353496f2e892SBill Paul 		if (m_head == NULL)
353596f2e892SBill Paul 			break;
353696f2e892SBill Paul 
3537a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
353882a67a70SMarius Strobl 			if (m_head == NULL)
353982a67a70SMarius Strobl 				break;
3540cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
354113f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
354296f2e892SBill Paul 			break;
354396f2e892SBill Paul 		}
354496f2e892SBill Paul 
3545cbaf877fSBrian Feldman 		queued++;
354696f2e892SBill Paul 		/*
354796f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
354896f2e892SBill Paul 		 * to him.
354996f2e892SBill Paul 		 */
35509ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
355196f2e892SBill Paul 	}
355296f2e892SBill Paul 
3553cbaf877fSBrian Feldman 	if (queued > 0) {
355496f2e892SBill Paul 		/* Transmit */
355596f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
355696f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
355796f2e892SBill Paul 
355896f2e892SBill Paul 		/*
355996f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
356096f2e892SBill Paul 		 */
3561b1d16143SMarius Strobl 		sc->dc_wdog_timer = 5;
3562cbaf877fSBrian Feldman 	}
356396f2e892SBill Paul }
356496f2e892SBill Paul 
3565e3d2833aSAlfred Perlstein static void
35660934f18aSMaxime Henrion dc_init(void *xsc)
356796f2e892SBill Paul {
356896f2e892SBill Paul 	struct dc_softc *sc = xsc;
3569c8b27acaSJohn Baldwin 
3570c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3571c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3572c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3573c8b27acaSJohn Baldwin }
3574c8b27acaSJohn Baldwin 
3575c8b27acaSJohn Baldwin static void
3576c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3577c8b27acaSJohn Baldwin {
3578fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
357996f2e892SBill Paul 	struct mii_data *mii;
3580d314ebf5SPyun YongHyeon 	struct ifmedia *ifm;
358196f2e892SBill Paul 
3582c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
358396f2e892SBill Paul 
35848f382a1fSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
35858f382a1fSPyun YongHyeon 		return;
35868f382a1fSPyun YongHyeon 
358796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
358896f2e892SBill Paul 
358996f2e892SBill Paul 	/*
359096f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
359196f2e892SBill Paul 	 */
359296f2e892SBill Paul 	dc_stop(sc);
359396f2e892SBill Paul 	dc_reset(sc);
3594d314ebf5SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
3595d314ebf5SPyun YongHyeon 		ifm = &mii->mii_media;
3596d314ebf5SPyun YongHyeon 		dc_apply_fixup(sc, ifm->ifm_media);
3597d314ebf5SPyun YongHyeon 	}
359896f2e892SBill Paul 
359996f2e892SBill Paul 	/*
360096f2e892SBill Paul 	 * Set cache alignment and burst length.
360196f2e892SBill Paul 	 */
360288d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
360396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
360496f2e892SBill Paul 	else
360596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3606935fe010SLuigi Rizzo 	/*
3607935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3608935fe010SLuigi Rizzo 	 */
3609935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3610935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
361196f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
361296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
361396f2e892SBill Paul 	} else {
361496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
361596f2e892SBill Paul 	}
361696f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
361796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
361896f2e892SBill Paul 	switch(sc->dc_cachesize) {
361996f2e892SBill Paul 	case 32:
362096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
362196f2e892SBill Paul 		break;
362296f2e892SBill Paul 	case 16:
362396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
362496f2e892SBill Paul 		break;
362596f2e892SBill Paul 	case 8:
362696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
362796f2e892SBill Paul 		break;
362896f2e892SBill Paul 	case 0:
362996f2e892SBill Paul 	default:
363096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
363196f2e892SBill Paul 		break;
363296f2e892SBill Paul 	}
363396f2e892SBill Paul 
363496f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
363596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
363696f2e892SBill Paul 	else {
3637d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
363896f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
363996f2e892SBill Paul 		} else {
364096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
364196f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
364296f2e892SBill Paul 		}
364396f2e892SBill Paul 	}
364496f2e892SBill Paul 
364596f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
364696f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
364796f2e892SBill Paul 
364896f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
364996f2e892SBill Paul 		/*
365096f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
365196f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
365296f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
365396f2e892SBill Paul 		 * document the meaning of these bits so there's no way
365496f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
365596f2e892SBill Paul 		 * number all its own; the rest all use a different one.
365696f2e892SBill Paul 		 */
365796f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
365896f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
365996f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
366096f2e892SBill Paul 		else
366196f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
366296f2e892SBill Paul 	}
366396f2e892SBill Paul 
3664feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3665feb78939SJonathan Chen 		/*
3666feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3667feb78939SJonathan Chen 		 * can talk to the MII.
3668feb78939SJonathan Chen 		 */
3669feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3670feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3671feb78939SJonathan Chen 		DELAY(10);
3672feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3673feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3674feb78939SJonathan Chen 		DELAY(10);
3675feb78939SJonathan Chen 	}
3676feb78939SJonathan Chen 
367796f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3678d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
367996f2e892SBill Paul 
368096f2e892SBill Paul 	/* Init circular RX list. */
368196f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
36826b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
368322f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
368496f2e892SBill Paul 		dc_stop(sc);
368596f2e892SBill Paul 		return;
368696f2e892SBill Paul 	}
368796f2e892SBill Paul 
368896f2e892SBill Paul 	/*
368956e5e7aeSMaxime Henrion 	 * Init TX descriptors.
369096f2e892SBill Paul 	 */
369196f2e892SBill Paul 	dc_list_tx_init(sc);
369296f2e892SBill Paul 
369396f2e892SBill Paul 	/*
369496f2e892SBill Paul 	 * Load the address of the RX list.
369596f2e892SBill Paul 	 */
369656e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
369756e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
369896f2e892SBill Paul 
369996f2e892SBill Paul 	/*
370096f2e892SBill Paul 	 * Enable interrupts.
370196f2e892SBill Paul 	 */
3702e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3703e4fc250cSLuigi Rizzo 	/*
3704e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3705e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3706e4fc250cSLuigi Rizzo 	 * after a reset.
3707e4fc250cSLuigi Rizzo 	 */
370840929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3709e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3710e4fc250cSLuigi Rizzo 	else
3711e4fc250cSLuigi Rizzo #endif
371296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
371396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
371496f2e892SBill Paul 
371596f2e892SBill Paul 	/* Enable transmitter. */
371696f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
371796f2e892SBill Paul 
371896f2e892SBill Paul 	/*
3719918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3720918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3721918434c8SBill Paul 	 * link and activity indications.
3722918434c8SBill Paul 	 */
372378999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3724918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3725918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
372678999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3727918434c8SBill Paul 	}
3728918434c8SBill Paul 
3729918434c8SBill Paul 	/*
373096f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
373196f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
373296f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
373396f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
373496f2e892SBill Paul 	 */
373596f2e892SBill Paul 	dc_setfilt(sc);
373696f2e892SBill Paul 
373796f2e892SBill Paul 	/* Enable receiver. */
373896f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
373996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
374096f2e892SBill Paul 
374113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
374213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
374396f2e892SBill Paul 
3744*d7e9ac75SPyun YongHyeon 	dc_ifmedia_upd_locked(sc);
3745d314ebf5SPyun YongHyeon 
374626b40a65SPyun YongHyeon 	/* Clear missed frames and overflow counter. */
374726b40a65SPyun YongHyeon 	CSR_READ_4(sc, DC_FRAMESDISCARDED);
374826b40a65SPyun YongHyeon 
3749857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
375045521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3751857fd445SBill Paul 		sc->dc_link = 1;
3752857fd445SBill Paul 	else {
3753318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3754b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3755318b02fdSBill Paul 		else
3756b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3757857fd445SBill Paul 	}
3758b1d16143SMarius Strobl 
3759b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
3760b1d16143SMarius Strobl 	callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
376196f2e892SBill Paul }
376296f2e892SBill Paul 
376396f2e892SBill Paul /*
376496f2e892SBill Paul  * Set media options.
376596f2e892SBill Paul  */
3766e3d2833aSAlfred Perlstein static int
37670934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
376896f2e892SBill Paul {
376996f2e892SBill Paul 	struct dc_softc *sc;
3770*d7e9ac75SPyun YongHyeon 	int error;
377196f2e892SBill Paul 
377296f2e892SBill Paul 	sc = ifp->if_softc;
3773c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3774*d7e9ac75SPyun YongHyeon 	error = dc_ifmedia_upd_locked(sc);
3775*d7e9ac75SPyun YongHyeon 	DC_UNLOCK(sc);
3776*d7e9ac75SPyun YongHyeon 	return (error);
3777*d7e9ac75SPyun YongHyeon }
3778f43d9309SBill Paul 
3779*d7e9ac75SPyun YongHyeon static int
3780*d7e9ac75SPyun YongHyeon dc_ifmedia_upd_locked(struct dc_softc *sc)
3781*d7e9ac75SPyun YongHyeon {
3782*d7e9ac75SPyun YongHyeon 	struct mii_data *mii;
3783*d7e9ac75SPyun YongHyeon 	struct ifmedia *ifm;
3784*d7e9ac75SPyun YongHyeon 	int error;
3785*d7e9ac75SPyun YongHyeon 
3786*d7e9ac75SPyun YongHyeon 	DC_LOCK_ASSERT(sc);
3787*d7e9ac75SPyun YongHyeon 
3788*d7e9ac75SPyun YongHyeon 	sc->dc_link = 0;
3789*d7e9ac75SPyun YongHyeon 	mii = device_get_softc(sc->dc_miibus);
3790*d7e9ac75SPyun YongHyeon 	error = mii_mediachg(mii);
3791*d7e9ac75SPyun YongHyeon 	if (error == 0) {
3792*d7e9ac75SPyun YongHyeon 		ifm = &mii->mii_media;
3793d314ebf5SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3794d314ebf5SPyun YongHyeon 			dc_setcfg(sc, ifm->ifm_media);
3795d314ebf5SPyun YongHyeon 		else if (DC_IS_DAVICOM(sc) &&
379645521525SPoul-Henning Kamp 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3797f43d9309SBill Paul 			dc_setcfg(sc, ifm->ifm_media);
3798*d7e9ac75SPyun YongHyeon 	}
379996f2e892SBill Paul 
3800*d7e9ac75SPyun YongHyeon 	return (error);
380196f2e892SBill Paul }
380296f2e892SBill Paul 
380396f2e892SBill Paul /*
380496f2e892SBill Paul  * Report current media status.
380596f2e892SBill Paul  */
3806e3d2833aSAlfred Perlstein static void
38070934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
380896f2e892SBill Paul {
380996f2e892SBill Paul 	struct dc_softc *sc;
381096f2e892SBill Paul 	struct mii_data *mii;
3811f43d9309SBill Paul 	struct ifmedia *ifm;
381296f2e892SBill Paul 
381396f2e892SBill Paul 	sc = ifp->if_softc;
381496f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3815c8b27acaSJohn Baldwin 	DC_LOCK(sc);
381696f2e892SBill Paul 	mii_pollstat(mii);
3817f43d9309SBill Paul 	ifm = &mii->mii_media;
3818f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
381945521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3820f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3821f43d9309SBill Paul 			ifmr->ifm_status = 0;
3822432120f2SMarius Strobl 			DC_UNLOCK(sc);
3823f43d9309SBill Paul 			return;
3824f43d9309SBill Paul 		}
3825f43d9309SBill Paul 	}
382696f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
382796f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3828c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
382996f2e892SBill Paul }
383096f2e892SBill Paul 
3831e3d2833aSAlfred Perlstein static int
38320934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
383396f2e892SBill Paul {
383496f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
383596f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
383696f2e892SBill Paul 	struct mii_data *mii;
3837d1ce9105SBill Paul 	int error = 0;
383896f2e892SBill Paul 
383996f2e892SBill Paul 	switch (command) {
384096f2e892SBill Paul 	case SIOCSIFFLAGS:
3841c8b27acaSJohn Baldwin 		DC_LOCK(sc);
384296f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
38435d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
38445d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
38455d6dfbbbSLuigi Rizzo 
384613f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
38475d6dfbbbSLuigi Rizzo 				if (need_setfilt)
384896f2e892SBill Paul 					dc_setfilt(sc);
38495d6dfbbbSLuigi Rizzo 			} else {
38508f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3851c8b27acaSJohn Baldwin 				dc_init_locked(sc);
385296f2e892SBill Paul 			}
385396f2e892SBill Paul 		} else {
385413f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
385596f2e892SBill Paul 				dc_stop(sc);
385696f2e892SBill Paul 		}
385796f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3858c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
385996f2e892SBill Paul 		break;
386096f2e892SBill Paul 	case SIOCADDMULTI:
386196f2e892SBill Paul 	case SIOCDELMULTI:
3862c8b27acaSJohn Baldwin 		DC_LOCK(sc);
386324507bc1SPyun YongHyeon 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
386496f2e892SBill Paul 			dc_setfilt(sc);
3865c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
386696f2e892SBill Paul 		break;
386796f2e892SBill Paul 	case SIOCGIFMEDIA:
386896f2e892SBill Paul 	case SIOCSIFMEDIA:
386996f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
387096f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
387196f2e892SBill Paul 		break;
3872e695984eSRuslan Ermilov 	case SIOCSIFCAP:
387340929967SGleb Smirnoff #ifdef DEVICE_POLLING
387440929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
387540929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
387640929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
387740929967SGleb Smirnoff 			if (error)
387840929967SGleb Smirnoff 				return(error);
3879c8b27acaSJohn Baldwin 			DC_LOCK(sc);
388040929967SGleb Smirnoff 			/* Disable interrupts */
388140929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
388240929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3883c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
388440929967SGleb Smirnoff 			return (error);
388540929967SGleb Smirnoff 		}
388640929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
388740929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
388840929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
388940929967SGleb Smirnoff 			/* Enable interrupts. */
389040929967SGleb Smirnoff 			DC_LOCK(sc);
389140929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
389240929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
389340929967SGleb Smirnoff 			DC_UNLOCK(sc);
389440929967SGleb Smirnoff 			return (error);
389540929967SGleb Smirnoff 		}
389640929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3897e695984eSRuslan Ermilov 		break;
389896f2e892SBill Paul 	default:
38999ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
390096f2e892SBill Paul 		break;
390196f2e892SBill Paul 	}
390296f2e892SBill Paul 
390396f2e892SBill Paul 	return (error);
390496f2e892SBill Paul }
390596f2e892SBill Paul 
3906e3d2833aSAlfred Perlstein static void
3907b1d16143SMarius Strobl dc_watchdog(void *xsc)
390896f2e892SBill Paul {
3909b1d16143SMarius Strobl 	struct dc_softc *sc = xsc;
3910b1d16143SMarius Strobl 	struct ifnet *ifp;
391196f2e892SBill Paul 
3912b1d16143SMarius Strobl 	DC_LOCK_ASSERT(sc);
391396f2e892SBill Paul 
3914b1d16143SMarius Strobl 	if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3915b1d16143SMarius Strobl 		callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3916b1d16143SMarius Strobl 		return;
3917b1d16143SMarius Strobl 	}
3918d1ce9105SBill Paul 
3919b1d16143SMarius Strobl 	ifp = sc->dc_ifp;
392096f2e892SBill Paul 	ifp->if_oerrors++;
3921b1d16143SMarius Strobl 	device_printf(sc->dc_dev, "watchdog timeout\n");
392296f2e892SBill Paul 
39238f382a1fSPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3924c8b27acaSJohn Baldwin 	dc_init_locked(sc);
392596f2e892SBill Paul 
3926cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3927c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
392896f2e892SBill Paul }
392996f2e892SBill Paul 
393096f2e892SBill Paul /*
393196f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
393296f2e892SBill Paul  * RX and TX lists.
393396f2e892SBill Paul  */
3934e3d2833aSAlfred Perlstein static void
39350934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
393696f2e892SBill Paul {
393796f2e892SBill Paul 	struct ifnet *ifp;
3938b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3939b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3940b3811c95SMaxime Henrion 	int i;
39411da7683aSPyun YongHyeon 	uint32_t ctl, netcfg;
394296f2e892SBill Paul 
3943c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3944d1ce9105SBill Paul 
3945fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
39465f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
3947b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
394896f2e892SBill Paul 
3949b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
3950b1d16143SMarius Strobl 	callout_stop(&sc->dc_wdog_ch);
3951b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
39521da7683aSPyun YongHyeon 	sc->dc_link = 0;
395396f2e892SBill Paul 
395413f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
39553b3ec200SPeter Wemm 
39561da7683aSPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
39571da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39581da7683aSPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
39591da7683aSPyun YongHyeon 		   netcfg & ~(DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
396096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
39611da7683aSPyun YongHyeon 	/* Wait the completion of TX/RX SM. */
39621da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39631da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
39641da7683aSPyun YongHyeon 
396596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
396696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
396796f2e892SBill Paul 
396896f2e892SBill Paul 	/*
396996f2e892SBill Paul 	 * Free data in the RX lists.
397096f2e892SBill Paul 	 */
397196f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3972b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
39735f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_rx_mtag,
39745f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i], BUS_DMASYNC_POSTREAD);
39755f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_mtag,
39765f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i]);
397756e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
397856e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
397996f2e892SBill Paul 		}
398096f2e892SBill Paul 	}
39815f14ee23SPyun YongHyeon 	bzero(ld->dc_rx_list, DC_RX_LIST_SZ);
39825f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
39835f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
398496f2e892SBill Paul 
398596f2e892SBill Paul 	/*
398696f2e892SBill Paul 	 * Free the TX list buffers.
398796f2e892SBill Paul 	 */
398896f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
3989b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
3990af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
39915f14ee23SPyun YongHyeon 			if (ctl & DC_TXCTL_SETUP) {
39925f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
39935f14ee23SPyun YongHyeon 				    BUS_DMASYNC_POSTWRITE);
39945f14ee23SPyun YongHyeon 			} else {
39955f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_tx_mtag,
39965f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i], BUS_DMASYNC_POSTWRITE);
39975f14ee23SPyun YongHyeon 				bus_dmamap_unload(sc->dc_tx_mtag,
39985f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i]);
399956e5e7aeSMaxime Henrion 				m_freem(cd->dc_tx_chain[i]);
40005f14ee23SPyun YongHyeon 			}
4001b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
400296f2e892SBill Paul 		}
400396f2e892SBill Paul 	}
40045f14ee23SPyun YongHyeon 	bzero(ld->dc_tx_list, DC_TX_LIST_SZ);
40055f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
40065f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
400796f2e892SBill Paul }
400896f2e892SBill Paul 
400996f2e892SBill Paul /*
4010e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
4011e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
4012e8388e14SMitsuru IWASAKI  * resume.
4013e8388e14SMitsuru IWASAKI  */
4014e3d2833aSAlfred Perlstein static int
40150934f18aSMaxime Henrion dc_suspend(device_t dev)
4016e8388e14SMitsuru IWASAKI {
4017e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4018e8388e14SMitsuru IWASAKI 
4019e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4020c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4021e8388e14SMitsuru IWASAKI 	dc_stop(sc);
4022e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
4023c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4024e8388e14SMitsuru IWASAKI 
4025e8388e14SMitsuru IWASAKI 	return (0);
4026e8388e14SMitsuru IWASAKI }
4027e8388e14SMitsuru IWASAKI 
4028e8388e14SMitsuru IWASAKI /*
4029e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
4030e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
4031e8388e14SMitsuru IWASAKI  * appropriate.
4032e8388e14SMitsuru IWASAKI  */
4033e3d2833aSAlfred Perlstein static int
40340934f18aSMaxime Henrion dc_resume(device_t dev)
4035e8388e14SMitsuru IWASAKI {
4036e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4037e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
4038e8388e14SMitsuru IWASAKI 
4039e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4040fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
4041e8388e14SMitsuru IWASAKI 
4042e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
4043c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4044e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
4045c8b27acaSJohn Baldwin 		dc_init_locked(sc);
4046e8388e14SMitsuru IWASAKI 
4047e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
4048c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4049e8388e14SMitsuru IWASAKI 
4050e8388e14SMitsuru IWASAKI 	return (0);
4051e8388e14SMitsuru IWASAKI }
4052e8388e14SMitsuru IWASAKI 
4053e8388e14SMitsuru IWASAKI /*
405496f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
405596f2e892SBill Paul  * get confused by errant DMAs when rebooting.
405696f2e892SBill Paul  */
40576a087a87SPyun YongHyeon static int
40580934f18aSMaxime Henrion dc_shutdown(device_t dev)
405996f2e892SBill Paul {
406096f2e892SBill Paul 	struct dc_softc *sc;
406196f2e892SBill Paul 
406296f2e892SBill Paul 	sc = device_get_softc(dev);
406396f2e892SBill Paul 
4064c8b27acaSJohn Baldwin 	DC_LOCK(sc);
406596f2e892SBill Paul 	dc_stop(sc);
4066c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
40676a087a87SPyun YongHyeon 
40686a087a87SPyun YongHyeon 	return (0);
406996f2e892SBill Paul }
407039d76ed6SPyun YongHyeon 
407139d76ed6SPyun YongHyeon static int
407239d76ed6SPyun YongHyeon dc_check_multiport(struct dc_softc *sc)
407339d76ed6SPyun YongHyeon {
407439d76ed6SPyun YongHyeon 	struct dc_softc *dsc;
407539d76ed6SPyun YongHyeon 	devclass_t dc;
407639d76ed6SPyun YongHyeon 	device_t child;
407739d76ed6SPyun YongHyeon 	uint8_t *eaddr;
407839d76ed6SPyun YongHyeon 	int unit;
407939d76ed6SPyun YongHyeon 
408039d76ed6SPyun YongHyeon 	dc = devclass_find("dc");
408139d76ed6SPyun YongHyeon 	for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
408239d76ed6SPyun YongHyeon 		child = devclass_get_device(dc, unit);
408339d76ed6SPyun YongHyeon 		if (child == NULL)
408439d76ed6SPyun YongHyeon 			continue;
408539d76ed6SPyun YongHyeon 		if (child == sc->dc_dev)
408639d76ed6SPyun YongHyeon 			continue;
408739d76ed6SPyun YongHyeon 		if (device_get_parent(child) != device_get_parent(sc->dc_dev))
408839d76ed6SPyun YongHyeon 			continue;
408939d76ed6SPyun YongHyeon 		if (unit > device_get_unit(sc->dc_dev))
409039d76ed6SPyun YongHyeon 			continue;
4091b289c607SPyun YongHyeon 		if (device_is_attached(child) == 0)
4092b289c607SPyun YongHyeon 			continue;
409339d76ed6SPyun YongHyeon 		dsc = device_get_softc(child);
4094b289c607SPyun YongHyeon 		device_printf(sc->dc_dev,
4095b289c607SPyun YongHyeon 		    "Using station address of %s as base\n",
409639d76ed6SPyun YongHyeon 		    device_get_nameunit(child));
409739d76ed6SPyun YongHyeon 		bcopy(dsc->dc_eaddr, sc->dc_eaddr, ETHER_ADDR_LEN);
409839d76ed6SPyun YongHyeon 		eaddr = (uint8_t *)sc->dc_eaddr;
409939d76ed6SPyun YongHyeon 		eaddr[5]++;
4100b289c607SPyun YongHyeon 		/* Prepare SROM to parse again. */
4101b289c607SPyun YongHyeon 		if (DC_IS_INTEL(sc) && dsc->dc_srom != NULL &&
4102b289c607SPyun YongHyeon 		    sc->dc_romwidth != 0) {
4103b289c607SPyun YongHyeon 			free(sc->dc_srom, M_DEVBUF);
4104b289c607SPyun YongHyeon 			sc->dc_romwidth = dsc->dc_romwidth;
4105b289c607SPyun YongHyeon 			sc->dc_srom = malloc(DC_ROM_SIZE(sc->dc_romwidth),
4106b289c607SPyun YongHyeon 			    M_DEVBUF, M_NOWAIT);
4107b289c607SPyun YongHyeon 			if (sc->dc_srom == NULL) {
4108b289c607SPyun YongHyeon 				device_printf(sc->dc_dev,
4109b289c607SPyun YongHyeon 				    "Could not allocate SROM buffer\n");
4110b289c607SPyun YongHyeon 				return (ENOMEM);
4111b289c607SPyun YongHyeon 			}
4112b289c607SPyun YongHyeon 			bcopy(dsc->dc_srom, sc->dc_srom,
4113b289c607SPyun YongHyeon 			    DC_ROM_SIZE(sc->dc_romwidth));
4114b289c607SPyun YongHyeon 		}
411539d76ed6SPyun YongHyeon 		return (0);
411639d76ed6SPyun YongHyeon 	}
411739d76ed6SPyun YongHyeon 	return (ENOENT);
411839d76ed6SPyun YongHyeon }
4119