xref: /freebsd/sys/dev/dc/if_dc.c (revision c8dfaf382fa6df9dc6fd1e1c3356e0c8bf607e6a)
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>
11076039bc8SGleb Smirnoff #include <net/if_var.h>
11196f2e892SBill Paul #include <net/if_arp.h>
11296f2e892SBill Paul #include <net/ethernet.h>
11396f2e892SBill Paul #include <net/if_dl.h>
11496f2e892SBill Paul #include <net/if_media.h>
115db40c1aeSDoug Ambrisko #include <net/if_types.h>
116db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
11796f2e892SBill Paul 
11896f2e892SBill Paul #include <net/bpf.h>
11996f2e892SBill Paul 
12096f2e892SBill Paul #include <machine/bus.h>
12196f2e892SBill Paul #include <machine/resource.h>
12296f2e892SBill Paul #include <sys/bus.h>
12396f2e892SBill Paul #include <sys/rman.h>
12496f2e892SBill Paul 
12596f2e892SBill Paul #include <dev/mii/mii.h>
1268c1093fcSMarius Strobl #include <dev/mii/mii_bitbang.h>
12796f2e892SBill Paul #include <dev/mii/miivar.h>
12896f2e892SBill Paul 
12919b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
13019b7ffd1SWarner Losh #include <dev/pci/pcivar.h>
13196f2e892SBill Paul 
13296f2e892SBill Paul #define	DC_USEIOSPACE
13396f2e892SBill Paul 
1346a3033a8SWarner Losh #include <dev/dc/if_dcreg.h>
13596f2e892SBill Paul 
136ec6a7299SMaxime Henrion #ifdef __sparc64__
137ec6a7299SMaxime Henrion #include <dev/ofw/openfirm.h>
138ec6a7299SMaxime Henrion #include <machine/ofw_machdep.h>
139ec6a7299SMaxime Henrion #endif
140ec6a7299SMaxime Henrion 
141f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, pci, 1, 1, 1);
142f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, ether, 1, 1, 1);
14395a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
14495a16455SPeter Wemm 
145919ccba7SWarner Losh /*
146919ccba7SWarner Losh  * "device miibus" is required in kernel config.  See GENERIC if you get
147919ccba7SWarner Losh  * errors here.
148919ccba7SWarner Losh  */
14996f2e892SBill Paul #include "miibus_if.h"
15096f2e892SBill Paul 
15196f2e892SBill Paul /*
15296f2e892SBill Paul  * Various supported device vendors/types and their names.
15396f2e892SBill Paul  */
15429658c96SDimitry Andric static const struct dc_type dc_devs[] = {
1551e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143), 0,
15696f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
1571e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009), 0,
15838deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
1591e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100), 0,
16096f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
1611e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), DC_REVISION_DM9102A,
16288d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
1631e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), 0,
1641e2e70b1SJohn Baldwin 		"Davicom DM9102 10/100BaseTX" },
1651e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981), 0,
16696f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
167593a1aeaSMartin Blapp 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983), 0,
168593a1aeaSMartin Blapp 		"ADMtek AN983 10/100BaseTX" },
1691e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
170a2d61e43SWarner Losh 		"ADMtek AN985 CardBus 10/100BaseTX or clone" },
1711e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
172e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
1731e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
174e351d778SMartin Blapp 		"ADMtek ADM9513 10/100BaseTX" },
1751e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), DC_REVISION_88141,
17696f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
1771e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), 0,
1781e2e70b1SJohn Baldwin 		"ASIX AX88140A 10/100BaseTX" },
1791e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), DC_REVISION_98713A,
18096f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
1811e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), 0,
1821e2e70b1SJohn Baldwin 		"Macronix 98713 10/100BaseTX" },
1831e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), DC_REVISION_98713A,
18496f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1851e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), 0,
18696f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1871e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98725,
18896f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
1891e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98715AEC_C,
1901e2e70b1SJohn Baldwin 		"Macronix 98715AEC-C 10/100BaseTX" },
1911e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), 0,
1921e2e70b1SJohn Baldwin 		"Macronix 98715/98715A 10/100BaseTX" },
1931e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727), 0,
194ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
1951e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115), 0,
19696f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
1971e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), DC_REVISION_82C169,
19896f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1991e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), 0,
2001e2e70b1SJohn Baldwin 		"82c168 PNIC 10/100BaseTX" },
2011e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217), 0,
2029ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
2031e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242), 0,
204fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
2051e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201), 0,
206feb78939SJonathan Chen 		"Xircom X3201 10/100BaseTX" },
2071e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD), 0,
2089be0993cSJohn Baldwin 		"Neteasy DRP-32TXD Cardbus 10/100" },
2091e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500), 0,
2101d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
2111e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX), 0,
212773c505fSMIHIRA Sanpei Yoshiro 		"Abocom FE2500MX 10/100BaseTX" },
2131e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112), 0,
2141af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
2151e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX), 0,
216948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
2171e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T), 0,
21897f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2191e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB), 0,
2207eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
2211e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120), 0,
222e7b9ab3aSBill Paul 		"Microsoft MN-120 CardBus 10/100" },
2231e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130), 0,
224e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
22517762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08), 0,
22617762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22717762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09), 0,
22817762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22952ca7ee2SPyun YongHyeon 	{ DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261), 0,
23052ca7ee2SPyun YongHyeon 		"ULi M5261 FastEthernet" },
23152ca7ee2SPyun YongHyeon 	{ DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263), 0,
23252ca7ee2SPyun YongHyeon 		"ULi M5263 FastEthernet" },
23396f2e892SBill Paul 	{ 0, 0, NULL }
23496f2e892SBill Paul };
23596f2e892SBill Paul 
236e51a25f8SAlfred Perlstein static int dc_probe(device_t);
237e51a25f8SAlfred Perlstein static int dc_attach(device_t);
238e51a25f8SAlfred Perlstein static int dc_detach(device_t);
239e8388e14SMitsuru IWASAKI static int dc_suspend(device_t);
240e8388e14SMitsuru IWASAKI static int dc_resume(device_t);
241ebc284ccSMarius Strobl static const struct dc_type *dc_devtype(device_t);
2425f14ee23SPyun YongHyeon static void dc_discard_rxbuf(struct dc_softc *, int);
2435f14ee23SPyun YongHyeon static int dc_newbuf(struct dc_softc *, int);
244a10c0e45SMike Silbersack static int dc_encap(struct dc_softc *, struct mbuf **);
245e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war(struct dc_softc *, int);
246e51a25f8SAlfred Perlstein static int dc_rx_resync(struct dc_softc *);
2471abcdbd1SAttilio Rao static int dc_rxeof(struct dc_softc *);
248e51a25f8SAlfred Perlstein static void dc_txeof(struct dc_softc *);
249e51a25f8SAlfred Perlstein static void dc_tick(void *);
250e51a25f8SAlfred Perlstein static void dc_tx_underrun(struct dc_softc *);
251e51a25f8SAlfred Perlstein static void dc_intr(void *);
252e51a25f8SAlfred Perlstein static void dc_start(struct ifnet *);
253c8b27acaSJohn Baldwin static void dc_start_locked(struct ifnet *);
254e51a25f8SAlfred Perlstein static int dc_ioctl(struct ifnet *, u_long, caddr_t);
255e51a25f8SAlfred Perlstein static void dc_init(void *);
256c8b27acaSJohn Baldwin static void dc_init_locked(struct dc_softc *);
257e51a25f8SAlfred Perlstein static void dc_stop(struct dc_softc *);
258b1d16143SMarius Strobl static void dc_watchdog(void *);
2596a087a87SPyun YongHyeon static int dc_shutdown(device_t);
260e51a25f8SAlfred Perlstein static int dc_ifmedia_upd(struct ifnet *);
261d7e9ac75SPyun YongHyeon static int dc_ifmedia_upd_locked(struct dc_softc *);
262e51a25f8SAlfred Perlstein static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
26396f2e892SBill Paul 
2645f14ee23SPyun YongHyeon static int dc_dma_alloc(struct dc_softc *);
2655f14ee23SPyun YongHyeon static void dc_dma_free(struct dc_softc *);
2665f14ee23SPyun YongHyeon static void dc_dma_map_addr(void *, bus_dma_segment_t *, int, int);
2675f14ee23SPyun YongHyeon 
268e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
269e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
270e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
271ee320f98SPyun YongHyeon static void dc_eeprom_getword(struct dc_softc *, int, uint16_t *);
272ee320f98SPyun YongHyeon static void dc_eeprom_getword_pnic(struct dc_softc *, int, uint16_t *);
273ee320f98SPyun YongHyeon static void dc_eeprom_getword_xircom(struct dc_softc *, int, uint16_t *);
2743097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
275e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
27696f2e892SBill Paul 
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 *);
28952ca7ee2SPyun YongHyeon static void dc_setfilt_uli(struct dc_softc *);
290e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
29196f2e892SBill Paul 
292e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
29396f2e892SBill Paul 
294e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
295e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
296e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
29796f2e892SBill Paul 
298abe4e865SPyun YongHyeon static int dc_read_srom(struct dc_softc *, int);
299abe4e865SPyun YongHyeon static int dc_parse_21143_srom(struct dc_softc *);
300abe4e865SPyun YongHyeon static int dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
301abe4e865SPyun YongHyeon static int dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
302abe4e865SPyun YongHyeon static int dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
303e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
30439d76ed6SPyun YongHyeon static int dc_check_multiport(struct dc_softc *);
3055c1cfac4SBill Paul 
3068c1093fcSMarius Strobl /*
3078c1093fcSMarius Strobl  * MII bit-bang glue
3088c1093fcSMarius Strobl  */
3098c1093fcSMarius Strobl static uint32_t dc_mii_bitbang_read(device_t);
3108c1093fcSMarius Strobl static void dc_mii_bitbang_write(device_t, uint32_t);
3118c1093fcSMarius Strobl 
3128c1093fcSMarius Strobl static const struct mii_bitbang_ops dc_mii_bitbang_ops = {
3138c1093fcSMarius Strobl 	dc_mii_bitbang_read,
3148c1093fcSMarius Strobl 	dc_mii_bitbang_write,
3158c1093fcSMarius Strobl 	{
3168c1093fcSMarius Strobl 		DC_SIO_MII_DATAOUT,	/* MII_BIT_MDO */
3178c1093fcSMarius Strobl 		DC_SIO_MII_DATAIN,	/* MII_BIT_MDI */
3188c1093fcSMarius Strobl 		DC_SIO_MII_CLK,		/* MII_BIT_MDC */
3198c1093fcSMarius Strobl 		0,			/* MII_BIT_DIR_HOST_PHY */
3208c1093fcSMarius Strobl 		DC_SIO_MII_DIR,		/* MII_BIT_DIR_PHY_HOST */
3218c1093fcSMarius Strobl 	}
3228c1093fcSMarius Strobl };
3238c1093fcSMarius Strobl 
32496f2e892SBill Paul #ifdef DC_USEIOSPACE
32596f2e892SBill Paul #define	DC_RES			SYS_RES_IOPORT
32696f2e892SBill Paul #define	DC_RID			DC_PCI_CFBIO
32796f2e892SBill Paul #else
32896f2e892SBill Paul #define	DC_RES			SYS_RES_MEMORY
32996f2e892SBill Paul #define	DC_RID			DC_PCI_CFBMA
33096f2e892SBill Paul #endif
33196f2e892SBill Paul 
33296f2e892SBill Paul static device_method_t dc_methods[] = {
33396f2e892SBill Paul 	/* Device interface */
33496f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
33596f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
33696f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
337e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
338e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
33996f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
34096f2e892SBill Paul 
34196f2e892SBill Paul 	/* MII interface */
34296f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
34396f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
34496f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
345f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
34696f2e892SBill Paul 
3474b7ec270SMarius Strobl 	DEVMETHOD_END
34896f2e892SBill Paul };
34996f2e892SBill Paul 
35096f2e892SBill Paul static driver_t dc_driver = {
35196f2e892SBill Paul 	"dc",
35296f2e892SBill Paul 	dc_methods,
35396f2e892SBill Paul 	sizeof(struct dc_softc)
35496f2e892SBill Paul };
35596f2e892SBill Paul 
35696f2e892SBill Paul static devclass_t dc_devclass;
35796f2e892SBill Paul 
358e4029d4cSMarius Strobl DRIVER_MODULE_ORDERED(dc, pci, dc_driver, dc_devclass, NULL, NULL,
359e4029d4cSMarius Strobl     SI_ORDER_ANY);
360e4029d4cSMarius Strobl DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, NULL, NULL);
36196f2e892SBill Paul 
36296f2e892SBill Paul #define	DC_SETBIT(sc, reg, x)				\
36396f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
36496f2e892SBill Paul 
36596f2e892SBill Paul #define	DC_CLRBIT(sc, reg, x)				\
36696f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
36796f2e892SBill Paul 
36896f2e892SBill Paul #define	SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
36996f2e892SBill Paul #define	SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
37096f2e892SBill Paul 
371e3d2833aSAlfred Perlstein static void
3720934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
37396f2e892SBill Paul {
37496f2e892SBill Paul 	int idx;
37596f2e892SBill Paul 
37696f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
37796f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
37896f2e892SBill Paul }
37996f2e892SBill Paul 
3802c876e15SPoul-Henning Kamp static void
3810934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3823097aa70SWarner Losh {
3833097aa70SWarner Losh 	int i;
3843097aa70SWarner Losh 
3853097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3863097aa70SWarner Losh 	dc_eeprom_idle(sc);
3873097aa70SWarner Losh 
3883097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3893097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3903097aa70SWarner Losh 	dc_delay(sc);
3913097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3923097aa70SWarner Losh 	dc_delay(sc);
3933097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3943097aa70SWarner Losh 	dc_delay(sc);
3953097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3963097aa70SWarner Losh 	dc_delay(sc);
3973097aa70SWarner Losh 
3983097aa70SWarner Losh 	for (i = 3; i--;) {
3993097aa70SWarner Losh 		if (6 & (1 << i))
4003097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4013097aa70SWarner Losh 		else
4023097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4033097aa70SWarner Losh 		dc_delay(sc);
4043097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4053097aa70SWarner Losh 		dc_delay(sc);
4063097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4073097aa70SWarner Losh 		dc_delay(sc);
4083097aa70SWarner Losh 	}
4093097aa70SWarner Losh 
4103097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
4113097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4123097aa70SWarner Losh 		dc_delay(sc);
4133097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
4143097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4153097aa70SWarner Losh 			dc_delay(sc);
4163097aa70SWarner Losh 			break;
4173097aa70SWarner Losh 		}
4183097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4193097aa70SWarner Losh 		dc_delay(sc);
4203097aa70SWarner Losh 	}
4213097aa70SWarner Losh 
4223097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4233097aa70SWarner Losh 	dc_eeprom_idle(sc);
4243097aa70SWarner Losh 
4253097aa70SWarner Losh 	if (i < 4 || i > 12)
4263097aa70SWarner Losh 		sc->dc_romwidth = 6;
4273097aa70SWarner Losh 	else
4283097aa70SWarner Losh 		sc->dc_romwidth = i;
4293097aa70SWarner Losh 
4303097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4313097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4323097aa70SWarner Losh 	dc_delay(sc);
4333097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4343097aa70SWarner Losh 	dc_delay(sc);
4353097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4363097aa70SWarner Losh 	dc_delay(sc);
4373097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4383097aa70SWarner Losh 	dc_delay(sc);
4393097aa70SWarner Losh 
4403097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4413097aa70SWarner Losh 	dc_eeprom_idle(sc);
4423097aa70SWarner Losh }
4433097aa70SWarner Losh 
444e3d2833aSAlfred Perlstein static void
4450934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
44696f2e892SBill Paul {
4470934f18aSMaxime Henrion 	int i;
44896f2e892SBill Paul 
44996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
45096f2e892SBill Paul 	dc_delay(sc);
45196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
45296f2e892SBill Paul 	dc_delay(sc);
45396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45496f2e892SBill Paul 	dc_delay(sc);
45596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
45696f2e892SBill Paul 	dc_delay(sc);
45796f2e892SBill Paul 
45896f2e892SBill Paul 	for (i = 0; i < 25; i++) {
45996f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46096f2e892SBill Paul 		dc_delay(sc);
46196f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46296f2e892SBill Paul 		dc_delay(sc);
46396f2e892SBill Paul 	}
46496f2e892SBill Paul 
46596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46696f2e892SBill Paul 	dc_delay(sc);
46796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
46896f2e892SBill Paul 	dc_delay(sc);
46996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
47096f2e892SBill Paul }
47196f2e892SBill Paul 
47296f2e892SBill Paul /*
47396f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
47496f2e892SBill Paul  */
475e3d2833aSAlfred Perlstein static void
4760934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
47796f2e892SBill Paul {
4780934f18aSMaxime Henrion 	int d, i;
47996f2e892SBill Paul 
4803097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4813097aa70SWarner Losh 	for (i = 3; i--; ) {
4823097aa70SWarner Losh 		if (d & (1 << i))
4833097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
48496f2e892SBill Paul 		else
4853097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4863097aa70SWarner Losh 		dc_delay(sc);
4873097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4883097aa70SWarner Losh 		dc_delay(sc);
4893097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4903097aa70SWarner Losh 		dc_delay(sc);
4913097aa70SWarner Losh 	}
49296f2e892SBill Paul 
49396f2e892SBill Paul 	/*
49496f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
49596f2e892SBill Paul 	 */
4963097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4973097aa70SWarner Losh 		if (addr & (1 << i)) {
49896f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
49996f2e892SBill Paul 		} else {
50096f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
50196f2e892SBill Paul 		}
50296f2e892SBill Paul 		dc_delay(sc);
50396f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
50496f2e892SBill Paul 		dc_delay(sc);
50596f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
50696f2e892SBill Paul 		dc_delay(sc);
50796f2e892SBill Paul 	}
50896f2e892SBill Paul }
50996f2e892SBill Paul 
51096f2e892SBill Paul /*
51196f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
51296f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
51396f2e892SBill Paul  * the EEPROM.
51496f2e892SBill Paul  */
515e3d2833aSAlfred Perlstein static void
516ee320f98SPyun YongHyeon dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, uint16_t *dest)
51796f2e892SBill Paul {
5180934f18aSMaxime Henrion 	int i;
519ee320f98SPyun YongHyeon 	uint32_t r;
52096f2e892SBill Paul 
52196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
52296f2e892SBill Paul 
52396f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
52496f2e892SBill Paul 		DELAY(1);
52596f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
52696f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
527ee320f98SPyun YongHyeon 			*dest = (uint16_t)(r & 0xFFFF);
52896f2e892SBill Paul 			return;
52996f2e892SBill Paul 		}
53096f2e892SBill Paul 	}
53196f2e892SBill Paul }
53296f2e892SBill Paul 
53396f2e892SBill Paul /*
53496f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
535feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
536feb78939SJonathan Chen  * the EEPROM, too.
537feb78939SJonathan Chen  */
538e3d2833aSAlfred Perlstein static void
539ee320f98SPyun YongHyeon dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, uint16_t *dest)
540feb78939SJonathan Chen {
5410934f18aSMaxime Henrion 
542feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
543feb78939SJonathan Chen 
544feb78939SJonathan Chen 	addr *= 2;
545feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
546ee320f98SPyun YongHyeon 	*dest = (uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
547feb78939SJonathan Chen 	addr += 1;
548feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
549ee320f98SPyun YongHyeon 	*dest |= ((uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
550feb78939SJonathan Chen 
551feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
552feb78939SJonathan Chen }
553feb78939SJonathan Chen 
554feb78939SJonathan Chen /*
555feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
55696f2e892SBill Paul  */
557e3d2833aSAlfred Perlstein static void
558ee320f98SPyun YongHyeon dc_eeprom_getword(struct dc_softc *sc, int addr, uint16_t *dest)
55996f2e892SBill Paul {
5600934f18aSMaxime Henrion 	int i;
561ee320f98SPyun YongHyeon 	uint16_t word = 0;
56296f2e892SBill Paul 
56396f2e892SBill Paul 	/* Force EEPROM to idle state. */
56496f2e892SBill Paul 	dc_eeprom_idle(sc);
56596f2e892SBill Paul 
56696f2e892SBill Paul 	/* Enter EEPROM access mode. */
56796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
56896f2e892SBill Paul 	dc_delay(sc);
56996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
57096f2e892SBill Paul 	dc_delay(sc);
57196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
57296f2e892SBill Paul 	dc_delay(sc);
57396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
57496f2e892SBill Paul 	dc_delay(sc);
57596f2e892SBill Paul 
57696f2e892SBill Paul 	/*
57796f2e892SBill Paul 	 * Send address of word we want to read.
57896f2e892SBill Paul 	 */
57996f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
58096f2e892SBill Paul 
58196f2e892SBill Paul 	/*
58296f2e892SBill Paul 	 * Start reading bits from EEPROM.
58396f2e892SBill Paul 	 */
58496f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
58596f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
58696f2e892SBill Paul 		dc_delay(sc);
58796f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
58896f2e892SBill Paul 			word |= i;
58996f2e892SBill Paul 		dc_delay(sc);
59096f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
59196f2e892SBill Paul 		dc_delay(sc);
59296f2e892SBill Paul 	}
59396f2e892SBill Paul 
59496f2e892SBill Paul 	/* Turn off EEPROM access mode. */
59596f2e892SBill Paul 	dc_eeprom_idle(sc);
59696f2e892SBill Paul 
59796f2e892SBill Paul 	*dest = word;
59896f2e892SBill Paul }
59996f2e892SBill Paul 
60096f2e892SBill Paul /*
60196f2e892SBill Paul  * Read a sequence of words from the EEPROM.
60296f2e892SBill Paul  */
603e3d2833aSAlfred Perlstein static void
6048c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
60596f2e892SBill Paul {
60696f2e892SBill Paul 	int i;
607ee320f98SPyun YongHyeon 	uint16_t word = 0, *ptr;
60896f2e892SBill Paul 
60996f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
61096f2e892SBill Paul 		if (DC_IS_PNIC(sc))
61196f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
612feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
613feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
61496f2e892SBill Paul 		else
61596f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
616ee320f98SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
6178c7ff1f3SMaxime Henrion 		if (be)
6188c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
61996f2e892SBill Paul 		else
6208c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
62196f2e892SBill Paul 	}
62296f2e892SBill Paul }
62396f2e892SBill Paul 
62496f2e892SBill Paul /*
6258c1093fcSMarius Strobl  * Write the MII serial port for the MII bit-bang module.
62696f2e892SBill Paul  */
627e3d2833aSAlfred Perlstein static void
6288c1093fcSMarius Strobl dc_mii_bitbang_write(device_t dev, uint32_t val)
62996f2e892SBill Paul {
6308c1093fcSMarius Strobl 	struct dc_softc *sc;
6310934f18aSMaxime Henrion 
6328c1093fcSMarius Strobl 	sc = device_get_softc(dev);
63396f2e892SBill Paul 
6348c1093fcSMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, val);
63515578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
63615578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
63796f2e892SBill Paul }
63896f2e892SBill Paul 
63996f2e892SBill Paul /*
6408c1093fcSMarius Strobl  * Read the MII serial port for the MII bit-bang module.
64196f2e892SBill Paul  */
6428c1093fcSMarius Strobl static uint32_t
6438c1093fcSMarius Strobl dc_mii_bitbang_read(device_t dev)
64496f2e892SBill Paul {
6458c1093fcSMarius Strobl 	struct dc_softc *sc;
6468c1093fcSMarius Strobl 	uint32_t val;
6470934f18aSMaxime Henrion 
6488c1093fcSMarius Strobl 	sc = device_get_softc(dev);
6498c1093fcSMarius Strobl 
6508c1093fcSMarius Strobl 	val = CSR_READ_4(sc, DC_SIO);
65115578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
65215578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
65396f2e892SBill Paul 
6548c1093fcSMarius Strobl 	return (val);
65596f2e892SBill Paul }
65696f2e892SBill Paul 
657e3d2833aSAlfred Perlstein static int
6580934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
65996f2e892SBill Paul {
66096f2e892SBill Paul 	struct dc_softc *sc;
661c85c4667SBill Paul 	int i, rval, phy_reg = 0;
66296f2e892SBill Paul 
66396f2e892SBill Paul 	sc = device_get_softc(dev);
66496f2e892SBill Paul 
6655c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
66696f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
66796f2e892SBill Paul 			switch (reg) {
66896f2e892SBill Paul 			case MII_BMSR:
66996f2e892SBill Paul 			/*
67096f2e892SBill Paul 			 * Fake something to make the probe
67196f2e892SBill Paul 			 * code think there's a PHY here.
67296f2e892SBill Paul 			 */
67396f2e892SBill Paul 				return (BMSR_MEDIAMASK);
67496f2e892SBill Paul 				break;
67596f2e892SBill Paul 			case MII_PHYIDR1:
67696f2e892SBill Paul 				if (DC_IS_PNIC(sc))
67796f2e892SBill Paul 					return (DC_VENDORID_LO);
67896f2e892SBill Paul 				return (DC_VENDORID_DEC);
67996f2e892SBill Paul 				break;
68096f2e892SBill Paul 			case MII_PHYIDR2:
68196f2e892SBill Paul 				if (DC_IS_PNIC(sc))
68296f2e892SBill Paul 					return (DC_DEVICEID_82C168);
68396f2e892SBill Paul 				return (DC_DEVICEID_21143);
68496f2e892SBill Paul 				break;
68596f2e892SBill Paul 			default:
68696f2e892SBill Paul 				return (0);
68796f2e892SBill Paul 				break;
68896f2e892SBill Paul 			}
68996f2e892SBill Paul 		} else
69096f2e892SBill Paul 			return (0);
69196f2e892SBill Paul 	}
69296f2e892SBill Paul 
69396f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
69496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
69596f2e892SBill Paul 		    (phy << 23) | (reg << 18));
69696f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
69796f2e892SBill Paul 			DELAY(1);
69896f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
69996f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
70096f2e892SBill Paul 				rval &= 0xFFFF;
70196f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
70296f2e892SBill Paul 			}
70396f2e892SBill Paul 		}
70496f2e892SBill Paul 		return (0);
70596f2e892SBill Paul 	}
70696f2e892SBill Paul 
70752ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
70852ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
70952ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
71052ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
71152ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_READ);
71252ca7ee2SPyun YongHyeon 		for (i = 0; i < DC_TIMEOUT; i++) {
71352ca7ee2SPyun YongHyeon 			DELAY(1);
71452ca7ee2SPyun YongHyeon 			rval = CSR_READ_4(sc, DC_ROM);
71552ca7ee2SPyun YongHyeon 			if ((rval & DC_ULI_PHY_OP_DONE) != 0) {
71652ca7ee2SPyun YongHyeon 				return (rval & DC_ULI_PHY_DATA_MASK);
71752ca7ee2SPyun YongHyeon 			}
71852ca7ee2SPyun YongHyeon 		}
71952ca7ee2SPyun YongHyeon 		if (i == DC_TIMEOUT)
72052ca7ee2SPyun YongHyeon 			device_printf(dev, "phy read timed out\n");
72152ca7ee2SPyun YongHyeon 		return (0);
72252ca7ee2SPyun YongHyeon 	}
72352ca7ee2SPyun YongHyeon 
72496f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
72596f2e892SBill Paul 		switch (reg) {
72696f2e892SBill Paul 		case MII_BMCR:
72796f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
72896f2e892SBill Paul 			break;
72996f2e892SBill Paul 		case MII_BMSR:
73096f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
73196f2e892SBill Paul 			break;
73296f2e892SBill Paul 		case MII_PHYIDR1:
73396f2e892SBill Paul 			phy_reg = DC_AL_VENID;
73496f2e892SBill Paul 			break;
73596f2e892SBill Paul 		case MII_PHYIDR2:
73696f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
73796f2e892SBill Paul 			break;
73896f2e892SBill Paul 		case MII_ANAR:
73996f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
74096f2e892SBill Paul 			break;
74196f2e892SBill Paul 		case MII_ANLPAR:
74296f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
74396f2e892SBill Paul 			break;
74496f2e892SBill Paul 		case MII_ANER:
74596f2e892SBill Paul 			phy_reg = DC_AL_ANER;
74696f2e892SBill Paul 			break;
74796f2e892SBill Paul 		default:
74822f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
74922f6205dSJohn Baldwin 			    reg);
75096f2e892SBill Paul 			return (0);
75196f2e892SBill Paul 			break;
75296f2e892SBill Paul 		}
75396f2e892SBill Paul 
75496f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
75596f2e892SBill Paul 		if (rval == 0xFFFF)
75696f2e892SBill Paul 			return (0);
75796f2e892SBill Paul 		return (rval);
75896f2e892SBill Paul 	}
75996f2e892SBill Paul 
760419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
761f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
762f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
763419146d9SBill Paul 	}
7648c1093fcSMarius Strobl 	rval = mii_bitbang_readreg(dev, &dc_mii_bitbang_ops, phy, reg);
765419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
766f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
76796f2e892SBill Paul 
7688c1093fcSMarius Strobl 	return (rval);
76996f2e892SBill Paul }
77096f2e892SBill Paul 
771e3d2833aSAlfred Perlstein static int
7720934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
77396f2e892SBill Paul {
77496f2e892SBill Paul 	struct dc_softc *sc;
775c85c4667SBill Paul 	int i, phy_reg = 0;
77696f2e892SBill Paul 
77796f2e892SBill Paul 	sc = device_get_softc(dev);
77896f2e892SBill Paul 
77996f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
78096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
78196f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
78296f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
78396f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
78496f2e892SBill Paul 				break;
78596f2e892SBill Paul 		}
78696f2e892SBill Paul 		return (0);
78796f2e892SBill Paul 	}
78896f2e892SBill Paul 
78952ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
79052ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
79152ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
79252ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
79352ca7ee2SPyun YongHyeon 		    ((data << DC_ULI_PHY_DATA_SHIFT) & DC_ULI_PHY_DATA_MASK) |
79452ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_WRITE);
79552ca7ee2SPyun YongHyeon 		DELAY(1);
79652ca7ee2SPyun YongHyeon 		return (0);
79752ca7ee2SPyun YongHyeon 	}
79852ca7ee2SPyun YongHyeon 
79996f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
80096f2e892SBill Paul 		switch (reg) {
80196f2e892SBill Paul 		case MII_BMCR:
80296f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
80396f2e892SBill Paul 			break;
80496f2e892SBill Paul 		case MII_BMSR:
80596f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
80696f2e892SBill Paul 			break;
80796f2e892SBill Paul 		case MII_PHYIDR1:
80896f2e892SBill Paul 			phy_reg = DC_AL_VENID;
80996f2e892SBill Paul 			break;
81096f2e892SBill Paul 		case MII_PHYIDR2:
81196f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
81296f2e892SBill Paul 			break;
81396f2e892SBill Paul 		case MII_ANAR:
81496f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
81596f2e892SBill Paul 			break;
81696f2e892SBill Paul 		case MII_ANLPAR:
81796f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
81896f2e892SBill Paul 			break;
81996f2e892SBill Paul 		case MII_ANER:
82096f2e892SBill Paul 			phy_reg = DC_AL_ANER;
82196f2e892SBill Paul 			break;
82296f2e892SBill Paul 		default:
82322f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
82422f6205dSJohn Baldwin 			    reg);
82596f2e892SBill Paul 			return (0);
82696f2e892SBill Paul 			break;
82796f2e892SBill Paul 		}
82896f2e892SBill Paul 
82996f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
83096f2e892SBill Paul 		return (0);
83196f2e892SBill Paul 	}
83296f2e892SBill Paul 
833419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
834f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
835f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
836419146d9SBill Paul 	}
8378c1093fcSMarius Strobl 	mii_bitbang_writereg(dev, &dc_mii_bitbang_ops, phy, reg, data);
838419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
839f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
84096f2e892SBill Paul 
84196f2e892SBill Paul 	return (0);
84296f2e892SBill Paul }
84396f2e892SBill Paul 
844e3d2833aSAlfred Perlstein static void
8450934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
84696f2e892SBill Paul {
84796f2e892SBill Paul 	struct dc_softc *sc;
848d314ebf5SPyun YongHyeon 	struct ifnet *ifp;
84996f2e892SBill Paul 	struct mii_data *mii;
850f43d9309SBill Paul 	struct ifmedia *ifm;
85196f2e892SBill Paul 
85296f2e892SBill Paul 	sc = device_get_softc(dev);
8535c1cfac4SBill Paul 
85496f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
855d314ebf5SPyun YongHyeon 	ifp = sc->dc_ifp;
856d314ebf5SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
857d314ebf5SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
858d314ebf5SPyun YongHyeon 		return;
859d314ebf5SPyun YongHyeon 
860f43d9309SBill Paul 	ifm = &mii->mii_media;
86189b2411bSPyun YongHyeon 	if (DC_IS_DAVICOM(sc) && IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
862f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
863d314ebf5SPyun YongHyeon 		return;
86489b2411bSPyun YongHyeon 	} else if (!DC_IS_ADMTEK(sc))
86589b2411bSPyun YongHyeon 		dc_setcfg(sc, mii->mii_media_active);
866d314ebf5SPyun YongHyeon 
867d314ebf5SPyun YongHyeon 	sc->dc_link = 0;
868d314ebf5SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
869d314ebf5SPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
870d314ebf5SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
871d314ebf5SPyun YongHyeon 		case IFM_10_T:
872d314ebf5SPyun YongHyeon 		case IFM_100_TX:
873d314ebf5SPyun YongHyeon 			sc->dc_link = 1;
874d314ebf5SPyun YongHyeon 			break;
875d314ebf5SPyun YongHyeon 		}
876d314ebf5SPyun YongHyeon 	}
877f43d9309SBill Paul }
878f43d9309SBill Paul 
879f43d9309SBill Paul /*
880f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
881f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
882f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
883f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
884f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
885f43d9309SBill Paul  * with it itself. *sigh*
886f43d9309SBill Paul  */
887e3d2833aSAlfred Perlstein static void
8880934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
889f43d9309SBill Paul {
890f43d9309SBill Paul 	struct dc_softc *sc;
891f43d9309SBill Paul 	struct mii_data *mii;
892f43d9309SBill Paul 	struct ifmedia *ifm;
893f43d9309SBill Paul 	int rev;
894f43d9309SBill Paul 
8951e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
896f43d9309SBill Paul 
897f43d9309SBill Paul 	sc = device_get_softc(dev);
898f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
899f43d9309SBill Paul 	ifm = &mii->mii_media;
900f43d9309SBill Paul 
901f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
90245521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
90396f2e892SBill Paul }
90496f2e892SBill Paul 
90579d11e09SBill Paul #define	DC_BITS_512	9
90679d11e09SBill Paul #define	DC_BITS_128	7
90779d11e09SBill Paul #define	DC_BITS_64	6
90896f2e892SBill Paul 
9093373489bSWarner Losh static uint32_t
9103373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
91196f2e892SBill Paul {
9123373489bSWarner Losh 	uint32_t crc;
91396f2e892SBill Paul 
91496f2e892SBill Paul 	/* Compute CRC for the address value. */
9150e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
91696f2e892SBill Paul 
91779d11e09SBill Paul 	/*
91879d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
91979d11e09SBill Paul 	 * chips is only 128 bits wide.
92079d11e09SBill Paul 	 */
92179d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
92279d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
92396f2e892SBill Paul 
92479d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
92579d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
92679d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
92779d11e09SBill Paul 
928feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
929feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
930feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
931feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
9320934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
933feb78939SJonathan Chen 		else
9340934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
9350934f18aSMaxime Henrion 			    (12 << 4));
936feb78939SJonathan Chen 	}
937feb78939SJonathan Chen 
93879d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
93996f2e892SBill Paul }
94096f2e892SBill Paul 
94196f2e892SBill Paul /*
94296f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
94396f2e892SBill Paul  */
9443373489bSWarner Losh static uint32_t
9453373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
94696f2e892SBill Paul {
9470e939c0cSChristian Weisgerber 	uint32_t crc;
94896f2e892SBill Paul 
94996f2e892SBill Paul 	/* Compute CRC for the address value. */
9500e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
95196f2e892SBill Paul 
9520934f18aSMaxime Henrion 	/* Return the filter bit position. */
95396f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
95496f2e892SBill Paul }
95596f2e892SBill Paul 
95696f2e892SBill Paul /*
95796f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
95896f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
95996f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
96096f2e892SBill Paul  *
96196f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
96296f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
96396f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
96496f2e892SBill Paul  * we need that too.
96596f2e892SBill Paul  */
9662c876e15SPoul-Henning Kamp static void
9670934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
96896f2e892SBill Paul {
9698df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
97096f2e892SBill Paul 	struct dc_desc *sframe;
971ee320f98SPyun YongHyeon 	uint32_t h, *sp;
97296f2e892SBill Paul 	struct ifmultiaddr *ifma;
97396f2e892SBill Paul 	struct ifnet *ifp;
97496f2e892SBill Paul 	int i;
97596f2e892SBill Paul 
976fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
97796f2e892SBill Paul 
97896f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
97996f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
98096f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
9815f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
98256e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
9830934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
98496f2e892SBill Paul 
9855f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
986af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
987af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
98896f2e892SBill Paul 
98956e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
99096f2e892SBill Paul 
99196f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
99296f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
99396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99496f2e892SBill Paul 	else
99596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99696f2e892SBill Paul 
99796f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
99896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
99996f2e892SBill Paul 	else
100096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100196f2e892SBill Paul 
1002eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
10036817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
100496f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100596f2e892SBill Paul 			continue;
1006aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
100796f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1008af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
100996f2e892SBill Paul 	}
1010eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
101196f2e892SBill Paul 
101296f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1013aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1014af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
101596f2e892SBill Paul 	}
101696f2e892SBill Paul 
10178df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
10188df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10198df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
10208df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
10218df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
102296f2e892SBill Paul 
1023af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
10248c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
10258c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
10265f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
102796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
102896f2e892SBill Paul 
102996f2e892SBill Paul 	/*
103096f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
103196f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
103296f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
103396f2e892SBill Paul 	 * medicine.
103496f2e892SBill Paul 	 */
103596f2e892SBill Paul 	DELAY(10000);
103696f2e892SBill Paul 
1037b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
103896f2e892SBill Paul }
103996f2e892SBill Paul 
10402c876e15SPoul-Henning Kamp static void
10410934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
104296f2e892SBill Paul {
10432e3d4b79SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
104496f2e892SBill Paul 	struct ifnet *ifp;
10450934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
104696f2e892SBill Paul 	int h = 0;
1047ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
104896f2e892SBill Paul 
1049fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
105096f2e892SBill Paul 
10510934f18aSMaxime Henrion 	/* Init our MAC address. */
10528df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10532e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
10542e3d4b79SPyun YongHyeon 	    eaddr[1] << 8 | eaddr[0]);
10552e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
105696f2e892SBill Paul 
105796f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
105896f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
105996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
106096f2e892SBill Paul 	else
106196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
106296f2e892SBill Paul 
106396f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
106496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106596f2e892SBill Paul 	else
106696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106796f2e892SBill Paul 
10680934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
106996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
107096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
107196f2e892SBill Paul 
107296f2e892SBill Paul 	/*
107396f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
107496f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
107596f2e892SBill Paul 	 */
107696f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
107796f2e892SBill Paul 		return;
107896f2e892SBill Paul 
10790934f18aSMaxime Henrion 	/* Now program new ones. */
1080eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
10816817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
108296f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
108396f2e892SBill Paul 			continue;
1084acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1085aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1086aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1087acc1bcccSMartin Blapp 		else
1088aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1089aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
109096f2e892SBill Paul 		if (h < 32)
109196f2e892SBill Paul 			hashes[0] |= (1 << h);
109296f2e892SBill Paul 		else
109396f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
109496f2e892SBill Paul 	}
1095eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
109696f2e892SBill Paul 
109796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
109896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
109996f2e892SBill Paul }
110096f2e892SBill Paul 
11012c876e15SPoul-Henning Kamp static void
11020934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
110396f2e892SBill Paul {
11048df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
110596f2e892SBill Paul 	struct ifnet *ifp;
11060934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
110796f2e892SBill Paul 	int h = 0;
1108ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
110996f2e892SBill Paul 
1110fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
111196f2e892SBill Paul 
11128df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
11138df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
111496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
11158df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
111696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
11178df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
111896f2e892SBill Paul 
111996f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
112096f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
112196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112296f2e892SBill Paul 	else
112396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112496f2e892SBill Paul 
112596f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
112696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112796f2e892SBill Paul 	else
112896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112996f2e892SBill Paul 
113096f2e892SBill Paul 	/*
113196f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
113296f2e892SBill Paul 	 * of broadcast frames.
113396f2e892SBill Paul 	 */
113496f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
113596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113696f2e892SBill Paul 	else
113796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113896f2e892SBill Paul 
113996f2e892SBill Paul 	/* first, zot all the existing hash bits */
114096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
114196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
114396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114496f2e892SBill Paul 
114596f2e892SBill Paul 	/*
114696f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
114796f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
114896f2e892SBill Paul 	 */
114996f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
115096f2e892SBill Paul 		return;
115196f2e892SBill Paul 
115296f2e892SBill Paul 	/* now program new ones */
1153eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
11546817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
115596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
115696f2e892SBill Paul 			continue;
1157aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
115896f2e892SBill Paul 		if (h < 32)
115996f2e892SBill Paul 			hashes[0] |= (1 << h);
116096f2e892SBill Paul 		else
116196f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
116296f2e892SBill Paul 	}
1163eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
116496f2e892SBill Paul 
116596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
116696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
116796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
116896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
116996f2e892SBill Paul }
117096f2e892SBill Paul 
11712c876e15SPoul-Henning Kamp static void
117252ca7ee2SPyun YongHyeon dc_setfilt_uli(struct dc_softc *sc)
117352ca7ee2SPyun YongHyeon {
117452ca7ee2SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
117552ca7ee2SPyun YongHyeon 	struct ifnet *ifp;
117652ca7ee2SPyun YongHyeon 	struct ifmultiaddr *ifma;
117752ca7ee2SPyun YongHyeon 	struct dc_desc *sframe;
117852ca7ee2SPyun YongHyeon 	uint32_t filter, *sp;
117952ca7ee2SPyun YongHyeon 	uint8_t *ma;
118052ca7ee2SPyun YongHyeon 	int i, mcnt;
118152ca7ee2SPyun YongHyeon 
118252ca7ee2SPyun YongHyeon 	ifp = sc->dc_ifp;
118352ca7ee2SPyun YongHyeon 
118452ca7ee2SPyun YongHyeon 	i = sc->dc_cdata.dc_tx_prod;
118552ca7ee2SPyun YongHyeon 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
118652ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_cnt++;
118752ca7ee2SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
118852ca7ee2SPyun YongHyeon 	sp = sc->dc_cdata.dc_sbuf;
118952ca7ee2SPyun YongHyeon 	bzero(sp, DC_SFRAME_LEN);
119052ca7ee2SPyun YongHyeon 
119152ca7ee2SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
119252ca7ee2SPyun YongHyeon 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
119352ca7ee2SPyun YongHyeon 	    DC_TXCTL_TLINK | DC_FILTER_PERFECT | DC_TXCTL_FINT);
119452ca7ee2SPyun YongHyeon 
119552ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
119652ca7ee2SPyun YongHyeon 
119752ca7ee2SPyun YongHyeon 	/* Set station address. */
119852ca7ee2SPyun YongHyeon 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
119952ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[1] << 8 | eaddr[0]);
120052ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[3] << 8 | eaddr[2]);
120152ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[5] << 8 | eaddr[4]);
120252ca7ee2SPyun YongHyeon 
120352ca7ee2SPyun YongHyeon 	/* Set broadcast address. */
120452ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120552ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120652ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120752ca7ee2SPyun YongHyeon 
120852ca7ee2SPyun YongHyeon 	/* Extract current filter configuration. */
120952ca7ee2SPyun YongHyeon 	filter = CSR_READ_4(sc, DC_NETCFG);
121052ca7ee2SPyun YongHyeon 	filter &= ~(DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI);
121152ca7ee2SPyun YongHyeon 
121252ca7ee2SPyun YongHyeon 	/* Now build perfect filters. */
121352ca7ee2SPyun YongHyeon 	mcnt = 0;
121452ca7ee2SPyun YongHyeon 	if_maddr_rlock(ifp);
121552ca7ee2SPyun YongHyeon 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
121652ca7ee2SPyun YongHyeon 		if (ifma->ifma_addr->sa_family != AF_LINK)
121752ca7ee2SPyun YongHyeon 			continue;
121852ca7ee2SPyun YongHyeon 		if (mcnt >= DC_ULI_FILTER_NPERF) {
121952ca7ee2SPyun YongHyeon 			filter |= DC_NETCFG_RX_ALLMULTI;
122052ca7ee2SPyun YongHyeon 			break;
122152ca7ee2SPyun YongHyeon 		}
122252ca7ee2SPyun YongHyeon 		ma = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
122352ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[1] << 8 | ma[0]);
122452ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[3] << 8 | ma[2]);
122552ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[5] << 8 | ma[4]);
122652ca7ee2SPyun YongHyeon 		mcnt++;
122752ca7ee2SPyun YongHyeon 	}
122852ca7ee2SPyun YongHyeon 	if_maddr_runlock(ifp);
122952ca7ee2SPyun YongHyeon 
123052ca7ee2SPyun YongHyeon 	for (; mcnt < DC_ULI_FILTER_NPERF; mcnt++) {
123152ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123252ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123352ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123452ca7ee2SPyun YongHyeon 	}
123552ca7ee2SPyun YongHyeon 
123652ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
123752ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
123852ca7ee2SPyun YongHyeon 		    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
123952ca7ee2SPyun YongHyeon 	if (ifp->if_flags & IFF_PROMISC)
124052ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI;
124152ca7ee2SPyun YongHyeon 	if (ifp->if_flags & IFF_ALLMULTI)
124252ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_ALLMULTI;
124352ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_NETCFG,
124452ca7ee2SPyun YongHyeon 	    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
124552ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
124652ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, filter);
124752ca7ee2SPyun YongHyeon 
124852ca7ee2SPyun YongHyeon 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
124952ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
125052ca7ee2SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
125152ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
125252ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
125352ca7ee2SPyun YongHyeon 
125452ca7ee2SPyun YongHyeon 	/*
125552ca7ee2SPyun YongHyeon 	 * Wait some time...
125652ca7ee2SPyun YongHyeon 	 */
125752ca7ee2SPyun YongHyeon 	DELAY(1000);
125852ca7ee2SPyun YongHyeon 
125952ca7ee2SPyun YongHyeon 	sc->dc_wdog_timer = 5;
126052ca7ee2SPyun YongHyeon }
126152ca7ee2SPyun YongHyeon 
126252ca7ee2SPyun YongHyeon static void
12630934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1264feb78939SJonathan Chen {
12658df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
12660934f18aSMaxime Henrion 	struct ifnet *ifp;
12670934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1268feb78939SJonathan Chen 	struct dc_desc *sframe;
1269ee320f98SPyun YongHyeon 	uint32_t h, *sp;
1270feb78939SJonathan Chen 	int i;
1271feb78939SJonathan Chen 
1272fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1273feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1274feb78939SJonathan Chen 
1275feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1276feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1277feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
12785f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
127956e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
12800934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1281feb78939SJonathan Chen 
12825f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1283af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1284af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1285feb78939SJonathan Chen 
128656e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1287feb78939SJonathan Chen 
1288feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1289feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1290feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1291feb78939SJonathan Chen 	else
1292feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1293feb78939SJonathan Chen 
1294feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1295feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1296feb78939SJonathan Chen 	else
1297feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1298feb78939SJonathan Chen 
1299eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
13006817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1301feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1302feb78939SJonathan Chen 			continue;
1303aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13041d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1305af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1306feb78939SJonathan Chen 	}
1307eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
1308feb78939SJonathan Chen 
1309feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1310aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1311af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1312feb78939SJonathan Chen 	}
1313feb78939SJonathan Chen 
13148df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
13158df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13168df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13178df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13188df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1319feb78939SJonathan Chen 
1320feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1321feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1322af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
13238c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
13248c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
13255f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1326feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1327feb78939SJonathan Chen 
1328feb78939SJonathan Chen 	/*
13290934f18aSMaxime Henrion 	 * Wait some time...
1330feb78939SJonathan Chen 	 */
1331feb78939SJonathan Chen 	DELAY(1000);
1332feb78939SJonathan Chen 
1333b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
1334feb78939SJonathan Chen }
1335feb78939SJonathan Chen 
1336e3d2833aSAlfred Perlstein static void
13370934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
133896f2e892SBill Paul {
13390934f18aSMaxime Henrion 
134096f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13411af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
134296f2e892SBill Paul 		dc_setfilt_21143(sc);
134396f2e892SBill Paul 
134496f2e892SBill Paul 	if (DC_IS_ASIX(sc))
134596f2e892SBill Paul 		dc_setfilt_asix(sc);
134696f2e892SBill Paul 
134796f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
134896f2e892SBill Paul 		dc_setfilt_admtek(sc);
134996f2e892SBill Paul 
135052ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
135152ca7ee2SPyun YongHyeon 		dc_setfilt_uli(sc);
135252ca7ee2SPyun YongHyeon 
1353feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1354feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
135596f2e892SBill Paul }
135696f2e892SBill Paul 
1357e3d2833aSAlfred Perlstein static void
13581da7683aSPyun YongHyeon dc_netcfg_wait(struct dc_softc *sc)
135996f2e892SBill Paul {
1360ee320f98SPyun YongHyeon 	uint32_t isr;
13611da7683aSPyun YongHyeon 	int i;
136296f2e892SBill Paul 
136396f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
136496f2e892SBill Paul 		isr = CSR_READ_4(sc, DC_ISR);
1365d467c136SBill Paul 		if (isr & DC_ISR_TX_IDLE &&
1366351267c1SMartin Blapp 		    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1367351267c1SMartin Blapp 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
136896f2e892SBill Paul 			break;
1369d467c136SBill Paul 		DELAY(10);
137096f2e892SBill Paul 	}
13717a6fab66SWarner Losh 	if (i == DC_TIMEOUT && bus_child_present(sc->dc_dev)) {
1372432120f2SMarius Strobl 		if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13736b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
13741da7683aSPyun YongHyeon 			    "%s: failed to force tx to idle state\n", __func__);
1375432120f2SMarius Strobl 		if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1376432120f2SMarius Strobl 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1377d0d67284SMarius Strobl 		    !DC_HAS_BROKEN_RXSTATE(sc))
1378432120f2SMarius Strobl 			device_printf(sc->dc_dev,
13791da7683aSPyun YongHyeon 			    "%s: failed to force rx to idle state\n", __func__);
1380432120f2SMarius Strobl 	}
138196f2e892SBill Paul }
138296f2e892SBill Paul 
13831da7683aSPyun YongHyeon /*
13841da7683aSPyun YongHyeon  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13851da7683aSPyun YongHyeon  * the netconfig register, we first have to put the transmit and/or
13861da7683aSPyun YongHyeon  * receive logic in the idle state.
13871da7683aSPyun YongHyeon  */
13881da7683aSPyun YongHyeon static void
13891da7683aSPyun YongHyeon dc_setcfg(struct dc_softc *sc, int media)
13901da7683aSPyun YongHyeon {
13911da7683aSPyun YongHyeon 	int restart = 0, watchdogreg;
13921da7683aSPyun YongHyeon 
13931da7683aSPyun YongHyeon 	if (IFM_SUBTYPE(media) == IFM_NONE)
13941da7683aSPyun YongHyeon 		return;
13951da7683aSPyun YongHyeon 
13961da7683aSPyun YongHyeon 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
13971da7683aSPyun YongHyeon 		restart = 1;
13981da7683aSPyun YongHyeon 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
13991da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
14001da7683aSPyun YongHyeon 	}
14011da7683aSPyun YongHyeon 
140296f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1403042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1404042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
140596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1406bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14070934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14088273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14098273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14108273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14114c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1412bf645417SBill Paul 			} else {
1413bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1414bf645417SBill Paul 			}
141596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141696f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
141796f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
141896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141996f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
142088d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
142196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
142296f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
142396f2e892SBill Paul 		} else {
142496f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
142596f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
142696f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
142796f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
142896f2e892SBill Paul 			}
1429318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1430318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1431318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
143296f2e892SBill Paul 		}
143396f2e892SBill Paul 	}
143496f2e892SBill Paul 
143596f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1436042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1437042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
143896f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14390934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14404c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14418273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14428273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14438273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14448273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14454c2efe27SBill Paul 			} else {
14464c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14474c2efe27SBill Paul 			}
144896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
144996f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
145096f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
145196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
145288d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
145396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
145496f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
145596f2e892SBill Paul 		} else {
145696f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
145796f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
145896f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
145996f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
146096f2e892SBill Paul 			}
146196f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1462318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14645c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14655c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14665c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14675c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14685c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14695c1cfac4SBill Paul 				else
14705c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14715c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14725c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14735c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14745c1cfac4SBill Paul 				DELAY(20000);
14755c1cfac4SBill Paul 			}
147696f2e892SBill Paul 		}
147796f2e892SBill Paul 	}
147896f2e892SBill Paul 
1479f43d9309SBill Paul 	/*
1480f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1481f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1482f43d9309SBill Paul 	 * on the external MII port.
1483f43d9309SBill Paul 	 */
1484f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
148545521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1486f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1487f43d9309SBill Paul 			sc->dc_link = 1;
1488f43d9309SBill Paul 		} else {
1489f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1490f43d9309SBill Paul 		}
1491f43d9309SBill Paul 	}
1492f43d9309SBill Paul 
149396f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
149496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149696f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149796f2e892SBill Paul 	} else {
149896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149996f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
150096f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
150196f2e892SBill Paul 	}
150296f2e892SBill Paul 
150396f2e892SBill Paul 	if (restart)
150496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
150596f2e892SBill Paul }
150696f2e892SBill Paul 
1507e3d2833aSAlfred Perlstein static void
15080934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
150996f2e892SBill Paul {
15100934f18aSMaxime Henrion 	int i;
151196f2e892SBill Paul 
151296f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
151396f2e892SBill Paul 
151496f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
151596f2e892SBill Paul 		DELAY(10);
151696f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
151796f2e892SBill Paul 			break;
151896f2e892SBill Paul 	}
151996f2e892SBill Paul 
15201af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
152152ca7ee2SPyun YongHyeon 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc) || DC_IS_ULI(sc)) {
152296f2e892SBill Paul 		DELAY(10000);
152396f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152496f2e892SBill Paul 		i = 0;
152596f2e892SBill Paul 	}
152696f2e892SBill Paul 
152796f2e892SBill Paul 	if (i == DC_TIMEOUT)
15286b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev, "reset never completed!\n");
152996f2e892SBill Paul 
153096f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
153196f2e892SBill Paul 	DELAY(1000);
153296f2e892SBill Paul 
153396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
153496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
153596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
153696f2e892SBill Paul 
153791cc2adbSBill Paul 	/*
153891cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
153991cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
154091cc2adbSBill Paul 	 * into a state where it will never come out of reset
154191cc2adbSBill Paul 	 * until we reset the whole chip again.
154291cc2adbSBill Paul 	 */
15435c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
154491cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1545d314ebf5SPyun YongHyeon 		CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFFFFFF);
15465c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15475c1cfac4SBill Paul 	}
154896f2e892SBill Paul }
154996f2e892SBill Paul 
1550ebc284ccSMarius Strobl static const struct dc_type *
15510934f18aSMaxime Henrion dc_devtype(device_t dev)
155296f2e892SBill Paul {
1553ebc284ccSMarius Strobl 	const struct dc_type *t;
1554ee320f98SPyun YongHyeon 	uint32_t devid;
1555ee320f98SPyun YongHyeon 	uint8_t rev;
155696f2e892SBill Paul 
155796f2e892SBill Paul 	t = dc_devs;
15581e2e70b1SJohn Baldwin 	devid = pci_get_devid(dev);
15591e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
156096f2e892SBill Paul 
156196f2e892SBill Paul 	while (t->dc_name != NULL) {
15621e2e70b1SJohn Baldwin 		if (devid == t->dc_devid && rev >= t->dc_minrev)
156396f2e892SBill Paul 			return (t);
156496f2e892SBill Paul 		t++;
156596f2e892SBill Paul 	}
156696f2e892SBill Paul 
156796f2e892SBill Paul 	return (NULL);
156896f2e892SBill Paul }
156996f2e892SBill Paul 
157096f2e892SBill Paul /*
157196f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
157296f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
157396f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
157496f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
157596f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
157696f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
157796f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
157896f2e892SBill Paul  */
1579e3d2833aSAlfred Perlstein static int
15800934f18aSMaxime Henrion dc_probe(device_t dev)
158196f2e892SBill Paul {
1582ebc284ccSMarius Strobl 	const struct dc_type *t;
158396f2e892SBill Paul 
158496f2e892SBill Paul 	t = dc_devtype(dev);
158596f2e892SBill Paul 
158696f2e892SBill Paul 	if (t != NULL) {
158796f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1588d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
158996f2e892SBill Paul 	}
159096f2e892SBill Paul 
159196f2e892SBill Paul 	return (ENXIO);
159296f2e892SBill Paul }
159396f2e892SBill Paul 
1594e3d2833aSAlfred Perlstein static void
15950934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
15965c1cfac4SBill Paul {
15975c1cfac4SBill Paul 	struct dc_mediainfo *m;
1598ee320f98SPyun YongHyeon 	uint8_t *p;
15995c1cfac4SBill Paul 	int i;
1600ee320f98SPyun YongHyeon 	uint32_t reg;
16015c1cfac4SBill Paul 
16025c1cfac4SBill Paul 	m = sc->dc_mi;
16035c1cfac4SBill Paul 
16045c1cfac4SBill Paul 	while (m != NULL) {
16055c1cfac4SBill Paul 		if (m->dc_media == media)
16065c1cfac4SBill Paul 			break;
16075c1cfac4SBill Paul 		m = m->dc_next;
16085c1cfac4SBill Paul 	}
16095c1cfac4SBill Paul 
16105c1cfac4SBill Paul 	if (m == NULL)
16115c1cfac4SBill Paul 		return;
16125c1cfac4SBill Paul 
16135c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16145c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16155c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16165c1cfac4SBill Paul 	}
16175c1cfac4SBill Paul 
16185c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16195c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16205c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16215c1cfac4SBill Paul 	}
16225c1cfac4SBill Paul }
16235c1cfac4SBill Paul 
1624abe4e865SPyun YongHyeon static int
16250934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16265c1cfac4SBill Paul {
16275c1cfac4SBill Paul 	struct dc_mediainfo *m;
16285c1cfac4SBill Paul 
16290934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1630abe4e865SPyun YongHyeon 	if (m == NULL) {
1631abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1632abe4e865SPyun YongHyeon 		return (ENOMEM);
1633abe4e865SPyun YongHyeon 	}
163487f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
163587f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16365c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
163787f4fa15SMartin Blapp 		break;
163887f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16395c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
164087f4fa15SMartin Blapp 		break;
164187f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16425c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
164387f4fa15SMartin Blapp 		break;
164487f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16455c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
164687f4fa15SMartin Blapp 		break;
164787f4fa15SMartin Blapp 	default:
164887f4fa15SMartin Blapp 		break;
164987f4fa15SMartin Blapp 	}
16505c1cfac4SBill Paul 
165187f4fa15SMartin Blapp 	/*
165287f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
165387f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
165487f4fa15SMartin Blapp 	 * supply Media Specific Data.
165587f4fa15SMartin Blapp 	 */
165687f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16575c1cfac4SBill Paul 		m->dc_gp_len = 2;
165887f4fa15SMartin Blapp 		m->dc_gp_ptr =
1659ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
166087f4fa15SMartin Blapp 	} else {
166187f4fa15SMartin Blapp 		m->dc_gp_len = 2;
166287f4fa15SMartin Blapp 		m->dc_gp_ptr =
1663ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
166487f4fa15SMartin Blapp 	}
16655c1cfac4SBill Paul 
16665c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16675c1cfac4SBill Paul 	sc->dc_mi = m;
16685c1cfac4SBill Paul 
16695c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
1670abe4e865SPyun YongHyeon 	return (0);
16715c1cfac4SBill Paul }
16725c1cfac4SBill Paul 
1673abe4e865SPyun YongHyeon static int
16740934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16755c1cfac4SBill Paul {
16765c1cfac4SBill Paul 	struct dc_mediainfo *m;
16775c1cfac4SBill Paul 
16780934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1679abe4e865SPyun YongHyeon 	if (m == NULL) {
1680abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1681abe4e865SPyun YongHyeon 		return (ENOMEM);
1682abe4e865SPyun YongHyeon 	}
16835c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16845c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16855c1cfac4SBill Paul 
16865c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16875c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
16885c1cfac4SBill Paul 
16895c1cfac4SBill Paul 	m->dc_gp_len = 2;
1690ee320f98SPyun YongHyeon 	m->dc_gp_ptr = (uint8_t *)&l->dc_sym_gpio_ctl;
16915c1cfac4SBill Paul 
16925c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16935c1cfac4SBill Paul 	sc->dc_mi = m;
16945c1cfac4SBill Paul 
16955c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
1696abe4e865SPyun YongHyeon 	return (0);
16975c1cfac4SBill Paul }
16985c1cfac4SBill Paul 
1699abe4e865SPyun YongHyeon static int
17000934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17015c1cfac4SBill Paul {
17025c1cfac4SBill Paul 	struct dc_mediainfo *m;
1703ee320f98SPyun YongHyeon 	uint8_t *p;
17045c1cfac4SBill Paul 
17050934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1706abe4e865SPyun YongHyeon 	if (m == NULL) {
1707abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1708abe4e865SPyun YongHyeon 		return (ENOMEM);
1709abe4e865SPyun YongHyeon 	}
17105c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17115c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17125c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17135c1cfac4SBill Paul 
1714ee320f98SPyun YongHyeon 	p = (uint8_t *)l;
17155c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17165c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17175c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17185c1cfac4SBill Paul 	m->dc_reset_len = *p;
17195c1cfac4SBill Paul 	p++;
17205c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17215c1cfac4SBill Paul 
17225c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17235c1cfac4SBill Paul 	sc->dc_mi = m;
1724abe4e865SPyun YongHyeon 	return (0);
17255c1cfac4SBill Paul }
17265c1cfac4SBill Paul 
1727abe4e865SPyun YongHyeon static int
17280934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17293097aa70SWarner Losh {
17303097aa70SWarner Losh 	int size;
17313097aa70SWarner Losh 
1732abe4e865SPyun YongHyeon 	size = DC_ROM_SIZE(bits);
173352ca7ee2SPyun YongHyeon 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
1734abe4e865SPyun YongHyeon 	if (sc->dc_srom == NULL) {
1735abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate SROM buffer\n");
1736abe4e865SPyun YongHyeon 		return (ENOMEM);
1737abe4e865SPyun YongHyeon 	}
17383097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1739abe4e865SPyun YongHyeon 	return (0);
17403097aa70SWarner Losh }
17413097aa70SWarner Losh 
1742abe4e865SPyun YongHyeon static int
17430934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17445c1cfac4SBill Paul {
17455c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17465c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
1747abe4e865SPyun YongHyeon 	int error, have_mii, i, loff;
17485c1cfac4SBill Paul 	char *ptr;
17495c1cfac4SBill Paul 
1750f956e0b3SMartin Blapp 	have_mii = 0;
17515c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17525c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17535c1cfac4SBill Paul 
17545c1cfac4SBill Paul 	ptr = (char *)lhdr;
17555c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1756f956e0b3SMartin Blapp 	/*
1757f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1758f956e0b3SMartin Blapp 	 */
1759f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1760f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1761f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1762f956e0b3SMartin Blapp 		    have_mii++;
1763f956e0b3SMartin Blapp 
1764f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1765f956e0b3SMartin Blapp 		ptr++;
1766f956e0b3SMartin Blapp 	}
1767f956e0b3SMartin Blapp 
1768f956e0b3SMartin Blapp 	/*
1769f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1770f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1771f956e0b3SMartin Blapp 	 */
1772f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1773f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1774abe4e865SPyun YongHyeon 	error = 0;
17755c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17765c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17775c1cfac4SBill Paul 		switch (hdr->dc_type) {
17785c1cfac4SBill Paul 		case DC_EBLOCK_MII:
1779abe4e865SPyun YongHyeon 			error = dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17805c1cfac4SBill Paul 			break;
17815c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1782f956e0b3SMartin Blapp 			if (! have_mii)
1783abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sia(sc,
1784f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
17855c1cfac4SBill Paul 			break;
17865c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1787f956e0b3SMartin Blapp 			if (! have_mii)
1788abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sym(sc,
1789f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
17905c1cfac4SBill Paul 			break;
17915c1cfac4SBill Paul 		default:
17925c1cfac4SBill Paul 			/* Don't care. Yet. */
17935c1cfac4SBill Paul 			break;
17945c1cfac4SBill Paul 		}
17955c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
17965c1cfac4SBill Paul 		ptr++;
17975c1cfac4SBill Paul 	}
1798abe4e865SPyun YongHyeon 	return (error);
17995c1cfac4SBill Paul }
18005c1cfac4SBill Paul 
180156e5e7aeSMaxime Henrion static void
180256e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
180356e5e7aeSMaxime Henrion {
18045f14ee23SPyun YongHyeon 	bus_addr_t *paddr;
180556e5e7aeSMaxime Henrion 
1806ebc284ccSMarius Strobl 	KASSERT(nseg == 1,
1807ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
180856e5e7aeSMaxime Henrion 	paddr = arg;
180956e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
181056e5e7aeSMaxime Henrion }
181156e5e7aeSMaxime Henrion 
18125f14ee23SPyun YongHyeon static int
18135f14ee23SPyun YongHyeon dc_dma_alloc(struct dc_softc *sc)
18145f14ee23SPyun YongHyeon {
18155f14ee23SPyun YongHyeon 	int error, i;
18165f14ee23SPyun YongHyeon 
18175f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dc_dev), 1, 0,
18185f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
18195f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
18205f14ee23SPyun YongHyeon 	    NULL, NULL, &sc->dc_ptag);
18215f14ee23SPyun YongHyeon 	if (error) {
18225f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18235f14ee23SPyun YongHyeon 		    "failed to allocate parent DMA tag\n");
18245f14ee23SPyun YongHyeon 		goto fail;
18255f14ee23SPyun YongHyeon 	}
18265f14ee23SPyun YongHyeon 
18275f14ee23SPyun YongHyeon 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
18285f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18295f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_RX_LIST_SZ, 1,
18305f14ee23SPyun YongHyeon 	    DC_RX_LIST_SZ, 0, NULL, NULL, &sc->dc_rx_ltag);
18315f14ee23SPyun YongHyeon 	if (error) {
18325f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX list DMA tag\n");
18335f14ee23SPyun YongHyeon 		goto fail;
18345f14ee23SPyun YongHyeon 	}
18355f14ee23SPyun YongHyeon 
18365f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18375f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_TX_LIST_SZ, 1,
18385f14ee23SPyun YongHyeon 	    DC_TX_LIST_SZ, 0, NULL, NULL, &sc->dc_tx_ltag);
18395f14ee23SPyun YongHyeon 	if (error) {
18405f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX list DMA tag\n");
18415f14ee23SPyun YongHyeon 		goto fail;
18425f14ee23SPyun YongHyeon 	}
18435f14ee23SPyun YongHyeon 
18445f14ee23SPyun YongHyeon 	/* RX descriptor list. */
18455f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_rx_ltag,
18465f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_rx_list, BUS_DMA_NOWAIT |
18475f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_rx_lmap);
18485f14ee23SPyun YongHyeon 	if (error) {
18495f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18505f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for RX list\n");
18515f14ee23SPyun YongHyeon 		goto fail;
18525f14ee23SPyun YongHyeon 	}
18535f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_rx_ltag, sc->dc_rx_lmap,
18545f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_rx_list, DC_RX_LIST_SZ, dc_dma_map_addr,
18555f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_rx_list_paddr, BUS_DMA_NOWAIT);
18565f14ee23SPyun YongHyeon 	if (error) {
18575f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18585f14ee23SPyun YongHyeon 		    "failed to load DMA'able memory for RX list\n");
18595f14ee23SPyun YongHyeon 		goto fail;
18605f14ee23SPyun YongHyeon 	}
18615f14ee23SPyun YongHyeon 	/* TX descriptor list. */
18625f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_tx_ltag,
18635f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_tx_list, BUS_DMA_NOWAIT |
18645f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_tx_lmap);
18655f14ee23SPyun YongHyeon 	if (error) {
18665f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18675f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for TX list\n");
18685f14ee23SPyun YongHyeon 		goto fail;
18695f14ee23SPyun YongHyeon 	}
18705f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_tx_ltag, sc->dc_tx_lmap,
18715f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_tx_list, DC_TX_LIST_SZ, dc_dma_map_addr,
18725f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_tx_list_paddr, BUS_DMA_NOWAIT);
18735f14ee23SPyun YongHyeon 	if (error) {
18745f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18755f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for TX list\n");
18765f14ee23SPyun YongHyeon 		goto fail;
18775f14ee23SPyun YongHyeon 	}
18785f14ee23SPyun YongHyeon 
18795f14ee23SPyun YongHyeon 	/*
18805f14ee23SPyun YongHyeon 	 * Allocate a busdma tag and DMA safe memory for the multicast
18815f14ee23SPyun YongHyeon 	 * setup frame.
18825f14ee23SPyun YongHyeon 	 */
18835f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18845f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
18855f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
18865f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_stag);
18875f14ee23SPyun YongHyeon 	if (error) {
18885f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18895f14ee23SPyun YongHyeon 		    "failed to create DMA tag for setup frame\n");
18905f14ee23SPyun YongHyeon 		goto fail;
18915f14ee23SPyun YongHyeon 	}
18925f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
18935f14ee23SPyun YongHyeon 	    BUS_DMA_NOWAIT, &sc->dc_smap);
18945f14ee23SPyun YongHyeon 	if (error) {
18955f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18965f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for setup frame\n");
18975f14ee23SPyun YongHyeon 		goto fail;
18985f14ee23SPyun YongHyeon 	}
18995f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
19005f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
19015f14ee23SPyun YongHyeon 	if (error) {
19025f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19035f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for setup frame\n");
19045f14ee23SPyun YongHyeon 		goto fail;
19055f14ee23SPyun YongHyeon 	}
19065f14ee23SPyun YongHyeon 
19075f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for RX mbufs. */
19085f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_RXBUF_ALIGN, 0,
19095f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19105f14ee23SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->dc_rx_mtag);
19115f14ee23SPyun YongHyeon 	if (error) {
19125f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX mbuf tag\n");
19135f14ee23SPyun YongHyeon 		goto fail;
19145f14ee23SPyun YongHyeon 	}
19155f14ee23SPyun YongHyeon 
19165f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for TX mbufs. */
19175f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, 1, 0,
19185f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19195f14ee23SPyun YongHyeon 	    MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
19205f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_tx_mtag);
19215f14ee23SPyun YongHyeon 	if (error) {
19225f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX mbuf tag\n");
19235f14ee23SPyun YongHyeon 		goto fail;
19245f14ee23SPyun YongHyeon 	}
19255f14ee23SPyun YongHyeon 
19265f14ee23SPyun YongHyeon 	/* Create the TX/RX busdma maps. */
19275f14ee23SPyun YongHyeon 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
19285f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_tx_mtag, 0,
19295f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_tx_map[i]);
19305f14ee23SPyun YongHyeon 		if (error) {
19315f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19325f14ee23SPyun YongHyeon 			    "failed to create TX mbuf dmamap\n");
19335f14ee23SPyun YongHyeon 			goto fail;
19345f14ee23SPyun YongHyeon 		}
19355f14ee23SPyun YongHyeon 	}
19365f14ee23SPyun YongHyeon 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
19375f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_rx_mtag, 0,
19385f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_rx_map[i]);
19395f14ee23SPyun YongHyeon 		if (error) {
19405f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19415f14ee23SPyun YongHyeon 			    "failed to create RX mbuf dmamap\n");
19425f14ee23SPyun YongHyeon 			goto fail;
19435f14ee23SPyun YongHyeon 		}
19445f14ee23SPyun YongHyeon 	}
19455f14ee23SPyun YongHyeon 	error = bus_dmamap_create(sc->dc_rx_mtag, 0, &sc->dc_sparemap);
19465f14ee23SPyun YongHyeon 	if (error) {
19475f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19485f14ee23SPyun YongHyeon 		    "failed to create spare RX mbuf dmamap\n");
19495f14ee23SPyun YongHyeon 		goto fail;
19505f14ee23SPyun YongHyeon 	}
19515f14ee23SPyun YongHyeon 
19525f14ee23SPyun YongHyeon fail:
19535f14ee23SPyun YongHyeon 	return (error);
19545f14ee23SPyun YongHyeon }
19555f14ee23SPyun YongHyeon 
19565f14ee23SPyun YongHyeon static void
19575f14ee23SPyun YongHyeon dc_dma_free(struct dc_softc *sc)
19585f14ee23SPyun YongHyeon {
19595f14ee23SPyun YongHyeon 	int i;
19605f14ee23SPyun YongHyeon 
19615f14ee23SPyun YongHyeon 	/* RX buffers. */
19625f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19635f14ee23SPyun YongHyeon 		for (i = 0; i < DC_RX_LIST_CNT; i++) {
19645f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
19655f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_rx_mtag,
19665f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_rx_map[i]);
19675f14ee23SPyun YongHyeon 		}
19685f14ee23SPyun YongHyeon 		if (sc->dc_sparemap != NULL)
19695f14ee23SPyun YongHyeon 			bus_dmamap_destroy(sc->dc_rx_mtag, sc->dc_sparemap);
19705f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_mtag);
19715f14ee23SPyun YongHyeon 	}
19725f14ee23SPyun YongHyeon 
19735f14ee23SPyun YongHyeon 	/* TX buffers. */
19745f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19755f14ee23SPyun YongHyeon 		for (i = 0; i < DC_TX_LIST_CNT; i++) {
19765f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
19775f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_tx_mtag,
19785f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_tx_map[i]);
19795f14ee23SPyun YongHyeon 		}
19805f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_mtag);
19815f14ee23SPyun YongHyeon 	}
19825f14ee23SPyun YongHyeon 
19835f14ee23SPyun YongHyeon 	/* RX descriptor list. */
19845f14ee23SPyun YongHyeon 	if (sc->dc_rx_ltag) {
1985068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_rx_list_paddr != 0)
19865f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_ltag, sc->dc_rx_lmap);
1987068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_rx_list != NULL)
19885f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_rx_ltag, sc->dc_ldata.dc_rx_list,
19895f14ee23SPyun YongHyeon 			    sc->dc_rx_lmap);
19905f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_ltag);
19915f14ee23SPyun YongHyeon 	}
19925f14ee23SPyun YongHyeon 
19935f14ee23SPyun YongHyeon 	/* TX descriptor list. */
19945f14ee23SPyun YongHyeon 	if (sc->dc_tx_ltag) {
1995068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_tx_list_paddr != 0)
19965f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_ltag, sc->dc_tx_lmap);
1997068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_tx_list != NULL)
19985f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_tx_ltag, sc->dc_ldata.dc_tx_list,
19995f14ee23SPyun YongHyeon 			    sc->dc_tx_lmap);
20005f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_ltag);
20015f14ee23SPyun YongHyeon 	}
20025f14ee23SPyun YongHyeon 
20035f14ee23SPyun YongHyeon 	/* multicast setup frame. */
20045f14ee23SPyun YongHyeon 	if (sc->dc_stag) {
2005068d8643SJohn Baldwin 		if (sc->dc_saddr != 0)
20065f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_stag, sc->dc_smap);
2007068d8643SJohn Baldwin 		if (sc->dc_cdata.dc_sbuf != NULL)
20085f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf,
20095f14ee23SPyun YongHyeon 			    sc->dc_smap);
20105f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_stag);
20115f14ee23SPyun YongHyeon 	}
20125f14ee23SPyun YongHyeon }
20135f14ee23SPyun YongHyeon 
201496f2e892SBill Paul /*
201596f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
201696f2e892SBill Paul  * setup and ethernet/BPF attach.
201796f2e892SBill Paul  */
2018e3d2833aSAlfred Perlstein static int
20190934f18aSMaxime Henrion dc_attach(device_t dev)
202096f2e892SBill Paul {
20218df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
2022ee320f98SPyun YongHyeon 	uint32_t command;
202396f2e892SBill Paul 	struct dc_softc *sc;
202496f2e892SBill Paul 	struct ifnet *ifp;
2025b289c607SPyun YongHyeon 	struct dc_mediainfo *m;
2026ee320f98SPyun YongHyeon 	uint32_t reg, revision;
202752ca7ee2SPyun YongHyeon 	uint16_t *srom;
202852ca7ee2SPyun YongHyeon 	int error, mac_offset, n, phy, rid, tmp;
2029ee320f98SPyun YongHyeon 	uint8_t *mac;
203096f2e892SBill Paul 
203196f2e892SBill Paul 	sc = device_get_softc(dev);
20326b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
203396f2e892SBill Paul 
20346008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2035c8b27acaSJohn Baldwin 	    MTX_DEF);
2036c3e7434fSWarner Losh 
203796f2e892SBill Paul 	/*
203896f2e892SBill Paul 	 * Map control/status registers.
203996f2e892SBill Paul 	 */
204007f65363SBill Paul 	pci_enable_busmaster(dev);
204196f2e892SBill Paul 
204296f2e892SBill Paul 	rid = DC_RID;
20435f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
204496f2e892SBill Paul 
204596f2e892SBill Paul 	if (sc->dc_res == NULL) {
204622f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
204796f2e892SBill Paul 		error = ENXIO;
2048608654d4SNate Lawson 		goto fail;
204996f2e892SBill Paul 	}
205096f2e892SBill Paul 
205196f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
205296f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
205396f2e892SBill Paul 
20540934f18aSMaxime Henrion 	/* Allocate interrupt. */
205554f1f1d1SNate Lawson 	rid = 0;
20565f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
205754f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
205854f1f1d1SNate Lawson 
205954f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
206022f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
206154f1f1d1SNate Lawson 		error = ENXIO;
206254f1f1d1SNate Lawson 		goto fail;
206354f1f1d1SNate Lawson 	}
206454f1f1d1SNate Lawson 
206596f2e892SBill Paul 	/* Need this info to decide on a chip type. */
206696f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
20671e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
206896f2e892SBill Paul 
2069abe4e865SPyun YongHyeon 	error = 0;
20706d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
20711e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
20721e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
20731e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
20741e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
2075eecb3844SMartin Blapp 		dc_eeprom_width(sc);
2076eecb3844SMartin Blapp 
20771e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
20781e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
207996f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
208096f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2081042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
20825c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
2083abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2084abe4e865SPyun YongHyeon 		if (error != 0)
2085abe4e865SPyun YongHyeon 			goto fail;
208696f2e892SBill Paul 		break;
20871e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
20881e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
20891e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
209096f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
2091318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
2092318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
20937dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
20944a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
20951e2e70b1SJohn Baldwin 
20960a46b1dcSBill Paul 		/* Increase the latency timer value. */
20971e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
209896f2e892SBill Paul 		break;
20991e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
210096f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
210196f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
210296f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
210396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2104abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2105abe4e865SPyun YongHyeon 		if (error != 0)
2106abe4e865SPyun YongHyeon 			goto fail;
210796f2e892SBill Paul 		break;
2108593a1aeaSMartin Blapp 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983):
21091e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
21101e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
21111e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
21121e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
21131e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
21141e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
21151e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
21161e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
21171e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
21181e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
21191e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
21201e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
212117762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
212217762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
2123593a1aeaSMartin Blapp 		sc->dc_type = DC_TYPE_AN983;
2124acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
212596f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
212696f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
212796f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2128129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
212996f2e892SBill Paul 		break;
21301e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
21311e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
213296f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
213396f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
213496f2e892SBill Paul 		}
2135318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
213696f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
2137318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
2138318b02fdSBill Paul 		}
2139318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
214096f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
214196f2e892SBill Paul 		break;
21421e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
21431e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
214479d11e09SBill Paul 		/*
214579d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
214679d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
214779d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
214879d11e09SBill Paul 		 * get the right number of bits out of the
214979d11e09SBill Paul 		 * CRC routine.
215079d11e09SBill Paul 		 */
215179d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
215279d11e09SBill Paul 		    revision < DC_REVISION_98725)
215379d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
215496f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
215596f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2156318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
215796f2e892SBill Paul 		break;
21581e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
2159ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
2160ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2161ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2162ead7cde9SBill Paul 		break;
21631e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
216496f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
216579d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
2166318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
216796f2e892SBill Paul 		break;
21681e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
216996f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
217091cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
217196f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
217296f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
2173abe4e865SPyun YongHyeon 		if (sc->dc_pnic_rx_buf == NULL) {
2174abe4e865SPyun YongHyeon 			device_printf(sc->dc_dev,
2175abe4e865SPyun YongHyeon 			    "Could not allocate PNIC RX buffer\n");
2176abe4e865SPyun YongHyeon 			error = ENOMEM;
2177abe4e865SPyun YongHyeon 			goto fail;
2178abe4e865SPyun YongHyeon 		}
217996f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
218096f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
218196f2e892SBill Paul 		break;
21821e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
218396f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
218496f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
218596f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
218696f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
218796f2e892SBill Paul 		break;
21881e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
2189feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
21902dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
21912dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2192feb78939SJonathan Chen 		/*
2193feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2194feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
21952dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2196feb78939SJonathan Chen 		 */
21973097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2198feb78939SJonathan Chen 		break;
21991e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
22001af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
22011af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
22021af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
22031af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2204abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2205abe4e865SPyun YongHyeon 		if (error != 0)
2206abe4e865SPyun YongHyeon 			goto fail;
22071af8bec7SBill Paul 		break;
220852ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261):
220952ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263):
221052ca7ee2SPyun YongHyeon 		if (sc->dc_info->dc_devid ==
221152ca7ee2SPyun YongHyeon 		    DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261))
221252ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5261;
221352ca7ee2SPyun YongHyeon 		else
221452ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5263;
221552ca7ee2SPyun YongHyeon 		/* TX buffers should be aligned on 4 byte boundary. */
221652ca7ee2SPyun YongHyeon 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
221752ca7ee2SPyun YongHyeon 		    DC_TX_ALIGN;
221852ca7ee2SPyun YongHyeon 		sc->dc_pmode = DC_PMODE_MII;
221952ca7ee2SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
222052ca7ee2SPyun YongHyeon 		if (error != 0)
222152ca7ee2SPyun YongHyeon 			goto fail;
222252ca7ee2SPyun YongHyeon 		break;
222396f2e892SBill Paul 	default:
22241e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
22251e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
222696f2e892SBill Paul 		break;
222796f2e892SBill Paul 	}
222896f2e892SBill Paul 
222996f2e892SBill Paul 	/* Save the cache line size. */
223088d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
223188d739dcSBill Paul 		sc->dc_cachesize = 0;
223288d739dcSBill Paul 	else
22331e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
223496f2e892SBill Paul 
223596f2e892SBill Paul 	/* Reset the adapter. */
223696f2e892SBill Paul 	dc_reset(sc);
223796f2e892SBill Paul 
223896f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2239feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
224096f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
224196f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
224296f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
224396f2e892SBill Paul 	}
224496f2e892SBill Paul 
224596f2e892SBill Paul 	/*
224696f2e892SBill Paul 	 * Try to learn something about the supported media.
224796f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
224896f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
224996f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
225096f2e892SBill Paul 	 * Intel 21143.
225196f2e892SBill Paul 	 */
2252abe4e865SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
2253abe4e865SPyun YongHyeon 		error = dc_parse_21143_srom(sc);
2254abe4e865SPyun YongHyeon 		if (error != 0)
2255abe4e865SPyun YongHyeon 			goto fail;
2256abe4e865SPyun YongHyeon 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
225796f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
225896f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
225996f2e892SBill Paul 		else
226096f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
226196f2e892SBill Paul 	} else if (!sc->dc_pmode)
226296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
226396f2e892SBill Paul 
226496f2e892SBill Paul 	/*
226596f2e892SBill Paul 	 * Get station address from the EEPROM.
226696f2e892SBill Paul 	 */
226796f2e892SBill Paul 	switch(sc->dc_type) {
226896f2e892SBill Paul 	case DC_TYPE_98713:
226996f2e892SBill Paul 	case DC_TYPE_98713A:
227096f2e892SBill Paul 	case DC_TYPE_987x5:
227196f2e892SBill Paul 	case DC_TYPE_PNICII:
227296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
227396f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
227496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
227596f2e892SBill Paul 		break;
227696f2e892SBill Paul 	case DC_TYPE_PNIC:
227796f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
227896f2e892SBill Paul 		break;
227996f2e892SBill Paul 	case DC_TYPE_DM9102:
2280ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2281ec6a7299SMaxime Henrion #ifdef __sparc64__
2282ec6a7299SMaxime Henrion 		/*
2283ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2284802cab03SMarius Strobl 		 * the EEPROM is all zero and we have to get it from the FCode.
2285ec6a7299SMaxime Henrion 		 */
2286802cab03SMarius Strobl 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
22878069c79dSRuslan Ermilov 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2288ec6a7299SMaxime Henrion #endif
2289ec6a7299SMaxime Henrion 		break;
229096f2e892SBill Paul 	case DC_TYPE_21143:
229196f2e892SBill Paul 	case DC_TYPE_ASIX:
229296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
229396f2e892SBill Paul 		break;
229496f2e892SBill Paul 	case DC_TYPE_AL981:
2295593a1aeaSMartin Blapp 	case DC_TYPE_AN983:
22962e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR0);
22972e3d4b79SPyun YongHyeon 		mac = (uint8_t *)&eaddr[0];
22982e3d4b79SPyun YongHyeon 		mac[0] = (reg >> 0) & 0xff;
22992e3d4b79SPyun YongHyeon 		mac[1] = (reg >> 8) & 0xff;
23002e3d4b79SPyun YongHyeon 		mac[2] = (reg >> 16) & 0xff;
23012e3d4b79SPyun YongHyeon 		mac[3] = (reg >> 24) & 0xff;
23022e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR1);
23032e3d4b79SPyun YongHyeon 		mac[4] = (reg >> 0) & 0xff;
23042e3d4b79SPyun YongHyeon 		mac[5] = (reg >> 8) & 0xff;
230596f2e892SBill Paul 		break;
23061af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
23070934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
23080934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
23091af8bec7SBill Paul 		break;
2310feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
23110934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2312e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2313e7b01d07SWarner Losh 		if (!mac) {
2314e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2315608654d4SNate Lawson 			error = ENXIO;
2316e7b01d07SWarner Losh 			goto fail;
2317e7b01d07SWarner Losh 		}
2318e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2319feb78939SJonathan Chen 		break;
232052ca7ee2SPyun YongHyeon 	case DC_TYPE_ULI_M5261:
232152ca7ee2SPyun YongHyeon 	case DC_TYPE_ULI_M5263:
232252ca7ee2SPyun YongHyeon 		srom = (uint16_t *)sc->dc_srom;
232352ca7ee2SPyun YongHyeon 		if (srom == NULL || *srom == 0xFFFF || *srom == 0) {
232452ca7ee2SPyun YongHyeon 			/*
232552ca7ee2SPyun YongHyeon 			 * No valid SROM present, read station address
232652ca7ee2SPyun YongHyeon 			 * from ID Table.
232752ca7ee2SPyun YongHyeon 			 */
232852ca7ee2SPyun YongHyeon 			device_printf(dev,
232952ca7ee2SPyun YongHyeon 			    "Reading station address from ID Table.\n");
233052ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_BUSCTL, 0x10000);
233152ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x01C0);
233252ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0000);
233352ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0010);
233452ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0000);
233552ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x0000);
233652ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x01B0);
233752ca7ee2SPyun YongHyeon 			mac = (uint8_t *)eaddr;
233852ca7ee2SPyun YongHyeon 			for (n = 0; n < ETHER_ADDR_LEN; n++)
233952ca7ee2SPyun YongHyeon 				mac[n] = (uint8_t)CSR_READ_4(sc, DC_10BTCTRL);
234052ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x0000);
234152ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_BUSCTL, 0x0000);
234252ca7ee2SPyun YongHyeon 			DELAY(10);
234352ca7ee2SPyun YongHyeon 		} else
234452ca7ee2SPyun YongHyeon 			dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3,
234552ca7ee2SPyun YongHyeon 			    0);
234652ca7ee2SPyun YongHyeon 		break;
234796f2e892SBill Paul 	default:
234896f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
234996f2e892SBill Paul 		break;
235096f2e892SBill Paul 	}
235196f2e892SBill Paul 
235239d76ed6SPyun YongHyeon 	bcopy(eaddr, sc->dc_eaddr, sizeof(eaddr));
235339d76ed6SPyun YongHyeon 	/*
235439d76ed6SPyun YongHyeon 	 * If we still have invalid station address, see whether we can
235539d76ed6SPyun YongHyeon 	 * find station address for chip 0.  Some multi-port controllers
235639d76ed6SPyun YongHyeon 	 * just store station address for chip 0 if they have a shared
235739d76ed6SPyun YongHyeon 	 * SROM.
235839d76ed6SPyun YongHyeon 	 */
235939d76ed6SPyun YongHyeon 	if ((sc->dc_eaddr[0] == 0 && (sc->dc_eaddr[1] & ~0xffff) == 0) ||
236039d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[0] == 0xffffffff &&
236139d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[1] & 0xffff) == 0xffff)) {
2362b289c607SPyun YongHyeon 		error = dc_check_multiport(sc);
2363b289c607SPyun YongHyeon 		if (error == 0) {
236439d76ed6SPyun YongHyeon 			bcopy(sc->dc_eaddr, eaddr, sizeof(eaddr));
2365b289c607SPyun YongHyeon 			/* Extract media information. */
2366b289c607SPyun YongHyeon 			if (DC_IS_INTEL(sc) && sc->dc_srom != NULL) {
2367b289c607SPyun YongHyeon 				while (sc->dc_mi != NULL) {
2368b289c607SPyun YongHyeon 					m = sc->dc_mi->dc_next;
2369b289c607SPyun YongHyeon 					free(sc->dc_mi, M_DEVBUF);
2370b289c607SPyun YongHyeon 					sc->dc_mi = m;
2371b289c607SPyun YongHyeon 				}
2372b289c607SPyun YongHyeon 				error = dc_parse_21143_srom(sc);
2373b289c607SPyun YongHyeon 				if (error != 0)
2374b289c607SPyun YongHyeon 					goto fail;
2375b289c607SPyun YongHyeon 			}
2376b289c607SPyun YongHyeon 		} else if (error == ENOMEM)
2377b289c607SPyun YongHyeon 			goto fail;
2378b289c607SPyun YongHyeon 		else
2379b289c607SPyun YongHyeon 			error = 0;
238039d76ed6SPyun YongHyeon 	}
238139d76ed6SPyun YongHyeon 
23825f14ee23SPyun YongHyeon 	if ((error = dc_dma_alloc(sc)) != 0)
238356e5e7aeSMaxime Henrion 		goto fail;
238496f2e892SBill Paul 
2385fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2386fc74a9f9SBrooks Davis 	if (ifp == NULL) {
238722f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2388fc74a9f9SBrooks Davis 		error = ENOSPC;
2389fc74a9f9SBrooks Davis 		goto fail;
2390fc74a9f9SBrooks Davis 	}
239196f2e892SBill Paul 	ifp->if_softc = sc;
23929bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
23933d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
239496f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
239596f2e892SBill Paul 	ifp->if_start = dc_start;
239696f2e892SBill Paul 	ifp->if_init = dc_init;
2397cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2398cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2399cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
240096f2e892SBill Paul 
240196f2e892SBill Paul 	/*
24025c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
24035c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
24045c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
24055c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
24065c1cfac4SBill Paul 	 * driver instead.
240796f2e892SBill Paul 	 */
24088e5d93dbSMarius Strobl 	tmp = 0;
24095c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
24105c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
24115c1cfac4SBill Paul 		tmp = sc->dc_pmode;
24125c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
24135c1cfac4SBill Paul 	}
24145c1cfac4SBill Paul 
24156d431b17SWarner Losh 	/*
24166d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
24178e5d93dbSMarius Strobl 	 * to the MII.  This needs to be done before mii_attach so that
24186d431b17SWarner Losh 	 * we can actually see them.
24196d431b17SWarner Losh 	 */
24206d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
24216d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
24226d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24236d431b17SWarner Losh 		DELAY(10);
24246d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
24256d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24266d431b17SWarner Losh 		DELAY(10);
24276d431b17SWarner Losh 	}
24286d431b17SWarner Losh 
24298e5d93dbSMarius Strobl 	phy = MII_PHY_ANY;
24308e5d93dbSMarius Strobl 	/*
24318e5d93dbSMarius Strobl 	 * Note: both the AL981 and AN983 have internal PHYs, however the
24328e5d93dbSMarius Strobl 	 * AL981 provides direct access to the PHY registers while the AN983
24338e5d93dbSMarius Strobl 	 * uses a serial MII interface. The AN983's MII interface is also
24348e5d93dbSMarius Strobl 	 * buggy in that you can read from any MII address (0 to 31), but
24358e5d93dbSMarius Strobl 	 * only address 1 behaves normally. To deal with both cases, we
24368e5d93dbSMarius Strobl 	 * pretend that the PHY is at MII address 1.
24378e5d93dbSMarius Strobl 	 */
24388e5d93dbSMarius Strobl 	if (DC_IS_ADMTEK(sc))
24398e5d93dbSMarius Strobl 		phy = DC_ADMTEK_PHYADDR;
24408e5d93dbSMarius Strobl 
24418e5d93dbSMarius Strobl 	/*
24428e5d93dbSMarius Strobl 	 * Note: the ukphy probes of the RS7112 report a PHY at MII address
24438e5d93dbSMarius Strobl 	 * 0 (possibly HomePNA?) and 1 (ethernet) so we only respond to the
24448e5d93dbSMarius Strobl 	 * correct one.
24458e5d93dbSMarius Strobl 	 */
24468e5d93dbSMarius Strobl 	if (DC_IS_CONEXANT(sc))
24478e5d93dbSMarius Strobl 		phy = DC_CONEXANT_PHYADDR;
24488e5d93dbSMarius Strobl 
24498e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24508e5d93dbSMarius Strobl 	    dc_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
245196f2e892SBill Paul 
245296f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
24535c1cfac4SBill Paul 		sc->dc_pmode = tmp;
24545c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
245596f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2456042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
245778999dd1SBill Paul 		/*
245878999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
245978999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
246078999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
246178999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
246278999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
246378999dd1SBill Paul 		 */
24641e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
24651e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
246678999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
2467166e31d9SMarius Strobl 		error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
2468166e31d9SMarius Strobl 		    dc_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
2469166e31d9SMarius Strobl 		    MII_OFFSET_ANY, 0);
247096f2e892SBill Paul 	}
247196f2e892SBill Paul 
247296f2e892SBill Paul 	if (error) {
24738e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
247496f2e892SBill Paul 		goto fail;
247596f2e892SBill Paul 	}
247696f2e892SBill Paul 
2477028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2478028a8491SMartin Blapp 		/*
2479028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2480028a8491SMartin Blapp 		 */
2481028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2482028a8491SMartin Blapp 	}
2483028a8491SMartin Blapp 
248496f2e892SBill Paul 	/*
2485db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2486db40c1aeSDoug Ambrisko 	 */
24871bffa951SGleb Smirnoff 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
24889ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
248940929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2490e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2491e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2492e695984eSRuslan Ermilov #endif
2493db40c1aeSDoug Ambrisko 
2494c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2495b1d16143SMarius Strobl 	callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
249696f2e892SBill Paul 
2497608654d4SNate Lawson 	/*
2498608654d4SNate Lawson 	 * Call MI attach routine.
2499608654d4SNate Lawson 	 */
25008df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2501608654d4SNate Lawson 
250254f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2503c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2504ef544f63SPaolo Pisati 	    NULL, dc_intr, sc, &sc->dc_intrhand);
2505608654d4SNate Lawson 
2506608654d4SNate Lawson 	if (error) {
250722f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2508693f4477SNate Lawson 		ether_ifdetach(ifp);
250954f1f1d1SNate Lawson 		goto fail;
2510608654d4SNate Lawson 	}
2511510a809eSMike Smith 
251296f2e892SBill Paul fail:
251354f1f1d1SNate Lawson 	if (error)
251454f1f1d1SNate Lawson 		dc_detach(dev);
251596f2e892SBill Paul 	return (error);
251696f2e892SBill Paul }
251796f2e892SBill Paul 
2518693f4477SNate Lawson /*
2519693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2520693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2521693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2522693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2523693f4477SNate Lawson  * allocated.
2524693f4477SNate Lawson  */
2525e3d2833aSAlfred Perlstein static int
25260934f18aSMaxime Henrion dc_detach(device_t dev)
252796f2e892SBill Paul {
252896f2e892SBill Paul 	struct dc_softc *sc;
252996f2e892SBill Paul 	struct ifnet *ifp;
25305c1cfac4SBill Paul 	struct dc_mediainfo *m;
253196f2e892SBill Paul 
253296f2e892SBill Paul 	sc = device_get_softc(dev);
253359f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2534d1ce9105SBill Paul 
2535fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
253696f2e892SBill Paul 
253740929967SGleb Smirnoff #ifdef DEVICE_POLLING
2538166e31d9SMarius Strobl 	if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
253940929967SGleb Smirnoff 		ether_poll_deregister(ifp);
254040929967SGleb Smirnoff #endif
254140929967SGleb Smirnoff 
2542693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2543214073e5SWarner Losh 	if (device_is_attached(dev)) {
2544c8b27acaSJohn Baldwin 		DC_LOCK(sc);
254596f2e892SBill Paul 		dc_stop(sc);
2546c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2547c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
2548b1d16143SMarius Strobl 		callout_drain(&sc->dc_wdog_ch);
25499ef8b520SSam Leffler 		ether_ifdetach(ifp);
2550693f4477SNate Lawson 	}
2551693f4477SNate Lawson 	if (sc->dc_miibus)
255296f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
255354f1f1d1SNate Lawson 	bus_generic_detach(dev);
255496f2e892SBill Paul 
255554f1f1d1SNate Lawson 	if (sc->dc_intrhand)
255696f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
255754f1f1d1SNate Lawson 	if (sc->dc_irq)
255896f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
255954f1f1d1SNate Lawson 	if (sc->dc_res)
256096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
256196f2e892SBill Paul 
2562166e31d9SMarius Strobl 	if (ifp != NULL)
25636a3033a8SWarner Losh 		if_free(ifp);
25646a3033a8SWarner Losh 
25655f14ee23SPyun YongHyeon 	dc_dma_free(sc);
256656e5e7aeSMaxime Henrion 
256796f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
256896f2e892SBill Paul 
25695c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
25705c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
25715c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
25725c1cfac4SBill Paul 		sc->dc_mi = m;
25735c1cfac4SBill Paul 	}
25747efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
25755c1cfac4SBill Paul 
2576d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
257796f2e892SBill Paul 
257896f2e892SBill Paul 	return (0);
257996f2e892SBill Paul }
258096f2e892SBill Paul 
258196f2e892SBill Paul /*
258296f2e892SBill Paul  * Initialize the transmit descriptors.
258396f2e892SBill Paul  */
2584e3d2833aSAlfred Perlstein static int
25850934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
258696f2e892SBill Paul {
258796f2e892SBill Paul 	struct dc_chain_data *cd;
258896f2e892SBill Paul 	struct dc_list_data *ld;
258901faf54bSLuigi Rizzo 	int i, nexti;
259096f2e892SBill Paul 
259196f2e892SBill Paul 	cd = &sc->dc_cdata;
25925f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
259396f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2594b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2595b3811c95SMaxime Henrion 			nexti = 0;
2596b3811c95SMaxime Henrion 		else
2597b3811c95SMaxime Henrion 			nexti = i + 1;
259852c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_status = 0;
259952c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_ctl = 0;
260052c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_data = 0;
2601af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
260296f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
260396f2e892SBill Paul 	}
260496f2e892SBill Paul 
260596f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
260606d23883SPyun YongHyeon 	cd->dc_tx_pkts = 0;
26075f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
260856e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
260996f2e892SBill Paul 	return (0);
261096f2e892SBill Paul }
261196f2e892SBill Paul 
261296f2e892SBill Paul /*
261396f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
261496f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
261596f2e892SBill Paul  * points back to the first.
261696f2e892SBill Paul  */
2617e3d2833aSAlfred Perlstein static int
26180934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
261996f2e892SBill Paul {
262096f2e892SBill Paul 	struct dc_chain_data *cd;
262196f2e892SBill Paul 	struct dc_list_data *ld;
262201faf54bSLuigi Rizzo 	int i, nexti;
262396f2e892SBill Paul 
262496f2e892SBill Paul 	cd = &sc->dc_cdata;
26255f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
262696f2e892SBill Paul 
262796f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
26285f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0)
262996f2e892SBill Paul 			return (ENOBUFS);
2630b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2631b3811c95SMaxime Henrion 			nexti = 0;
2632b3811c95SMaxime Henrion 		else
2633b3811c95SMaxime Henrion 			nexti = i + 1;
2634af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
263596f2e892SBill Paul 	}
263696f2e892SBill Paul 
263796f2e892SBill Paul 	cd->dc_rx_prod = 0;
26385f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
263956e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
264096f2e892SBill Paul 	return (0);
264196f2e892SBill Paul }
264296f2e892SBill Paul 
264396f2e892SBill Paul /*
264496f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
264596f2e892SBill Paul  */
2646e3d2833aSAlfred Perlstein static int
26475f14ee23SPyun YongHyeon dc_newbuf(struct dc_softc *sc, int i)
264896f2e892SBill Paul {
26495f14ee23SPyun YongHyeon 	struct mbuf *m;
26505f14ee23SPyun YongHyeon 	bus_dmamap_t map;
265182a67a70SMarius Strobl 	bus_dma_segment_t segs[1];
265282a67a70SMarius Strobl 	int error, nseg;
265396f2e892SBill Paul 
2654c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
26555f14ee23SPyun YongHyeon 	if (m == NULL)
265696f2e892SBill Paul 		return (ENOBUFS);
26575f14ee23SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
26585f14ee23SPyun YongHyeon 	m_adj(m, sizeof(u_int64_t));
265996f2e892SBill Paul 
266096f2e892SBill Paul 	/*
266196f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
266296f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
266396f2e892SBill Paul 	 * 82c169 chips.
266496f2e892SBill Paul 	 */
266596f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
26665f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
266796f2e892SBill Paul 
26685f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_rx_mtag, sc->dc_sparemap,
26695f14ee23SPyun YongHyeon 	    m, segs, &nseg, 0);
267056e5e7aeSMaxime Henrion 	if (error) {
26715f14ee23SPyun YongHyeon 		m_freem(m);
267256e5e7aeSMaxime Henrion 		return (error);
267356e5e7aeSMaxime Henrion 	}
26745f14ee23SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: wrong number of segments (%d)", __func__,
26755f14ee23SPyun YongHyeon 	    nseg));
26765f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_rx_chain[i] != NULL)
26775f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i]);
267896f2e892SBill Paul 
26795f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_rx_map[i];
26805f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
26815f14ee23SPyun YongHyeon 	sc->dc_sparemap = map;
26825f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_chain[i] = m;
26835f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
268456e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
26855f14ee23SPyun YongHyeon 
26865f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
26875f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_data =
26885f14ee23SPyun YongHyeon 	    htole32(DC_ADDR_LO(segs[0].ds_addr));
26895f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
26905f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
269156e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
269296f2e892SBill Paul 	return (0);
269396f2e892SBill Paul }
269496f2e892SBill Paul 
269596f2e892SBill Paul /*
269696f2e892SBill Paul  * Grrrrr.
269796f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
269896f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
269996f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
270096f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
270196f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
270296f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
270396f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
270496f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
270596f2e892SBill Paul  *
270696f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
270796f2e892SBill Paul  * Here's what we know:
270896f2e892SBill Paul  *
270996f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
271096f2e892SBill Paul  *   descriptors uploaded.
271196f2e892SBill Paul  *
271296f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
271396f2e892SBill Paul  *   total data upload.
271496f2e892SBill Paul  *
271596f2e892SBill Paul  * - We know the size of the desired received frame because it will be
271696f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
271796f2e892SBill Paul  *
271896f2e892SBill Paul  * Here's what we do:
271996f2e892SBill Paul  *
272096f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
272196f2e892SBill Paul  *   This means that we know that the buffer contents should be all
272296f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
272396f2e892SBill Paul  *
272496f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
272596f2e892SBill Paul  *   ethernet CRC at the end.
272696f2e892SBill Paul  *
272796f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
272896f2e892SBill Paul  *
272996f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
273096f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
273196f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
273296f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
273396f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
273496f2e892SBill Paul  *   we won't be fooled.
273596f2e892SBill Paul  *
273696f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
273796f2e892SBill Paul  *   that value from the current pointer location. This brings us
273896f2e892SBill Paul  *   to the start of the actual received packet.
273996f2e892SBill Paul  *
274096f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
274196f2e892SBill Paul  *   frame length.
274296f2e892SBill Paul  *
274396f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
274496f2e892SBill Paul  * the time.
274596f2e892SBill Paul  */
274696f2e892SBill Paul 
274796f2e892SBill Paul #define	DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2748e3d2833aSAlfred Perlstein static void
27490934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
275096f2e892SBill Paul {
275196f2e892SBill Paul 	struct dc_desc *cur_rx;
275296f2e892SBill Paul 	struct dc_desc *c = NULL;
275396f2e892SBill Paul 	struct mbuf *m = NULL;
275496f2e892SBill Paul 	unsigned char *ptr;
275596f2e892SBill Paul 	int i, total_len;
2756ee320f98SPyun YongHyeon 	uint32_t rxstat = 0;
275796f2e892SBill Paul 
275896f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
27595f14ee23SPyun YongHyeon 	cur_rx = &sc->dc_ldata.dc_rx_list[idx];
276096f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
27611edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
276296f2e892SBill Paul 
276396f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
276496f2e892SBill Paul 	while (1) {
27655f14ee23SPyun YongHyeon 		c = &sc->dc_ldata.dc_rx_list[i];
2766af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
276796f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
276896f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
276996f2e892SBill Paul 		ptr += DC_RXLEN;
277096f2e892SBill Paul 		/* If this is the last buffer, break out. */
277196f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
277296f2e892SBill Paul 			break;
27735f14ee23SPyun YongHyeon 		dc_discard_rxbuf(sc, i);
277496f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
277596f2e892SBill Paul 	}
277696f2e892SBill Paul 
277796f2e892SBill Paul 	/* Find the length of the actual receive frame. */
277896f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
277996f2e892SBill Paul 
278096f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
278196f2e892SBill Paul 	while (*ptr == 0x00)
278296f2e892SBill Paul 		ptr--;
278396f2e892SBill Paul 
278496f2e892SBill Paul 	/* Round off. */
278596f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
278696f2e892SBill Paul 		ptr -= 1;
278796f2e892SBill Paul 
278896f2e892SBill Paul 	/* Now find the start of the frame. */
278996f2e892SBill Paul 	ptr -= total_len;
279096f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
279196f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
279296f2e892SBill Paul 
279396f2e892SBill Paul 	/*
279496f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
279596f2e892SBill Paul 	 * the status word to make it look like a successful
279696f2e892SBill Paul 	 * frame reception.
279796f2e892SBill Paul 	 */
279896f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2799af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
280096f2e892SBill Paul }
280196f2e892SBill Paul 
280296f2e892SBill Paul /*
280373bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
280473bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
280573bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
280673bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
280773bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
280873bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
280973bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
281073bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
281173bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
281273bf949cSBill Paul  */
2813e3d2833aSAlfred Perlstein static int
28140934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
281573bf949cSBill Paul {
281673bf949cSBill Paul 	struct dc_desc *cur_rx;
28170934f18aSMaxime Henrion 	int i, pos;
281873bf949cSBill Paul 
281973bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
282073bf949cSBill Paul 
282173bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
28225f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[pos];
2823af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
282473bf949cSBill Paul 			break;
282573bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
282673bf949cSBill Paul 	}
282773bf949cSBill Paul 
282873bf949cSBill Paul 	/* If the ring really is empty, then just return. */
282973bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
283073bf949cSBill Paul 		return (0);
283173bf949cSBill Paul 
283273bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
283373bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
283473bf949cSBill Paul 
283573bf949cSBill Paul 	return (EAGAIN);
283673bf949cSBill Paul }
283773bf949cSBill Paul 
28385f14ee23SPyun YongHyeon static void
28395f14ee23SPyun YongHyeon dc_discard_rxbuf(struct dc_softc *sc, int i)
28405f14ee23SPyun YongHyeon {
28415f14ee23SPyun YongHyeon 	struct mbuf *m;
28425f14ee23SPyun YongHyeon 
28435f14ee23SPyun YongHyeon 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
28445f14ee23SPyun YongHyeon 		m = sc->dc_cdata.dc_rx_chain[i];
28455f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
28465f14ee23SPyun YongHyeon 	}
28475f14ee23SPyun YongHyeon 
28485f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
28495f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
28505f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_PREREAD |
28515f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
28525f14ee23SPyun YongHyeon }
28535f14ee23SPyun YongHyeon 
285473bf949cSBill Paul /*
285596f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
285696f2e892SBill Paul  * the higher level protocols.
285796f2e892SBill Paul  */
28581abcdbd1SAttilio Rao static int
28590934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
286096f2e892SBill Paul {
28615f14ee23SPyun YongHyeon 	struct mbuf *m;
286296f2e892SBill Paul 	struct ifnet *ifp;
286396f2e892SBill Paul 	struct dc_desc *cur_rx;
28641abcdbd1SAttilio Rao 	int i, total_len, rx_npkts;
2865ee320f98SPyun YongHyeon 	uint32_t rxstat;
286696f2e892SBill Paul 
28675120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
28685120abbfSSam Leffler 
2869fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
28701abcdbd1SAttilio Rao 	rx_npkts = 0;
287196f2e892SBill Paul 
28725f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_POSTREAD |
28735f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
28745f14ee23SPyun YongHyeon 	for (i = sc->dc_cdata.dc_rx_prod;
28755f14ee23SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
28765f14ee23SPyun YongHyeon 	    DC_INC(i, DC_RX_LIST_CNT)) {
2877e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
287840929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2879e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2880e4fc250cSLuigi Rizzo 				break;
2881e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2882e4fc250cSLuigi Rizzo 		}
28830934f18aSMaxime Henrion #endif
28845f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[i];
2885af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
28865f14ee23SPyun YongHyeon 		if ((rxstat & DC_RXSTAT_OWN) != 0)
28875f14ee23SPyun YongHyeon 			break;
288896f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
28895f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
289056e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
289196f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
2892848a02fcSPyun YongHyeon 		rx_npkts++;
289396f2e892SBill Paul 
289496f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
289596f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
289696f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
289796f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
28985f14ee23SPyun YongHyeon 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0)
289996f2e892SBill Paul 					continue;
290096f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2901af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
290296f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
290396f2e892SBill Paul 			}
290496f2e892SBill Paul 		}
290596f2e892SBill Paul 
290696f2e892SBill Paul 		/*
290796f2e892SBill Paul 		 * If an error occurs, update stats, clear the
290896f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
290996f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2910db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
29110934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
291296f2e892SBill Paul 		 */
2913db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2914db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2915db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2916db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2917db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
2918*c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
291996f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
2920*c8dfaf38SGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
29215f14ee23SPyun YongHyeon 				dc_discard_rxbuf(sc, i);
29225f14ee23SPyun YongHyeon 				if (rxstat & DC_RXSTAT_CRCERR)
292396f2e892SBill Paul 					continue;
29245f14ee23SPyun YongHyeon 				else {
29258f382a1fSPyun YongHyeon 					ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2926c8b27acaSJohn Baldwin 					dc_init_locked(sc);
29271abcdbd1SAttilio Rao 					return (rx_npkts);
292896f2e892SBill Paul 				}
292996f2e892SBill Paul 			}
2930db40c1aeSDoug Ambrisko 		}
293196f2e892SBill Paul 
293296f2e892SBill Paul 		/* No errors; receive the packet. */
293396f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2934432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
293501faf54bSLuigi Rizzo 		/*
2936432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
293701faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
293801faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
293901faf54bSLuigi Rizzo 		 * copy done in m_devget().
294001faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
294101faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
294201faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
294301faf54bSLuigi Rizzo 		 */
29445f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0) {
29455f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
2946*c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
29475f14ee23SPyun YongHyeon 			continue;
29485f14ee23SPyun YongHyeon 		}
294901faf54bSLuigi Rizzo 		m->m_pkthdr.rcvif = ifp;
295001faf54bSLuigi Rizzo 		m->m_pkthdr.len = m->m_len = total_len;
29515f14ee23SPyun YongHyeon #else
295201faf54bSLuigi Rizzo 		{
29535f14ee23SPyun YongHyeon 			struct mbuf *m0;
29545f14ee23SPyun YongHyeon 
295501faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
295601faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
29575f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
295896f2e892SBill Paul 			if (m0 == NULL) {
2959*c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
296096f2e892SBill Paul 				continue;
296196f2e892SBill Paul 			}
296296f2e892SBill Paul 			m = m0;
296301faf54bSLuigi Rizzo 		}
29645f14ee23SPyun YongHyeon #endif
296596f2e892SBill Paul 
2966*c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
29675120abbfSSam Leffler 		DC_UNLOCK(sc);
29689ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
29695120abbfSSam Leffler 		DC_LOCK(sc);
297096f2e892SBill Paul 	}
297196f2e892SBill Paul 
297296f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
29731abcdbd1SAttilio Rao 	return (rx_npkts);
297496f2e892SBill Paul }
297596f2e892SBill Paul 
297696f2e892SBill Paul /*
297796f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
297896f2e892SBill Paul  * the list buffers.
297996f2e892SBill Paul  */
2980e3d2833aSAlfred Perlstein static void
29810934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
298296f2e892SBill Paul {
29835f14ee23SPyun YongHyeon 	struct dc_desc *cur_tx;
298496f2e892SBill Paul 	struct ifnet *ifp;
29855f14ee23SPyun YongHyeon 	int idx, setup;
2986ee320f98SPyun YongHyeon 	uint32_t ctl, txstat;
298796f2e892SBill Paul 
298806d23883SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt == 0)
298906d23883SPyun YongHyeon 		return;
299006d23883SPyun YongHyeon 
2991fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
299296f2e892SBill Paul 
299396f2e892SBill Paul 	/*
299496f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
299596f2e892SBill Paul 	 * frames that have been transmitted.
299696f2e892SBill Paul 	 */
2997cb94db27SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_POSTREAD |
29985f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
29995f14ee23SPyun YongHyeon 	setup = 0;
30005f14ee23SPyun YongHyeon 	for (idx = sc->dc_cdata.dc_tx_cons; idx != sc->dc_cdata.dc_tx_prod;
30015f14ee23SPyun YongHyeon 	    DC_INC(idx, DC_TX_LIST_CNT), sc->dc_cdata.dc_tx_cnt--) {
30025f14ee23SPyun YongHyeon 		cur_tx = &sc->dc_ldata.dc_tx_list[idx];
3003af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
3004af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
300596f2e892SBill Paul 
300696f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
300796f2e892SBill Paul 			break;
300896f2e892SBill Paul 
30095f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_chain[idx] == NULL)
30105f14ee23SPyun YongHyeon 			continue;
30115f14ee23SPyun YongHyeon 
3012af4358c7SMaxime Henrion 		if (ctl & DC_TXCTL_SETUP) {
30135f14ee23SPyun YongHyeon 			cur_tx->dc_ctl = htole32(ctl & ~DC_TXCTL_SETUP);
30145f14ee23SPyun YongHyeon 			setup++;
30155f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
30165f14ee23SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
301796f2e892SBill Paul 			/*
301896f2e892SBill Paul 			 * Yes, the PNIC is so brain damaged
301996f2e892SBill Paul 			 * that it will sometimes generate a TX
302096f2e892SBill Paul 			 * underrun error while DMAing the RX
302196f2e892SBill Paul 			 * filter setup frame. If we detect this,
302296f2e892SBill Paul 			 * we have to send the setup frame again,
302396f2e892SBill Paul 			 * or else the filter won't be programmed
302496f2e892SBill Paul 			 * correctly.
302596f2e892SBill Paul 			 */
302696f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
302796f2e892SBill Paul 				if (txstat & DC_TXSTAT_ERRSUM)
302896f2e892SBill Paul 					dc_setfilt(sc);
302996f2e892SBill Paul 			}
303096f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
303196f2e892SBill Paul 			continue;
303296f2e892SBill Paul 		}
303396f2e892SBill Paul 
303429a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
3035feb78939SJonathan Chen 			/*
3036feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
3037feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
303829a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
303929a2220aSBill Paul 			 * Who knows, but Conexant chips have the
304029a2220aSBill Paul 			 * same problem. Maybe they took lessons
304129a2220aSBill Paul 			 * from Xircom.
304229a2220aSBill Paul 			 */
3043feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
3044feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
3045feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
3046feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
3047feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
3048feb78939SJonathan Chen 		} else {
304996f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
305096f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
305196f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
305296f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
305396f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
3054feb78939SJonathan Chen 		}
305596f2e892SBill Paul 
305696f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
3057*c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
305896f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
3059*c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
306096f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
3061*c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
306296f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
30638f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3064c8b27acaSJohn Baldwin 				dc_init_locked(sc);
306596f2e892SBill Paul 				return;
306696f2e892SBill Paul 			}
306752c43a47SPyun YongHyeon 		} else
3068*c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3069*c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_COLLISIONS, (txstat & DC_TXSTAT_COLLCNT) >> 3);
307096f2e892SBill Paul 
30715f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
307256e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
30735f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
307496f2e892SBill Paul 		m_freem(sc->dc_cdata.dc_tx_chain[idx]);
307596f2e892SBill Paul 		sc->dc_cdata.dc_tx_chain[idx] = NULL;
307696f2e892SBill Paul 	}
307796f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
307882a67a70SMarius Strobl 
30795f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
308013f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
30813e0e6726SMarius Strobl 		if (sc->dc_cdata.dc_tx_cnt == 0)
30823e0e6726SMarius Strobl 			sc->dc_wdog_timer = 0;
308396f2e892SBill Paul 	}
30845f14ee23SPyun YongHyeon 	if (setup > 0)
30855f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
30865f14ee23SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
30875f14ee23SPyun YongHyeon }
308896f2e892SBill Paul 
3089e3d2833aSAlfred Perlstein static void
30900934f18aSMaxime Henrion dc_tick(void *xsc)
309196f2e892SBill Paul {
309296f2e892SBill Paul 	struct dc_softc *sc;
309396f2e892SBill Paul 	struct mii_data *mii;
309496f2e892SBill Paul 	struct ifnet *ifp;
3095ee320f98SPyun YongHyeon 	uint32_t r;
309696f2e892SBill Paul 
309796f2e892SBill Paul 	sc = xsc;
3098c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3099fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
310096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
310196f2e892SBill Paul 
310206d23883SPyun YongHyeon 	/*
310306d23883SPyun YongHyeon 	 * Reclaim transmitted frames for controllers that do
310406d23883SPyun YongHyeon 	 * not generate TX completion interrupt for every frame.
310506d23883SPyun YongHyeon 	 */
310606d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR)
310706d23883SPyun YongHyeon 		dc_txeof(sc);
310806d23883SPyun YongHyeon 
310996f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
3110318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
3111318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
3112318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3113318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
311496f2e892SBill Paul 				sc->dc_link = 0;
3115318b02fdSBill Paul 				mii_mediachg(mii);
3116318b02fdSBill Paul 			}
3117318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3118318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
3119318b02fdSBill Paul 				sc->dc_link = 0;
3120318b02fdSBill Paul 				mii_mediachg(mii);
3121318b02fdSBill Paul 			}
3122d675147eSBill Paul 			if (sc->dc_link == 0)
312396f2e892SBill Paul 				mii_tick(mii);
312496f2e892SBill Paul 		} else {
3125d0d67284SMarius Strobl 			/*
3126d0d67284SMarius Strobl 			 * For NICs which never report DC_RXSTATE_WAIT, we
3127d0d67284SMarius Strobl 			 * have to bite the bullet...
3128d0d67284SMarius Strobl 			 */
3129d0d67284SMarius Strobl 			if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
3130d0d67284SMarius Strobl 			    DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
3131d314ebf5SPyun YongHyeon 			    sc->dc_cdata.dc_tx_cnt == 0)
313296f2e892SBill Paul 				mii_tick(mii);
3133259b8d84SMartin Blapp 		}
313496f2e892SBill Paul 	} else
313596f2e892SBill Paul 		mii_tick(mii);
313696f2e892SBill Paul 
313796f2e892SBill Paul 	/*
313896f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
313996f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
314096f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
314196f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
314296f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
314396f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
314496f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
314596f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
314696f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
314796f2e892SBill Paul 	 * a screeching halt for several seconds.
314896f2e892SBill Paul 	 *
314996f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
315096f2e892SBill Paul 	 * any packets until a link has been established. After the
315196f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
315296f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
315396f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
315496f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
315596f2e892SBill Paul 	 */
3156d314ebf5SPyun YongHyeon 	if (sc->dc_link != 0 && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3157c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
315896f2e892SBill Paul 
3159318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
3160b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3161318b02fdSBill Paul 	else
3162b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
316396f2e892SBill Paul }
316496f2e892SBill Paul 
3165d467c136SBill Paul /*
3166d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
3167d467c136SBill Paul  * or switch to store and forward mode if we have to.
3168d467c136SBill Paul  */
3169e3d2833aSAlfred Perlstein static void
31700934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
3171d467c136SBill Paul {
3172d9efae03SPyun YongHyeon 	uint32_t netcfg, isr;
3173d9efae03SPyun YongHyeon 	int i, reinit;
3174d467c136SBill Paul 
3175d9efae03SPyun YongHyeon 	reinit = 0;
3176d9efae03SPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
3177d9efae03SPyun YongHyeon 	device_printf(sc->dc_dev, "TX underrun -- ");
3178d9efae03SPyun YongHyeon 	if ((sc->dc_flags & DC_TX_STORENFWD) == 0) {
3179d9efae03SPyun YongHyeon 		if (sc->dc_txthresh + DC_TXTHRESH_INC > DC_TXTHRESH_MAX) {
3180d9efae03SPyun YongHyeon 			printf("using store and forward mode\n");
3181d9efae03SPyun YongHyeon 			netcfg |= DC_NETCFG_STORENFWD;
3182d9efae03SPyun YongHyeon 		} else {
3183d9efae03SPyun YongHyeon 			printf("increasing TX threshold\n");
3184d9efae03SPyun YongHyeon 			sc->dc_txthresh += DC_TXTHRESH_INC;
3185d9efae03SPyun YongHyeon 			netcfg &= ~DC_NETCFG_TX_THRESH;
3186d9efae03SPyun YongHyeon 			netcfg |= sc->dc_txthresh;
31878f382a1fSPyun YongHyeon 		}
3188d467c136SBill Paul 
3189d467c136SBill Paul 		if (DC_IS_INTEL(sc)) {
3190d467c136SBill Paul 			/*
3191d467c136SBill Paul 			 * The real 21143 requires that the transmitter be idle
3192d467c136SBill Paul 			 * in order to change the transmit threshold or store
3193d467c136SBill Paul 			 * and forward state.
3194d467c136SBill Paul 			 */
3195d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg & ~DC_NETCFG_TX_ON);
3196d467c136SBill Paul 
3197d467c136SBill Paul 			for (i = 0; i < DC_TIMEOUT; i++) {
3198d467c136SBill Paul 				isr = CSR_READ_4(sc, DC_ISR);
3199d467c136SBill Paul 				if (isr & DC_ISR_TX_IDLE)
3200d467c136SBill Paul 					break;
3201d467c136SBill Paul 				DELAY(10);
3202d467c136SBill Paul 			}
3203d467c136SBill Paul 			if (i == DC_TIMEOUT) {
32046b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
3205432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
3206432120f2SMarius Strobl 				    __func__);
3207d9efae03SPyun YongHyeon 				reinit++;
3208d9efae03SPyun YongHyeon 			}
3209d9efae03SPyun YongHyeon 		}
3210d9efae03SPyun YongHyeon 	} else {
3211d9efae03SPyun YongHyeon 		printf("resetting\n");
3212d9efae03SPyun YongHyeon 		reinit++;
3213d9efae03SPyun YongHyeon 	}
3214d9efae03SPyun YongHyeon 
3215d9efae03SPyun YongHyeon 	if (reinit == 0) {
3216d9efae03SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, netcfg);
3217d9efae03SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3218d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg | DC_NETCFG_TX_ON);
3219d9efae03SPyun YongHyeon 	} else {
32208f382a1fSPyun YongHyeon 		sc->dc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3221c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3222d467c136SBill Paul 	}
3223d467c136SBill Paul }
3224d467c136SBill Paul 
3225e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3226e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3227e4fc250cSLuigi Rizzo 
32281abcdbd1SAttilio Rao static int
3229e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3230e4fc250cSLuigi Rizzo {
3231e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
32321abcdbd1SAttilio Rao 	int rx_npkts = 0;
3233e4fc250cSLuigi Rizzo 
323440929967SGleb Smirnoff 	DC_LOCK(sc);
323540929967SGleb Smirnoff 
323640929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
323740929967SGleb Smirnoff 		DC_UNLOCK(sc);
32381abcdbd1SAttilio Rao 		return (rx_npkts);
3239e4fc250cSLuigi Rizzo 	}
324040929967SGleb Smirnoff 
3241e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
32421abcdbd1SAttilio Rao 	rx_npkts = dc_rxeof(sc);
3243e4fc250cSLuigi Rizzo 	dc_txeof(sc);
324413f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
324513f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3246c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3247e4fc250cSLuigi Rizzo 
3248e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3249ee320f98SPyun YongHyeon 		uint32_t	status;
3250e4fc250cSLuigi Rizzo 
3251e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3252e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3253e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3254e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
32555120abbfSSam Leffler 		if (!status) {
32565120abbfSSam Leffler 			DC_UNLOCK(sc);
32571abcdbd1SAttilio Rao 			return (rx_npkts);
32585120abbfSSam Leffler 		}
3259e4fc250cSLuigi Rizzo 		/* ack what we have */
3260e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3261e4fc250cSLuigi Rizzo 
3262e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3263ee320f98SPyun YongHyeon 			uint32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3264*c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, (r & 0xffff) + ((r >> 17) & 0x7ff));
3265e4fc250cSLuigi Rizzo 
3266e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3267e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3268e4fc250cSLuigi Rizzo 		}
3269e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3270e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3271e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3272e4fc250cSLuigi Rizzo 
3273e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3274e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3275e4fc250cSLuigi Rizzo 
3276e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
32776b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
32788f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3279c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3280e4fc250cSLuigi Rizzo 		}
3281e4fc250cSLuigi Rizzo 	}
32825120abbfSSam Leffler 	DC_UNLOCK(sc);
32831abcdbd1SAttilio Rao 	return (rx_npkts);
3284e4fc250cSLuigi Rizzo }
3285e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3286e4fc250cSLuigi Rizzo 
3287e3d2833aSAlfred Perlstein static void
32880934f18aSMaxime Henrion dc_intr(void *arg)
328996f2e892SBill Paul {
329096f2e892SBill Paul 	struct dc_softc *sc;
329196f2e892SBill Paul 	struct ifnet *ifp;
3292ee320f98SPyun YongHyeon 	uint32_t r, status;
3293848a02fcSPyun YongHyeon 	int n;
329496f2e892SBill Paul 
329596f2e892SBill Paul 	sc = arg;
3296d2a1864bSWarner Losh 
32970934f18aSMaxime Henrion 	if (sc->suspended)
3298e8388e14SMitsuru IWASAKI 		return;
3299e8388e14SMitsuru IWASAKI 
3300d1ce9105SBill Paul 	DC_LOCK(sc);
3301a84b4e80SPyun YongHyeon 	status = CSR_READ_4(sc, DC_ISR);
3302a84b4e80SPyun YongHyeon 	if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0) {
3303a84b4e80SPyun YongHyeon 		DC_UNLOCK(sc);
3304a84b4e80SPyun YongHyeon 		return;
3305a84b4e80SPyun YongHyeon 	}
3306fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3307e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
330840929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
330940929967SGleb Smirnoff 		DC_UNLOCK(sc);
331040929967SGleb Smirnoff 		return;
3311e4fc250cSLuigi Rizzo 	}
33120934f18aSMaxime Henrion #endif
331396f2e892SBill Paul 	/* Disable interrupts. */
331496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
331596f2e892SBill Paul 
3316a84b4e80SPyun YongHyeon 	for (n = 16; n > 0; n--) {
3317a84b4e80SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3318a84b4e80SPyun YongHyeon 			break;
3319a84b4e80SPyun YongHyeon 		/* Ack interrupts. */
332096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
332196f2e892SBill Paul 
332273bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
3323848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
332473bf949cSBill Paul 				while (dc_rx_resync(sc))
332573bf949cSBill Paul 					dc_rxeof(sc);
332673bf949cSBill Paul 			}
332773bf949cSBill Paul 		}
332896f2e892SBill Paul 
332996f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
333096f2e892SBill Paul 			dc_txeof(sc);
333196f2e892SBill Paul 
333296f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
333396f2e892SBill Paul 			dc_txeof(sc);
333496f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
333596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
333696f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
333796f2e892SBill Paul 			}
333896f2e892SBill Paul 		}
333996f2e892SBill Paul 
3340d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3341d467c136SBill Paul 			dc_tx_underrun(sc);
334296f2e892SBill Paul 
334396f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
334473bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
334526b40a65SPyun YongHyeon 			r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3346*c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, (r & 0xffff) + ((r >> 17) & 0x7ff));
3347848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
334873bf949cSBill Paul 				while (dc_rx_resync(sc))
334973bf949cSBill Paul 					dc_rxeof(sc);
335073bf949cSBill Paul 			}
335173bf949cSBill Paul 		}
335296f2e892SBill Paul 
3353a84b4e80SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3354a84b4e80SPyun YongHyeon 			dc_start_locked(ifp);
3355a84b4e80SPyun YongHyeon 
335696f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
33578f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3358c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3359a84b4e80SPyun YongHyeon 			DC_UNLOCK(sc);
3360a84b4e80SPyun YongHyeon 			return;
336196f2e892SBill Paul 		}
3362a84b4e80SPyun YongHyeon 		status = CSR_READ_4(sc, DC_ISR);
3363a84b4e80SPyun YongHyeon 		if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0)
3364a84b4e80SPyun YongHyeon 			break;
336596f2e892SBill Paul 	}
336696f2e892SBill Paul 
336796f2e892SBill Paul 	/* Re-enable interrupts. */
3368a84b4e80SPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
336996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
337096f2e892SBill Paul 
3371d1ce9105SBill Paul 	DC_UNLOCK(sc);
337296f2e892SBill Paul }
337396f2e892SBill Paul 
337496f2e892SBill Paul /*
337596f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
337696f2e892SBill Paul  * pointers to the fragment pointers.
337796f2e892SBill Paul  */
3378e3d2833aSAlfred Perlstein static int
3379a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
338096f2e892SBill Paul {
3381ebc284ccSMarius Strobl 	bus_dma_segment_t segs[DC_MAXFRAGS];
33825f14ee23SPyun YongHyeon 	bus_dmamap_t map;
3383ebc284ccSMarius Strobl 	struct dc_desc *f;
338496f2e892SBill Paul 	struct mbuf *m;
3385993a741aSMarius Strobl 	int cur, defragged, error, first, frag, i, idx, nseg;
3386cda97c50SMike Silbersack 
3387993a741aSMarius Strobl 	m = NULL;
3388993a741aSMarius Strobl 	defragged = 0;
3389993a741aSMarius Strobl 	if (sc->dc_flags & DC_TX_COALESCE &&
3390993a741aSMarius Strobl 	    ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3391c6499eccSGleb Smirnoff 		m = m_defrag(*m_head, M_NOWAIT);
3392993a741aSMarius Strobl 		defragged = 1;
3393993a741aSMarius Strobl 	} else {
3394cda97c50SMike Silbersack 		/*
3395993a741aSMarius Strobl 		 * Count the number of frags in this chain to see if we
3396993a741aSMarius Strobl 		 * need to m_collapse.  Since the descriptor list is shared
3397993a741aSMarius Strobl 		 * by all packets, we'll m_collapse long chains so that they
3398cda97c50SMike Silbersack 		 * do not use up the entire list, even if they would fit.
3399cda97c50SMike Silbersack 		 */
3400993a741aSMarius Strobl 		i = 0;
3401a10c0e45SMike Silbersack 		for (m = *m_head; m != NULL; m = m->m_next)
3402993a741aSMarius Strobl 			i++;
3403993a741aSMarius Strobl 		if (i > DC_TX_LIST_CNT / 4 ||
3404993a741aSMarius Strobl 		    DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3405993a741aSMarius Strobl 		    DC_TX_LIST_RSVD) {
3406c6499eccSGleb Smirnoff 			m = m_collapse(*m_head, M_NOWAIT, DC_MAXFRAGS);
3407993a741aSMarius Strobl 			defragged = 1;
3408993a741aSMarius Strobl 		}
3409993a741aSMarius Strobl 	}
3410993a741aSMarius Strobl 	if (defragged != 0) {
341182a67a70SMarius Strobl 		if (m == NULL) {
341282a67a70SMarius Strobl 			m_freem(*m_head);
341382a67a70SMarius Strobl 			*m_head = NULL;
3414cda97c50SMike Silbersack 			return (ENOBUFS);
341582a67a70SMarius Strobl 		}
3416a10c0e45SMike Silbersack 		*m_head = m;
3417cda97c50SMike Silbersack 	}
3418993a741aSMarius Strobl 
341956e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
34205f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3421ebc284ccSMarius Strobl 	    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3422ebc284ccSMarius Strobl 	if (error == EFBIG) {
3423c6499eccSGleb Smirnoff 		if (defragged != 0 || (m = m_collapse(*m_head, M_NOWAIT,
3424993a741aSMarius Strobl 		    DC_MAXFRAGS)) == NULL) {
3425ebc284ccSMarius Strobl 			m_freem(*m_head);
342682a67a70SMarius Strobl 			*m_head = NULL;
3427993a741aSMarius Strobl 			return (defragged != 0 ? error : ENOBUFS);
342882a67a70SMarius Strobl 		}
3429ebc284ccSMarius Strobl 		*m_head = m;
34305f14ee23SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3431ebc284ccSMarius Strobl 		    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3432ebc284ccSMarius Strobl 		if (error != 0) {
3433ebc284ccSMarius Strobl 			m_freem(*m_head);
3434ebc284ccSMarius Strobl 			*m_head = NULL;
3435ebc284ccSMarius Strobl 			return (error);
343682a67a70SMarius Strobl 		}
3437ebc284ccSMarius Strobl 	} else if (error != 0)
3438ebc284ccSMarius Strobl 		return (error);
3439ebc284ccSMarius Strobl 	KASSERT(nseg <= DC_MAXFRAGS,
3440ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
3441ebc284ccSMarius Strobl 	if (nseg == 0) {
3442ebc284ccSMarius Strobl 		m_freem(*m_head);
3443ebc284ccSMarius Strobl 		*m_head = NULL;
3444ebc284ccSMarius Strobl 		return (EIO);
3445ebc284ccSMarius Strobl 	}
3446ebc284ccSMarius Strobl 
34475f14ee23SPyun YongHyeon 	/* Check descriptor overruns. */
34485f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt + nseg > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
34495f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
34505f14ee23SPyun YongHyeon 		return (ENOBUFS);
34515f14ee23SPyun YongHyeon 	}
34525f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
34535f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
34545f14ee23SPyun YongHyeon 
3455ebc284ccSMarius Strobl 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3456ebc284ccSMarius Strobl 	for (i = 0; i < nseg; i++) {
3457ebc284ccSMarius Strobl 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3458ebc284ccSMarius Strobl 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3459ebc284ccSMarius Strobl 		    (first != sc->dc_cdata.dc_tx_first)) {
34605f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_mtag,
3461ebc284ccSMarius Strobl 			    sc->dc_cdata.dc_tx_map[first]);
3462ebc284ccSMarius Strobl 			m_freem(*m_head);
3463ebc284ccSMarius Strobl 			*m_head = NULL;
3464ebc284ccSMarius Strobl 			return (ENOBUFS);
3465ebc284ccSMarius Strobl 		}
3466ebc284ccSMarius Strobl 
34675f14ee23SPyun YongHyeon 		f = &sc->dc_ldata.dc_tx_list[frag];
3468ebc284ccSMarius Strobl 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3469ebc284ccSMarius Strobl 		if (i == 0) {
3470ebc284ccSMarius Strobl 			f->dc_status = 0;
3471ebc284ccSMarius Strobl 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3472ebc284ccSMarius Strobl 		} else
3473ebc284ccSMarius Strobl 			f->dc_status = htole32(DC_TXSTAT_OWN);
34745f14ee23SPyun YongHyeon 		f->dc_data = htole32(DC_ADDR_LO(segs[i].ds_addr));
3475ebc284ccSMarius Strobl 		cur = frag;
3476ebc284ccSMarius Strobl 		DC_INC(frag, DC_TX_LIST_CNT);
3477ebc284ccSMarius Strobl 	}
3478ebc284ccSMarius Strobl 
3479ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_prod = frag;
3480ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_cnt += nseg;
3481ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_chain[cur] = *m_head;
34825f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3483ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
34845f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[first].dc_ctl |=
3485ebc284ccSMarius Strobl 		    htole32(DC_TXCTL_FINT);
3486ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
34875f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
348806d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR &&
348906d23883SPyun YongHyeon 	    ++sc->dc_cdata.dc_tx_pkts >= 8) {
349006d23883SPyun YongHyeon 		sc->dc_cdata.dc_tx_pkts = 0;
34915f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
349206d23883SPyun YongHyeon 	}
34935f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3494ebc284ccSMarius Strobl 
34955f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
34965f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
34975f14ee23SPyun YongHyeon 
34985f14ee23SPyun YongHyeon 	/*
34995f14ee23SPyun YongHyeon 	 * Swap the last and the first dmamaps to ensure the map for
35005f14ee23SPyun YongHyeon 	 * this transmission is placed at the last descriptor.
35015f14ee23SPyun YongHyeon 	 */
35025f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_tx_map[cur];
35035f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[cur] = sc->dc_cdata.dc_tx_map[first];
35045f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[first] = map;
35055f14ee23SPyun YongHyeon 
350696f2e892SBill Paul 	return (0);
350796f2e892SBill Paul }
350896f2e892SBill Paul 
3509e3d2833aSAlfred Perlstein static void
35100934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
351196f2e892SBill Paul {
351296f2e892SBill Paul 	struct dc_softc *sc;
3513c8b27acaSJohn Baldwin 
3514c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3515c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3516c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3517c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3518c8b27acaSJohn Baldwin }
3519c8b27acaSJohn Baldwin 
3520ebc284ccSMarius Strobl /*
3521ebc284ccSMarius Strobl  * Main transmit routine
3522ebc284ccSMarius Strobl  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3523ebc284ccSMarius Strobl  * regions directly in the transmit lists.  We also save a copy of the
3524ebc284ccSMarius Strobl  * pointers since the transmit list fragment pointers are physical
3525ebc284ccSMarius Strobl  * addresses.
3526ebc284ccSMarius Strobl  */
3527c8b27acaSJohn Baldwin static void
3528c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3529c8b27acaSJohn Baldwin {
3530c8b27acaSJohn Baldwin 	struct dc_softc *sc;
35315f14ee23SPyun YongHyeon 	struct mbuf *m_head;
35325f14ee23SPyun YongHyeon 	int queued;
353396f2e892SBill Paul 
353496f2e892SBill Paul 	sc = ifp->if_softc;
353596f2e892SBill Paul 
3536c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
353796f2e892SBill Paul 
353876d40c85SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
353976d40c85SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->dc_link == 0)
3540d1ce9105SBill Paul 		return;
354196f2e892SBill Paul 
35425f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
354396f2e892SBill Paul 
35445f14ee23SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
35455f14ee23SPyun YongHyeon 		/*
35465f14ee23SPyun YongHyeon 		 * If there's no way we can send any packets, return now.
35475f14ee23SPyun YongHyeon 		 */
35485f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
35495f14ee23SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
35505f14ee23SPyun YongHyeon 			break;
35515f14ee23SPyun YongHyeon 		}
3552cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
355396f2e892SBill Paul 		if (m_head == NULL)
355496f2e892SBill Paul 			break;
355596f2e892SBill Paul 
3556a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
355782a67a70SMarius Strobl 			if (m_head == NULL)
355882a67a70SMarius Strobl 				break;
3559cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
356013f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
356196f2e892SBill Paul 			break;
356296f2e892SBill Paul 		}
356396f2e892SBill Paul 
3564cbaf877fSBrian Feldman 		queued++;
356596f2e892SBill Paul 		/*
356696f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
356796f2e892SBill Paul 		 * to him.
356896f2e892SBill Paul 		 */
35699ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
357096f2e892SBill Paul 	}
357196f2e892SBill Paul 
3572cbaf877fSBrian Feldman 	if (queued > 0) {
357396f2e892SBill Paul 		/* Transmit */
357496f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
357596f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
357696f2e892SBill Paul 
357796f2e892SBill Paul 		/*
357896f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
357996f2e892SBill Paul 		 */
3580b1d16143SMarius Strobl 		sc->dc_wdog_timer = 5;
3581cbaf877fSBrian Feldman 	}
358296f2e892SBill Paul }
358396f2e892SBill Paul 
3584e3d2833aSAlfred Perlstein static void
35850934f18aSMaxime Henrion dc_init(void *xsc)
358696f2e892SBill Paul {
358796f2e892SBill Paul 	struct dc_softc *sc = xsc;
3588c8b27acaSJohn Baldwin 
3589c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3590c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3591c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3592c8b27acaSJohn Baldwin }
3593c8b27acaSJohn Baldwin 
3594c8b27acaSJohn Baldwin static void
3595c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3596c8b27acaSJohn Baldwin {
3597fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
359896f2e892SBill Paul 	struct mii_data *mii;
3599d314ebf5SPyun YongHyeon 	struct ifmedia *ifm;
360096f2e892SBill Paul 
3601c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
360296f2e892SBill Paul 
36038f382a1fSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
36048f382a1fSPyun YongHyeon 		return;
36058f382a1fSPyun YongHyeon 
360696f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
360796f2e892SBill Paul 
360896f2e892SBill Paul 	/*
360996f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
361096f2e892SBill Paul 	 */
361196f2e892SBill Paul 	dc_stop(sc);
361296f2e892SBill Paul 	dc_reset(sc);
3613d314ebf5SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
3614d314ebf5SPyun YongHyeon 		ifm = &mii->mii_media;
3615d314ebf5SPyun YongHyeon 		dc_apply_fixup(sc, ifm->ifm_media);
3616d314ebf5SPyun YongHyeon 	}
361796f2e892SBill Paul 
361896f2e892SBill Paul 	/*
361996f2e892SBill Paul 	 * Set cache alignment and burst length.
362096f2e892SBill Paul 	 */
362152ca7ee2SPyun YongHyeon 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc) || DC_IS_ULI(sc))
362296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
362396f2e892SBill Paul 	else
362496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3625935fe010SLuigi Rizzo 	/*
3626935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3627935fe010SLuigi Rizzo 	 */
3628935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3629935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
363096f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
363196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
363296f2e892SBill Paul 	} else {
363396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
363496f2e892SBill Paul 	}
363596f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
363696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
363796f2e892SBill Paul 	switch(sc->dc_cachesize) {
363896f2e892SBill Paul 	case 32:
363996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
364096f2e892SBill Paul 		break;
364196f2e892SBill Paul 	case 16:
364296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
364396f2e892SBill Paul 		break;
364496f2e892SBill Paul 	case 8:
364596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
364696f2e892SBill Paul 		break;
364796f2e892SBill Paul 	case 0:
364896f2e892SBill Paul 	default:
364996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
365096f2e892SBill Paul 		break;
365196f2e892SBill Paul 	}
365296f2e892SBill Paul 
365396f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
365496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365596f2e892SBill Paul 	else {
3656d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
365796f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365896f2e892SBill Paul 		} else {
365996f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
366096f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
366196f2e892SBill Paul 		}
366296f2e892SBill Paul 	}
366396f2e892SBill Paul 
366496f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
366596f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
366696f2e892SBill Paul 
366796f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
366896f2e892SBill Paul 		/*
366996f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
367096f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
367196f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
367296f2e892SBill Paul 		 * document the meaning of these bits so there's no way
367396f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
367496f2e892SBill Paul 		 * number all its own; the rest all use a different one.
367596f2e892SBill Paul 		 */
367696f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
367796f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
367896f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
367996f2e892SBill Paul 		else
368096f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
368196f2e892SBill Paul 	}
368296f2e892SBill Paul 
3683feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3684feb78939SJonathan Chen 		/*
3685feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3686feb78939SJonathan Chen 		 * can talk to the MII.
3687feb78939SJonathan Chen 		 */
3688feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3689feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3690feb78939SJonathan Chen 		DELAY(10);
3691feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3692feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3693feb78939SJonathan Chen 		DELAY(10);
3694feb78939SJonathan Chen 	}
3695feb78939SJonathan Chen 
369696f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3697d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
369896f2e892SBill Paul 
369996f2e892SBill Paul 	/* Init circular RX list. */
370096f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
37016b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
370222f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
370396f2e892SBill Paul 		dc_stop(sc);
370496f2e892SBill Paul 		return;
370596f2e892SBill Paul 	}
370696f2e892SBill Paul 
370796f2e892SBill Paul 	/*
370856e5e7aeSMaxime Henrion 	 * Init TX descriptors.
370996f2e892SBill Paul 	 */
371096f2e892SBill Paul 	dc_list_tx_init(sc);
371196f2e892SBill Paul 
371296f2e892SBill Paul 	/*
371396f2e892SBill Paul 	 * Load the address of the RX list.
371496f2e892SBill Paul 	 */
371556e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
371656e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
371796f2e892SBill Paul 
371896f2e892SBill Paul 	/*
371996f2e892SBill Paul 	 * Enable interrupts.
372096f2e892SBill Paul 	 */
3721e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3722e4fc250cSLuigi Rizzo 	/*
3723e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3724e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3725e4fc250cSLuigi Rizzo 	 * after a reset.
3726e4fc250cSLuigi Rizzo 	 */
372740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3728e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3729e4fc250cSLuigi Rizzo 	else
3730e4fc250cSLuigi Rizzo #endif
373196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
373296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
373396f2e892SBill Paul 
373452ca7ee2SPyun YongHyeon 	/* Initialize TX jabber and RX watchdog timer. */
373552ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
373652ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_WATCHDOG, DC_WDOG_JABBERCLK |
373752ca7ee2SPyun YongHyeon 		    DC_WDOG_HOSTUNJAB);
373852ca7ee2SPyun YongHyeon 
373996f2e892SBill Paul 	/* Enable transmitter. */
374096f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
374196f2e892SBill Paul 
374296f2e892SBill Paul 	/*
3743918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3744918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3745918434c8SBill Paul 	 * link and activity indications.
3746918434c8SBill Paul 	 */
374778999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3748918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3749918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
375078999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3751918434c8SBill Paul 	}
3752918434c8SBill Paul 
3753918434c8SBill Paul 	/*
375496f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
375596f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
375696f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
375796f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
375896f2e892SBill Paul 	 */
375996f2e892SBill Paul 	dc_setfilt(sc);
376096f2e892SBill Paul 
376196f2e892SBill Paul 	/* Enable receiver. */
376296f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
376396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
376496f2e892SBill Paul 
376513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
376613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
376796f2e892SBill Paul 
3768d7e9ac75SPyun YongHyeon 	dc_ifmedia_upd_locked(sc);
3769d314ebf5SPyun YongHyeon 
377026b40a65SPyun YongHyeon 	/* Clear missed frames and overflow counter. */
377126b40a65SPyun YongHyeon 	CSR_READ_4(sc, DC_FRAMESDISCARDED);
377226b40a65SPyun YongHyeon 
3773857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
377445521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3775857fd445SBill Paul 		sc->dc_link = 1;
3776857fd445SBill Paul 	else {
3777318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3778b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3779318b02fdSBill Paul 		else
3780b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3781857fd445SBill Paul 	}
3782b1d16143SMarius Strobl 
3783b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
3784b1d16143SMarius Strobl 	callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
378596f2e892SBill Paul }
378696f2e892SBill Paul 
378796f2e892SBill Paul /*
378896f2e892SBill Paul  * Set media options.
378996f2e892SBill Paul  */
3790e3d2833aSAlfred Perlstein static int
37910934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
379296f2e892SBill Paul {
379396f2e892SBill Paul 	struct dc_softc *sc;
3794d7e9ac75SPyun YongHyeon 	int error;
379596f2e892SBill Paul 
379696f2e892SBill Paul 	sc = ifp->if_softc;
3797c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3798d7e9ac75SPyun YongHyeon 	error = dc_ifmedia_upd_locked(sc);
3799d7e9ac75SPyun YongHyeon 	DC_UNLOCK(sc);
3800d7e9ac75SPyun YongHyeon 	return (error);
3801d7e9ac75SPyun YongHyeon }
3802f43d9309SBill Paul 
3803d7e9ac75SPyun YongHyeon static int
3804d7e9ac75SPyun YongHyeon dc_ifmedia_upd_locked(struct dc_softc *sc)
3805d7e9ac75SPyun YongHyeon {
3806d7e9ac75SPyun YongHyeon 	struct mii_data *mii;
3807d7e9ac75SPyun YongHyeon 	struct ifmedia *ifm;
3808d7e9ac75SPyun YongHyeon 	int error;
3809d7e9ac75SPyun YongHyeon 
3810d7e9ac75SPyun YongHyeon 	DC_LOCK_ASSERT(sc);
3811d7e9ac75SPyun YongHyeon 
3812d7e9ac75SPyun YongHyeon 	sc->dc_link = 0;
3813d7e9ac75SPyun YongHyeon 	mii = device_get_softc(sc->dc_miibus);
3814d7e9ac75SPyun YongHyeon 	error = mii_mediachg(mii);
3815d7e9ac75SPyun YongHyeon 	if (error == 0) {
3816d7e9ac75SPyun YongHyeon 		ifm = &mii->mii_media;
3817d314ebf5SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3818d314ebf5SPyun YongHyeon 			dc_setcfg(sc, ifm->ifm_media);
3819d314ebf5SPyun YongHyeon 		else if (DC_IS_DAVICOM(sc) &&
382045521525SPoul-Henning Kamp 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3821f43d9309SBill Paul 			dc_setcfg(sc, ifm->ifm_media);
3822d7e9ac75SPyun YongHyeon 	}
382396f2e892SBill Paul 
3824d7e9ac75SPyun YongHyeon 	return (error);
382596f2e892SBill Paul }
382696f2e892SBill Paul 
382796f2e892SBill Paul /*
382896f2e892SBill Paul  * Report current media status.
382996f2e892SBill Paul  */
3830e3d2833aSAlfred Perlstein static void
38310934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
383296f2e892SBill Paul {
383396f2e892SBill Paul 	struct dc_softc *sc;
383496f2e892SBill Paul 	struct mii_data *mii;
3835f43d9309SBill Paul 	struct ifmedia *ifm;
383696f2e892SBill Paul 
383796f2e892SBill Paul 	sc = ifp->if_softc;
383896f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3839c8b27acaSJohn Baldwin 	DC_LOCK(sc);
384096f2e892SBill Paul 	mii_pollstat(mii);
3841f43d9309SBill Paul 	ifm = &mii->mii_media;
3842f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
384345521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3844f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3845f43d9309SBill Paul 			ifmr->ifm_status = 0;
3846432120f2SMarius Strobl 			DC_UNLOCK(sc);
3847f43d9309SBill Paul 			return;
3848f43d9309SBill Paul 		}
3849f43d9309SBill Paul 	}
385096f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
385196f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3852c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
385396f2e892SBill Paul }
385496f2e892SBill Paul 
3855e3d2833aSAlfred Perlstein static int
38560934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
385796f2e892SBill Paul {
385896f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
385996f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
386096f2e892SBill Paul 	struct mii_data *mii;
3861d1ce9105SBill Paul 	int error = 0;
386296f2e892SBill Paul 
386396f2e892SBill Paul 	switch (command) {
386496f2e892SBill Paul 	case SIOCSIFFLAGS:
3865c8b27acaSJohn Baldwin 		DC_LOCK(sc);
386696f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
38675d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
38685d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
38695d6dfbbbSLuigi Rizzo 
387013f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
38715d6dfbbbSLuigi Rizzo 				if (need_setfilt)
387296f2e892SBill Paul 					dc_setfilt(sc);
38735d6dfbbbSLuigi Rizzo 			} else {
38748f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3875c8b27acaSJohn Baldwin 				dc_init_locked(sc);
387696f2e892SBill Paul 			}
387796f2e892SBill Paul 		} else {
387813f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
387996f2e892SBill Paul 				dc_stop(sc);
388096f2e892SBill Paul 		}
388196f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3882c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
388396f2e892SBill Paul 		break;
388496f2e892SBill Paul 	case SIOCADDMULTI:
388596f2e892SBill Paul 	case SIOCDELMULTI:
3886c8b27acaSJohn Baldwin 		DC_LOCK(sc);
388724507bc1SPyun YongHyeon 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
388896f2e892SBill Paul 			dc_setfilt(sc);
3889c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
389096f2e892SBill Paul 		break;
389196f2e892SBill Paul 	case SIOCGIFMEDIA:
389296f2e892SBill Paul 	case SIOCSIFMEDIA:
389396f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
389496f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
389596f2e892SBill Paul 		break;
3896e695984eSRuslan Ermilov 	case SIOCSIFCAP:
389740929967SGleb Smirnoff #ifdef DEVICE_POLLING
389840929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
389940929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
390040929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
390140929967SGleb Smirnoff 			if (error)
390240929967SGleb Smirnoff 				return(error);
3903c8b27acaSJohn Baldwin 			DC_LOCK(sc);
390440929967SGleb Smirnoff 			/* Disable interrupts */
390540929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
390640929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3907c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
390840929967SGleb Smirnoff 			return (error);
390940929967SGleb Smirnoff 		}
391040929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
391140929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
391240929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
391340929967SGleb Smirnoff 			/* Enable interrupts. */
391440929967SGleb Smirnoff 			DC_LOCK(sc);
391540929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
391640929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
391740929967SGleb Smirnoff 			DC_UNLOCK(sc);
391840929967SGleb Smirnoff 			return (error);
391940929967SGleb Smirnoff 		}
392040929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3921e695984eSRuslan Ermilov 		break;
392296f2e892SBill Paul 	default:
39239ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
392496f2e892SBill Paul 		break;
392596f2e892SBill Paul 	}
392696f2e892SBill Paul 
392796f2e892SBill Paul 	return (error);
392896f2e892SBill Paul }
392996f2e892SBill Paul 
3930e3d2833aSAlfred Perlstein static void
3931b1d16143SMarius Strobl dc_watchdog(void *xsc)
393296f2e892SBill Paul {
3933b1d16143SMarius Strobl 	struct dc_softc *sc = xsc;
3934b1d16143SMarius Strobl 	struct ifnet *ifp;
393596f2e892SBill Paul 
3936b1d16143SMarius Strobl 	DC_LOCK_ASSERT(sc);
393796f2e892SBill Paul 
3938b1d16143SMarius Strobl 	if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3939b1d16143SMarius Strobl 		callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3940b1d16143SMarius Strobl 		return;
3941b1d16143SMarius Strobl 	}
3942d1ce9105SBill Paul 
3943b1d16143SMarius Strobl 	ifp = sc->dc_ifp;
3944*c8dfaf38SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3945b1d16143SMarius Strobl 	device_printf(sc->dc_dev, "watchdog timeout\n");
394696f2e892SBill Paul 
39478f382a1fSPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3948c8b27acaSJohn Baldwin 	dc_init_locked(sc);
394996f2e892SBill Paul 
3950cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3951c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
395296f2e892SBill Paul }
395396f2e892SBill Paul 
395496f2e892SBill Paul /*
395596f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
395696f2e892SBill Paul  * RX and TX lists.
395796f2e892SBill Paul  */
3958e3d2833aSAlfred Perlstein static void
39590934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
396096f2e892SBill Paul {
396196f2e892SBill Paul 	struct ifnet *ifp;
3962b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3963b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3964b3811c95SMaxime Henrion 	int i;
39651da7683aSPyun YongHyeon 	uint32_t ctl, netcfg;
396696f2e892SBill Paul 
3967c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3968d1ce9105SBill Paul 
3969fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
39705f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
3971b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
397296f2e892SBill Paul 
3973b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
3974b1d16143SMarius Strobl 	callout_stop(&sc->dc_wdog_ch);
3975b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
39761da7683aSPyun YongHyeon 	sc->dc_link = 0;
397796f2e892SBill Paul 
397813f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
39793b3ec200SPeter Wemm 
39801da7683aSPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
39811da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39821da7683aSPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
39831da7683aSPyun YongHyeon 		   netcfg & ~(DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
398496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
39851da7683aSPyun YongHyeon 	/* Wait the completion of TX/RX SM. */
39861da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39871da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
39881da7683aSPyun YongHyeon 
398996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
399096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
399196f2e892SBill Paul 
399296f2e892SBill Paul 	/*
399396f2e892SBill Paul 	 * Free data in the RX lists.
399496f2e892SBill Paul 	 */
399596f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3996b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
39975f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_rx_mtag,
39985f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i], BUS_DMASYNC_POSTREAD);
39995f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_mtag,
40005f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i]);
400156e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
400256e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
400396f2e892SBill Paul 		}
400496f2e892SBill Paul 	}
40055f14ee23SPyun YongHyeon 	bzero(ld->dc_rx_list, DC_RX_LIST_SZ);
40065f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
40075f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
400896f2e892SBill Paul 
400996f2e892SBill Paul 	/*
401096f2e892SBill Paul 	 * Free the TX list buffers.
401196f2e892SBill Paul 	 */
401296f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
4013b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
4014af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
40155f14ee23SPyun YongHyeon 			if (ctl & DC_TXCTL_SETUP) {
40165f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
40175f14ee23SPyun YongHyeon 				    BUS_DMASYNC_POSTWRITE);
40185f14ee23SPyun YongHyeon 			} else {
40195f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_tx_mtag,
40205f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i], BUS_DMASYNC_POSTWRITE);
40215f14ee23SPyun YongHyeon 				bus_dmamap_unload(sc->dc_tx_mtag,
40225f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i]);
402356e5e7aeSMaxime Henrion 				m_freem(cd->dc_tx_chain[i]);
40245f14ee23SPyun YongHyeon 			}
4025b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
402696f2e892SBill Paul 		}
402796f2e892SBill Paul 	}
40285f14ee23SPyun YongHyeon 	bzero(ld->dc_tx_list, DC_TX_LIST_SZ);
40295f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
40305f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
403196f2e892SBill Paul }
403296f2e892SBill Paul 
403396f2e892SBill Paul /*
4034e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
4035e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
4036e8388e14SMitsuru IWASAKI  * resume.
4037e8388e14SMitsuru IWASAKI  */
4038e3d2833aSAlfred Perlstein static int
40390934f18aSMaxime Henrion dc_suspend(device_t dev)
4040e8388e14SMitsuru IWASAKI {
4041e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4042e8388e14SMitsuru IWASAKI 
4043e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4044c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4045e8388e14SMitsuru IWASAKI 	dc_stop(sc);
4046e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
4047c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4048e8388e14SMitsuru IWASAKI 
4049e8388e14SMitsuru IWASAKI 	return (0);
4050e8388e14SMitsuru IWASAKI }
4051e8388e14SMitsuru IWASAKI 
4052e8388e14SMitsuru IWASAKI /*
4053e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
4054e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
4055e8388e14SMitsuru IWASAKI  * appropriate.
4056e8388e14SMitsuru IWASAKI  */
4057e3d2833aSAlfred Perlstein static int
40580934f18aSMaxime Henrion dc_resume(device_t dev)
4059e8388e14SMitsuru IWASAKI {
4060e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4061e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
4062e8388e14SMitsuru IWASAKI 
4063e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4064fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
4065e8388e14SMitsuru IWASAKI 
4066e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
4067c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4068e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
4069c8b27acaSJohn Baldwin 		dc_init_locked(sc);
4070e8388e14SMitsuru IWASAKI 
4071e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
4072c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4073e8388e14SMitsuru IWASAKI 
4074e8388e14SMitsuru IWASAKI 	return (0);
4075e8388e14SMitsuru IWASAKI }
4076e8388e14SMitsuru IWASAKI 
4077e8388e14SMitsuru IWASAKI /*
407896f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
407996f2e892SBill Paul  * get confused by errant DMAs when rebooting.
408096f2e892SBill Paul  */
40816a087a87SPyun YongHyeon static int
40820934f18aSMaxime Henrion dc_shutdown(device_t dev)
408396f2e892SBill Paul {
408496f2e892SBill Paul 	struct dc_softc *sc;
408596f2e892SBill Paul 
408696f2e892SBill Paul 	sc = device_get_softc(dev);
408796f2e892SBill Paul 
4088c8b27acaSJohn Baldwin 	DC_LOCK(sc);
408996f2e892SBill Paul 	dc_stop(sc);
4090c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
40916a087a87SPyun YongHyeon 
40926a087a87SPyun YongHyeon 	return (0);
409396f2e892SBill Paul }
409439d76ed6SPyun YongHyeon 
409539d76ed6SPyun YongHyeon static int
409639d76ed6SPyun YongHyeon dc_check_multiport(struct dc_softc *sc)
409739d76ed6SPyun YongHyeon {
409839d76ed6SPyun YongHyeon 	struct dc_softc *dsc;
409939d76ed6SPyun YongHyeon 	devclass_t dc;
410039d76ed6SPyun YongHyeon 	device_t child;
410139d76ed6SPyun YongHyeon 	uint8_t *eaddr;
410239d76ed6SPyun YongHyeon 	int unit;
410339d76ed6SPyun YongHyeon 
410439d76ed6SPyun YongHyeon 	dc = devclass_find("dc");
410539d76ed6SPyun YongHyeon 	for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
410639d76ed6SPyun YongHyeon 		child = devclass_get_device(dc, unit);
410739d76ed6SPyun YongHyeon 		if (child == NULL)
410839d76ed6SPyun YongHyeon 			continue;
410939d76ed6SPyun YongHyeon 		if (child == sc->dc_dev)
411039d76ed6SPyun YongHyeon 			continue;
411139d76ed6SPyun YongHyeon 		if (device_get_parent(child) != device_get_parent(sc->dc_dev))
411239d76ed6SPyun YongHyeon 			continue;
411339d76ed6SPyun YongHyeon 		if (unit > device_get_unit(sc->dc_dev))
411439d76ed6SPyun YongHyeon 			continue;
4115b289c607SPyun YongHyeon 		if (device_is_attached(child) == 0)
4116b289c607SPyun YongHyeon 			continue;
411739d76ed6SPyun YongHyeon 		dsc = device_get_softc(child);
4118b289c607SPyun YongHyeon 		device_printf(sc->dc_dev,
4119b289c607SPyun YongHyeon 		    "Using station address of %s as base\n",
412039d76ed6SPyun YongHyeon 		    device_get_nameunit(child));
412139d76ed6SPyun YongHyeon 		bcopy(dsc->dc_eaddr, sc->dc_eaddr, ETHER_ADDR_LEN);
412239d76ed6SPyun YongHyeon 		eaddr = (uint8_t *)sc->dc_eaddr;
412339d76ed6SPyun YongHyeon 		eaddr[5]++;
4124b289c607SPyun YongHyeon 		/* Prepare SROM to parse again. */
4125b289c607SPyun YongHyeon 		if (DC_IS_INTEL(sc) && dsc->dc_srom != NULL &&
4126b289c607SPyun YongHyeon 		    sc->dc_romwidth != 0) {
4127b289c607SPyun YongHyeon 			free(sc->dc_srom, M_DEVBUF);
4128b289c607SPyun YongHyeon 			sc->dc_romwidth = dsc->dc_romwidth;
4129b289c607SPyun YongHyeon 			sc->dc_srom = malloc(DC_ROM_SIZE(sc->dc_romwidth),
4130b289c607SPyun YongHyeon 			    M_DEVBUF, M_NOWAIT);
4131b289c607SPyun YongHyeon 			if (sc->dc_srom == NULL) {
4132b289c607SPyun YongHyeon 				device_printf(sc->dc_dev,
4133b289c607SPyun YongHyeon 				    "Could not allocate SROM buffer\n");
4134b289c607SPyun YongHyeon 				return (ENOMEM);
4135b289c607SPyun YongHyeon 			}
4136b289c607SPyun YongHyeon 			bcopy(dsc->dc_srom, sc->dc_srom,
4137b289c607SPyun YongHyeon 			    DC_ROM_SIZE(sc->dc_romwidth));
4138b289c607SPyun YongHyeon 		}
413939d76ed6SPyun YongHyeon 		return (0);
414039d76ed6SPyun YongHyeon 	}
414139d76ed6SPyun YongHyeon 	return (ENOENT);
414239d76ed6SPyun YongHyeon }
4143