xref: /freebsd/sys/dev/dc/if_dc.c (revision df57947f083046d50552e99b91074927d2458708)
160727d8bSWarner Losh /*-
2*df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3*df57947fSPedro F. Giffuni  *
496f2e892SBill Paul  * Copyright (c) 1997, 1998, 1999
596f2e892SBill Paul  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
696f2e892SBill Paul  *
796f2e892SBill Paul  * Redistribution and use in source and binary forms, with or without
896f2e892SBill Paul  * modification, are permitted provided that the following conditions
996f2e892SBill Paul  * are met:
1096f2e892SBill Paul  * 1. Redistributions of source code must retain the above copyright
1196f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer.
1296f2e892SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1396f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1496f2e892SBill Paul  *    documentation and/or other materials provided with the distribution.
1596f2e892SBill Paul  * 3. All advertising materials mentioning features or use of this software
1696f2e892SBill Paul  *    must display the following acknowledgement:
1796f2e892SBill Paul  *	This product includes software developed by Bill Paul.
1896f2e892SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1996f2e892SBill Paul  *    may be used to endorse or promote products derived from this software
2096f2e892SBill Paul  *    without specific prior written permission.
2196f2e892SBill Paul  *
2296f2e892SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2396f2e892SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2496f2e892SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2596f2e892SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2696f2e892SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2796f2e892SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2896f2e892SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2996f2e892SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3096f2e892SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3196f2e892SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3296f2e892SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3396f2e892SBill Paul  */
3496f2e892SBill Paul 
354dc52c32SDavid E. O'Brien #include <sys/cdefs.h>
364dc52c32SDavid E. O'Brien __FBSDID("$FreeBSD$");
374dc52c32SDavid E. O'Brien 
3896f2e892SBill Paul /*
3996f2e892SBill Paul  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
4096f2e892SBill Paul  * series chips and several workalikes including the following:
4196f2e892SBill Paul  *
42ead7cde9SBill Paul  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
4396f2e892SBill Paul  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
4496f2e892SBill Paul  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
4596f2e892SBill Paul  * ASIX Electronics AX88140A (www.asix.com.tw)
4696f2e892SBill Paul  * ASIX Electronics AX88141 (www.asix.com.tw)
4796f2e892SBill Paul  * ADMtek AL981 (www.admtek.com.tw)
48593a1aeaSMartin Blapp  * ADMtek AN983 (www.admtek.com.tw)
49a2d61e43SWarner Losh  * ADMtek CardBus AN985 (www.admtek.com.tw)
50a2d61e43SWarner Losh  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek CardBus AN985
5188d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
529ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
53feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
541d5e5310SBill Paul  * Abocom FE2500
551af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
567eac366bSMartin Blapp  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
5796f2e892SBill Paul  *
5896f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5996f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
6096f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
6196f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
6296f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
6396f2e892SBill Paul  * instead of 512.
6496f2e892SBill Paul  *
6596f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
6696f2e892SBill Paul  * Electrical Engineering Department
6796f2e892SBill Paul  * Columbia University, New York City
6896f2e892SBill Paul  */
6996f2e892SBill Paul /*
7096f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
7196f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
7296f2e892SBill Paul  * three kinds of media attachments:
7396f2e892SBill Paul  *
7496f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
7596f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7696f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7796f2e892SBill Paul  * o 10baseT port.
7896f2e892SBill Paul  * o AUI/BNC port.
7996f2e892SBill Paul  *
8096f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
8196f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
8296f2e892SBill Paul  * autosensing configuration.
8396f2e892SBill Paul  *
8496f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
8596f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8696f2e892SBill Paul  * handled separately due to its different register offsets and the
8796f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8896f2e892SBill Paul  * here, but I'm not thrilled about it.
8996f2e892SBill Paul  *
9096f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
9196f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
9296f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
9396f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
9496f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
9596f2e892SBill Paul  */
9696f2e892SBill Paul 
97f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
98f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
99f0796cd2SGleb Smirnoff #endif
100f0796cd2SGleb Smirnoff 
10196f2e892SBill Paul #include <sys/param.h>
102af4358c7SMaxime Henrion #include <sys/endian.h>
10396f2e892SBill Paul #include <sys/systm.h>
10496f2e892SBill Paul #include <sys/sockio.h>
10596f2e892SBill Paul #include <sys/mbuf.h>
10696f2e892SBill Paul #include <sys/malloc.h>
10796f2e892SBill Paul #include <sys/kernel.h>
108f11d01c3SPoul-Henning Kamp #include <sys/module.h>
10996f2e892SBill Paul #include <sys/socket.h>
11096f2e892SBill Paul 
11196f2e892SBill Paul #include <net/if.h>
11276039bc8SGleb Smirnoff #include <net/if_var.h>
11396f2e892SBill Paul #include <net/if_arp.h>
11496f2e892SBill Paul #include <net/ethernet.h>
11596f2e892SBill Paul #include <net/if_dl.h>
11696f2e892SBill Paul #include <net/if_media.h>
117db40c1aeSDoug Ambrisko #include <net/if_types.h>
118db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
11996f2e892SBill Paul 
12096f2e892SBill Paul #include <net/bpf.h>
12196f2e892SBill Paul 
12296f2e892SBill Paul #include <machine/bus.h>
12396f2e892SBill Paul #include <machine/resource.h>
12496f2e892SBill Paul #include <sys/bus.h>
12596f2e892SBill Paul #include <sys/rman.h>
12696f2e892SBill Paul 
12796f2e892SBill Paul #include <dev/mii/mii.h>
1288c1093fcSMarius Strobl #include <dev/mii/mii_bitbang.h>
12996f2e892SBill Paul #include <dev/mii/miivar.h>
13096f2e892SBill Paul 
13119b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
13219b7ffd1SWarner Losh #include <dev/pci/pcivar.h>
13396f2e892SBill Paul 
13496f2e892SBill Paul #define	DC_USEIOSPACE
13596f2e892SBill Paul 
1366a3033a8SWarner Losh #include <dev/dc/if_dcreg.h>
13796f2e892SBill Paul 
138ec6a7299SMaxime Henrion #ifdef __sparc64__
139ec6a7299SMaxime Henrion #include <dev/ofw/openfirm.h>
140ec6a7299SMaxime Henrion #include <machine/ofw_machdep.h>
141ec6a7299SMaxime Henrion #endif
142ec6a7299SMaxime Henrion 
143f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, pci, 1, 1, 1);
144f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, ether, 1, 1, 1);
14595a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
14695a16455SPeter Wemm 
147919ccba7SWarner Losh /*
148919ccba7SWarner Losh  * "device miibus" is required in kernel config.  See GENERIC if you get
149919ccba7SWarner Losh  * errors here.
150919ccba7SWarner Losh  */
15196f2e892SBill Paul #include "miibus_if.h"
15296f2e892SBill Paul 
15396f2e892SBill Paul /*
15496f2e892SBill Paul  * Various supported device vendors/types and their names.
15596f2e892SBill Paul  */
15629658c96SDimitry Andric static const struct dc_type dc_devs[] = {
1571e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143), 0,
15896f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
1591e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009), 0,
16038deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
1611e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100), 0,
16296f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
1631e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), DC_REVISION_DM9102A,
16488d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
1651e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), 0,
1661e2e70b1SJohn Baldwin 		"Davicom DM9102 10/100BaseTX" },
1671e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981), 0,
16896f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
169593a1aeaSMartin Blapp 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983), 0,
170593a1aeaSMartin Blapp 		"ADMtek AN983 10/100BaseTX" },
1711e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
172a2d61e43SWarner Losh 		"ADMtek AN985 CardBus 10/100BaseTX or clone" },
1731e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
174e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
1751e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
176e351d778SMartin Blapp 		"ADMtek ADM9513 10/100BaseTX" },
1771e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), DC_REVISION_88141,
17896f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
1791e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), 0,
1801e2e70b1SJohn Baldwin 		"ASIX AX88140A 10/100BaseTX" },
1811e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), DC_REVISION_98713A,
18296f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
1831e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), 0,
1841e2e70b1SJohn Baldwin 		"Macronix 98713 10/100BaseTX" },
1851e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), DC_REVISION_98713A,
18696f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1871e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), 0,
18896f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1891e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98725,
19096f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
1911e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98715AEC_C,
1921e2e70b1SJohn Baldwin 		"Macronix 98715AEC-C 10/100BaseTX" },
1931e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), 0,
1941e2e70b1SJohn Baldwin 		"Macronix 98715/98715A 10/100BaseTX" },
1951e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727), 0,
196ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
1971e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115), 0,
19896f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
1991e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), DC_REVISION_82C169,
20096f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
2011e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), 0,
2021e2e70b1SJohn Baldwin 		"82c168 PNIC 10/100BaseTX" },
2031e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217), 0,
2049ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
2051e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242), 0,
206fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
2071e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201), 0,
208feb78939SJonathan Chen 		"Xircom X3201 10/100BaseTX" },
2091e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD), 0,
2109be0993cSJohn Baldwin 		"Neteasy DRP-32TXD Cardbus 10/100" },
2111e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500), 0,
2121d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
2131e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX), 0,
214773c505fSMIHIRA Sanpei Yoshiro 		"Abocom FE2500MX 10/100BaseTX" },
2151e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112), 0,
2161af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
2171e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX), 0,
218948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
2191e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T), 0,
22097f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2211e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB), 0,
2227eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
2231e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120), 0,
224e7b9ab3aSBill Paul 		"Microsoft MN-120 CardBus 10/100" },
2251e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130), 0,
226e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
22717762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08), 0,
22817762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22917762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09), 0,
23017762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
23152ca7ee2SPyun YongHyeon 	{ DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261), 0,
23252ca7ee2SPyun YongHyeon 		"ULi M5261 FastEthernet" },
23352ca7ee2SPyun YongHyeon 	{ DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263), 0,
23452ca7ee2SPyun YongHyeon 		"ULi M5263 FastEthernet" },
23596f2e892SBill Paul 	{ 0, 0, NULL }
23696f2e892SBill Paul };
23796f2e892SBill Paul 
238e51a25f8SAlfred Perlstein static int dc_probe(device_t);
239e51a25f8SAlfred Perlstein static int dc_attach(device_t);
240e51a25f8SAlfred Perlstein static int dc_detach(device_t);
241e8388e14SMitsuru IWASAKI static int dc_suspend(device_t);
242e8388e14SMitsuru IWASAKI static int dc_resume(device_t);
243ebc284ccSMarius Strobl static const struct dc_type *dc_devtype(device_t);
2445f14ee23SPyun YongHyeon static void dc_discard_rxbuf(struct dc_softc *, int);
2455f14ee23SPyun YongHyeon static int dc_newbuf(struct dc_softc *, int);
246a10c0e45SMike Silbersack static int dc_encap(struct dc_softc *, struct mbuf **);
247e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war(struct dc_softc *, int);
248e51a25f8SAlfred Perlstein static int dc_rx_resync(struct dc_softc *);
2491abcdbd1SAttilio Rao static int dc_rxeof(struct dc_softc *);
250e51a25f8SAlfred Perlstein static void dc_txeof(struct dc_softc *);
251e51a25f8SAlfred Perlstein static void dc_tick(void *);
252e51a25f8SAlfred Perlstein static void dc_tx_underrun(struct dc_softc *);
253e51a25f8SAlfred Perlstein static void dc_intr(void *);
254e51a25f8SAlfred Perlstein static void dc_start(struct ifnet *);
255c8b27acaSJohn Baldwin static void dc_start_locked(struct ifnet *);
256e51a25f8SAlfred Perlstein static int dc_ioctl(struct ifnet *, u_long, caddr_t);
257e51a25f8SAlfred Perlstein static void dc_init(void *);
258c8b27acaSJohn Baldwin static void dc_init_locked(struct dc_softc *);
259e51a25f8SAlfred Perlstein static void dc_stop(struct dc_softc *);
260b1d16143SMarius Strobl static void dc_watchdog(void *);
2616a087a87SPyun YongHyeon static int dc_shutdown(device_t);
262e51a25f8SAlfred Perlstein static int dc_ifmedia_upd(struct ifnet *);
263d7e9ac75SPyun YongHyeon static int dc_ifmedia_upd_locked(struct dc_softc *);
264e51a25f8SAlfred Perlstein static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
26596f2e892SBill Paul 
2665f14ee23SPyun YongHyeon static int dc_dma_alloc(struct dc_softc *);
2675f14ee23SPyun YongHyeon static void dc_dma_free(struct dc_softc *);
2685f14ee23SPyun YongHyeon static void dc_dma_map_addr(void *, bus_dma_segment_t *, int, int);
2695f14ee23SPyun YongHyeon 
270e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
271e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
272e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
273ee320f98SPyun YongHyeon static void dc_eeprom_getword(struct dc_softc *, int, uint16_t *);
274ee320f98SPyun YongHyeon static void dc_eeprom_getword_pnic(struct dc_softc *, int, uint16_t *);
275ee320f98SPyun YongHyeon static void dc_eeprom_getword_xircom(struct dc_softc *, int, uint16_t *);
2763097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
277e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
27896f2e892SBill Paul 
279e51a25f8SAlfred Perlstein static int dc_miibus_readreg(device_t, int, int);
280e51a25f8SAlfred Perlstein static int dc_miibus_writereg(device_t, int, int, int);
281e51a25f8SAlfred Perlstein static void dc_miibus_statchg(device_t);
282e51a25f8SAlfred Perlstein static void dc_miibus_mediainit(device_t);
28396f2e892SBill Paul 
284e51a25f8SAlfred Perlstein static void dc_setcfg(struct dc_softc *, int);
2851da7683aSPyun YongHyeon static void dc_netcfg_wait(struct dc_softc *);
2863373489bSWarner Losh static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
2873373489bSWarner Losh static uint32_t dc_mchash_be(const uint8_t *);
288e51a25f8SAlfred Perlstein static void dc_setfilt_21143(struct dc_softc *);
289e51a25f8SAlfred Perlstein static void dc_setfilt_asix(struct dc_softc *);
290e51a25f8SAlfred Perlstein static void dc_setfilt_admtek(struct dc_softc *);
29152ca7ee2SPyun YongHyeon static void dc_setfilt_uli(struct dc_softc *);
292e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
29396f2e892SBill Paul 
294e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
29596f2e892SBill Paul 
296e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
297e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
298e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
29996f2e892SBill Paul 
300abe4e865SPyun YongHyeon static int dc_read_srom(struct dc_softc *, int);
301abe4e865SPyun YongHyeon static int dc_parse_21143_srom(struct dc_softc *);
302abe4e865SPyun YongHyeon static int dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
303abe4e865SPyun YongHyeon static int dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
304abe4e865SPyun YongHyeon static int dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
305e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
30639d76ed6SPyun YongHyeon static int dc_check_multiport(struct dc_softc *);
3075c1cfac4SBill Paul 
3088c1093fcSMarius Strobl /*
3098c1093fcSMarius Strobl  * MII bit-bang glue
3108c1093fcSMarius Strobl  */
3118c1093fcSMarius Strobl static uint32_t dc_mii_bitbang_read(device_t);
3128c1093fcSMarius Strobl static void dc_mii_bitbang_write(device_t, uint32_t);
3138c1093fcSMarius Strobl 
3148c1093fcSMarius Strobl static const struct mii_bitbang_ops dc_mii_bitbang_ops = {
3158c1093fcSMarius Strobl 	dc_mii_bitbang_read,
3168c1093fcSMarius Strobl 	dc_mii_bitbang_write,
3178c1093fcSMarius Strobl 	{
3188c1093fcSMarius Strobl 		DC_SIO_MII_DATAOUT,	/* MII_BIT_MDO */
3198c1093fcSMarius Strobl 		DC_SIO_MII_DATAIN,	/* MII_BIT_MDI */
3208c1093fcSMarius Strobl 		DC_SIO_MII_CLK,		/* MII_BIT_MDC */
3218c1093fcSMarius Strobl 		0,			/* MII_BIT_DIR_HOST_PHY */
3228c1093fcSMarius Strobl 		DC_SIO_MII_DIR,		/* MII_BIT_DIR_PHY_HOST */
3238c1093fcSMarius Strobl 	}
3248c1093fcSMarius Strobl };
3258c1093fcSMarius Strobl 
32696f2e892SBill Paul #ifdef DC_USEIOSPACE
32796f2e892SBill Paul #define	DC_RES			SYS_RES_IOPORT
32896f2e892SBill Paul #define	DC_RID			DC_PCI_CFBIO
32996f2e892SBill Paul #else
33096f2e892SBill Paul #define	DC_RES			SYS_RES_MEMORY
33196f2e892SBill Paul #define	DC_RID			DC_PCI_CFBMA
33296f2e892SBill Paul #endif
33396f2e892SBill Paul 
33496f2e892SBill Paul static device_method_t dc_methods[] = {
33596f2e892SBill Paul 	/* Device interface */
33696f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
33796f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
33896f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
339e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
340e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
34196f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
34296f2e892SBill Paul 
34396f2e892SBill Paul 	/* MII interface */
34496f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
34596f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
34696f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
347f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
34896f2e892SBill Paul 
3494b7ec270SMarius Strobl 	DEVMETHOD_END
35096f2e892SBill Paul };
35196f2e892SBill Paul 
35296f2e892SBill Paul static driver_t dc_driver = {
35396f2e892SBill Paul 	"dc",
35496f2e892SBill Paul 	dc_methods,
35596f2e892SBill Paul 	sizeof(struct dc_softc)
35696f2e892SBill Paul };
35796f2e892SBill Paul 
35896f2e892SBill Paul static devclass_t dc_devclass;
35996f2e892SBill Paul 
360e4029d4cSMarius Strobl DRIVER_MODULE_ORDERED(dc, pci, dc_driver, dc_devclass, NULL, NULL,
361e4029d4cSMarius Strobl     SI_ORDER_ANY);
362e4029d4cSMarius Strobl DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, NULL, NULL);
36396f2e892SBill Paul 
36496f2e892SBill Paul #define	DC_SETBIT(sc, reg, x)				\
36596f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
36696f2e892SBill Paul 
36796f2e892SBill Paul #define	DC_CLRBIT(sc, reg, x)				\
36896f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
36996f2e892SBill Paul 
37096f2e892SBill Paul #define	SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
37196f2e892SBill Paul #define	SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
37296f2e892SBill Paul 
373e3d2833aSAlfred Perlstein static void
3740934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
37596f2e892SBill Paul {
37696f2e892SBill Paul 	int idx;
37796f2e892SBill Paul 
37896f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
37996f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
38096f2e892SBill Paul }
38196f2e892SBill Paul 
3822c876e15SPoul-Henning Kamp static void
3830934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3843097aa70SWarner Losh {
3853097aa70SWarner Losh 	int i;
3863097aa70SWarner Losh 
3873097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3883097aa70SWarner Losh 	dc_eeprom_idle(sc);
3893097aa70SWarner Losh 
3903097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3913097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3923097aa70SWarner Losh 	dc_delay(sc);
3933097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3943097aa70SWarner Losh 	dc_delay(sc);
3953097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3963097aa70SWarner Losh 	dc_delay(sc);
3973097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3983097aa70SWarner Losh 	dc_delay(sc);
3993097aa70SWarner Losh 
4003097aa70SWarner Losh 	for (i = 3; i--;) {
4013097aa70SWarner Losh 		if (6 & (1 << i))
4023097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4033097aa70SWarner Losh 		else
4043097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4053097aa70SWarner Losh 		dc_delay(sc);
4063097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4073097aa70SWarner Losh 		dc_delay(sc);
4083097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4093097aa70SWarner Losh 		dc_delay(sc);
4103097aa70SWarner Losh 	}
4113097aa70SWarner Losh 
4123097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
4133097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4143097aa70SWarner Losh 		dc_delay(sc);
4153097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
4163097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4173097aa70SWarner Losh 			dc_delay(sc);
4183097aa70SWarner Losh 			break;
4193097aa70SWarner Losh 		}
4203097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4213097aa70SWarner Losh 		dc_delay(sc);
4223097aa70SWarner Losh 	}
4233097aa70SWarner Losh 
4243097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4253097aa70SWarner Losh 	dc_eeprom_idle(sc);
4263097aa70SWarner Losh 
4273097aa70SWarner Losh 	if (i < 4 || i > 12)
4283097aa70SWarner Losh 		sc->dc_romwidth = 6;
4293097aa70SWarner Losh 	else
4303097aa70SWarner Losh 		sc->dc_romwidth = i;
4313097aa70SWarner Losh 
4323097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4333097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4343097aa70SWarner Losh 	dc_delay(sc);
4353097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4363097aa70SWarner Losh 	dc_delay(sc);
4373097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4383097aa70SWarner Losh 	dc_delay(sc);
4393097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4403097aa70SWarner Losh 	dc_delay(sc);
4413097aa70SWarner Losh 
4423097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4433097aa70SWarner Losh 	dc_eeprom_idle(sc);
4443097aa70SWarner Losh }
4453097aa70SWarner Losh 
446e3d2833aSAlfred Perlstein static void
4470934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
44896f2e892SBill Paul {
4490934f18aSMaxime Henrion 	int i;
45096f2e892SBill Paul 
45196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
45296f2e892SBill Paul 	dc_delay(sc);
45396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
45496f2e892SBill Paul 	dc_delay(sc);
45596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45696f2e892SBill Paul 	dc_delay(sc);
45796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
45896f2e892SBill Paul 	dc_delay(sc);
45996f2e892SBill Paul 
46096f2e892SBill Paul 	for (i = 0; i < 25; i++) {
46196f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46296f2e892SBill Paul 		dc_delay(sc);
46396f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46496f2e892SBill Paul 		dc_delay(sc);
46596f2e892SBill Paul 	}
46696f2e892SBill Paul 
46796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46896f2e892SBill Paul 	dc_delay(sc);
46996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
47096f2e892SBill Paul 	dc_delay(sc);
47196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
47296f2e892SBill Paul }
47396f2e892SBill Paul 
47496f2e892SBill Paul /*
47596f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
47696f2e892SBill Paul  */
477e3d2833aSAlfred Perlstein static void
4780934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
47996f2e892SBill Paul {
4800934f18aSMaxime Henrion 	int d, i;
48196f2e892SBill Paul 
4823097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4833097aa70SWarner Losh 	for (i = 3; i--; ) {
4843097aa70SWarner Losh 		if (d & (1 << i))
4853097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
48696f2e892SBill Paul 		else
4873097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4883097aa70SWarner Losh 		dc_delay(sc);
4893097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4903097aa70SWarner Losh 		dc_delay(sc);
4913097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4923097aa70SWarner Losh 		dc_delay(sc);
4933097aa70SWarner Losh 	}
49496f2e892SBill Paul 
49596f2e892SBill Paul 	/*
49696f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
49796f2e892SBill Paul 	 */
4983097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4993097aa70SWarner Losh 		if (addr & (1 << i)) {
50096f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
50196f2e892SBill Paul 		} else {
50296f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
50396f2e892SBill Paul 		}
50496f2e892SBill Paul 		dc_delay(sc);
50596f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
50696f2e892SBill Paul 		dc_delay(sc);
50796f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
50896f2e892SBill Paul 		dc_delay(sc);
50996f2e892SBill Paul 	}
51096f2e892SBill Paul }
51196f2e892SBill Paul 
51296f2e892SBill Paul /*
51396f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
51496f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
51596f2e892SBill Paul  * the EEPROM.
51696f2e892SBill Paul  */
517e3d2833aSAlfred Perlstein static void
518ee320f98SPyun YongHyeon dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, uint16_t *dest)
51996f2e892SBill Paul {
5200934f18aSMaxime Henrion 	int i;
521ee320f98SPyun YongHyeon 	uint32_t r;
52296f2e892SBill Paul 
52396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
52496f2e892SBill Paul 
52596f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
52696f2e892SBill Paul 		DELAY(1);
52796f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
52896f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
529ee320f98SPyun YongHyeon 			*dest = (uint16_t)(r & 0xFFFF);
53096f2e892SBill Paul 			return;
53196f2e892SBill Paul 		}
53296f2e892SBill Paul 	}
53396f2e892SBill Paul }
53496f2e892SBill Paul 
53596f2e892SBill Paul /*
53696f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
537feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
538feb78939SJonathan Chen  * the EEPROM, too.
539feb78939SJonathan Chen  */
540e3d2833aSAlfred Perlstein static void
541ee320f98SPyun YongHyeon dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, uint16_t *dest)
542feb78939SJonathan Chen {
5430934f18aSMaxime Henrion 
544feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
545feb78939SJonathan Chen 
546feb78939SJonathan Chen 	addr *= 2;
547feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
548ee320f98SPyun YongHyeon 	*dest = (uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
549feb78939SJonathan Chen 	addr += 1;
550feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
551ee320f98SPyun YongHyeon 	*dest |= ((uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
552feb78939SJonathan Chen 
553feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
554feb78939SJonathan Chen }
555feb78939SJonathan Chen 
556feb78939SJonathan Chen /*
557feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
55896f2e892SBill Paul  */
559e3d2833aSAlfred Perlstein static void
560ee320f98SPyun YongHyeon dc_eeprom_getword(struct dc_softc *sc, int addr, uint16_t *dest)
56196f2e892SBill Paul {
5620934f18aSMaxime Henrion 	int i;
563ee320f98SPyun YongHyeon 	uint16_t word = 0;
56496f2e892SBill Paul 
56596f2e892SBill Paul 	/* Force EEPROM to idle state. */
56696f2e892SBill Paul 	dc_eeprom_idle(sc);
56796f2e892SBill Paul 
56896f2e892SBill Paul 	/* Enter EEPROM access mode. */
56996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
57096f2e892SBill Paul 	dc_delay(sc);
57196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
57296f2e892SBill Paul 	dc_delay(sc);
57396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
57496f2e892SBill Paul 	dc_delay(sc);
57596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
57696f2e892SBill Paul 	dc_delay(sc);
57796f2e892SBill Paul 
57896f2e892SBill Paul 	/*
57996f2e892SBill Paul 	 * Send address of word we want to read.
58096f2e892SBill Paul 	 */
58196f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
58296f2e892SBill Paul 
58396f2e892SBill Paul 	/*
58496f2e892SBill Paul 	 * Start reading bits from EEPROM.
58596f2e892SBill Paul 	 */
58696f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
58796f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
58896f2e892SBill Paul 		dc_delay(sc);
58996f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
59096f2e892SBill Paul 			word |= i;
59196f2e892SBill Paul 		dc_delay(sc);
59296f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
59396f2e892SBill Paul 		dc_delay(sc);
59496f2e892SBill Paul 	}
59596f2e892SBill Paul 
59696f2e892SBill Paul 	/* Turn off EEPROM access mode. */
59796f2e892SBill Paul 	dc_eeprom_idle(sc);
59896f2e892SBill Paul 
59996f2e892SBill Paul 	*dest = word;
60096f2e892SBill Paul }
60196f2e892SBill Paul 
60296f2e892SBill Paul /*
60396f2e892SBill Paul  * Read a sequence of words from the EEPROM.
60496f2e892SBill Paul  */
605e3d2833aSAlfred Perlstein static void
6068c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
60796f2e892SBill Paul {
60896f2e892SBill Paul 	int i;
609ee320f98SPyun YongHyeon 	uint16_t word = 0, *ptr;
61096f2e892SBill Paul 
61196f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
61296f2e892SBill Paul 		if (DC_IS_PNIC(sc))
61396f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
614feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
615feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
61696f2e892SBill Paul 		else
61796f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
618ee320f98SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
6198c7ff1f3SMaxime Henrion 		if (be)
6208c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
62196f2e892SBill Paul 		else
6228c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
62396f2e892SBill Paul 	}
62496f2e892SBill Paul }
62596f2e892SBill Paul 
62696f2e892SBill Paul /*
6278c1093fcSMarius Strobl  * Write the MII serial port for the MII bit-bang module.
62896f2e892SBill Paul  */
629e3d2833aSAlfred Perlstein static void
6308c1093fcSMarius Strobl dc_mii_bitbang_write(device_t dev, uint32_t val)
63196f2e892SBill Paul {
6328c1093fcSMarius Strobl 	struct dc_softc *sc;
6330934f18aSMaxime Henrion 
6348c1093fcSMarius Strobl 	sc = device_get_softc(dev);
63596f2e892SBill Paul 
6368c1093fcSMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, val);
63715578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
63815578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
63996f2e892SBill Paul }
64096f2e892SBill Paul 
64196f2e892SBill Paul /*
6428c1093fcSMarius Strobl  * Read the MII serial port for the MII bit-bang module.
64396f2e892SBill Paul  */
6448c1093fcSMarius Strobl static uint32_t
6458c1093fcSMarius Strobl dc_mii_bitbang_read(device_t dev)
64696f2e892SBill Paul {
6478c1093fcSMarius Strobl 	struct dc_softc *sc;
6488c1093fcSMarius Strobl 	uint32_t val;
6490934f18aSMaxime Henrion 
6508c1093fcSMarius Strobl 	sc = device_get_softc(dev);
6518c1093fcSMarius Strobl 
6528c1093fcSMarius Strobl 	val = CSR_READ_4(sc, DC_SIO);
65315578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
65415578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
65596f2e892SBill Paul 
6568c1093fcSMarius Strobl 	return (val);
65796f2e892SBill Paul }
65896f2e892SBill Paul 
659e3d2833aSAlfred Perlstein static int
6600934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
66196f2e892SBill Paul {
66296f2e892SBill Paul 	struct dc_softc *sc;
663c85c4667SBill Paul 	int i, rval, phy_reg = 0;
66496f2e892SBill Paul 
66596f2e892SBill Paul 	sc = device_get_softc(dev);
66696f2e892SBill Paul 
6675c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
66896f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
66996f2e892SBill Paul 			switch (reg) {
67096f2e892SBill Paul 			case MII_BMSR:
67196f2e892SBill Paul 			/*
67296f2e892SBill Paul 			 * Fake something to make the probe
67396f2e892SBill Paul 			 * code think there's a PHY here.
67496f2e892SBill Paul 			 */
67596f2e892SBill Paul 				return (BMSR_MEDIAMASK);
67696f2e892SBill Paul 			case MII_PHYIDR1:
67796f2e892SBill Paul 				if (DC_IS_PNIC(sc))
67896f2e892SBill Paul 					return (DC_VENDORID_LO);
67996f2e892SBill Paul 				return (DC_VENDORID_DEC);
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 			default:
68596f2e892SBill Paul 				return (0);
68696f2e892SBill Paul 			}
68796f2e892SBill Paul 		} else
68896f2e892SBill Paul 			return (0);
68996f2e892SBill Paul 	}
69096f2e892SBill Paul 
69196f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
69296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
69396f2e892SBill Paul 		    (phy << 23) | (reg << 18));
69496f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
69596f2e892SBill Paul 			DELAY(1);
69696f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
69796f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
69896f2e892SBill Paul 				rval &= 0xFFFF;
69996f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
70096f2e892SBill Paul 			}
70196f2e892SBill Paul 		}
70296f2e892SBill Paul 		return (0);
70396f2e892SBill Paul 	}
70496f2e892SBill Paul 
70552ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
70652ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
70752ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
70852ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
70952ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_READ);
71052ca7ee2SPyun YongHyeon 		for (i = 0; i < DC_TIMEOUT; i++) {
71152ca7ee2SPyun YongHyeon 			DELAY(1);
71252ca7ee2SPyun YongHyeon 			rval = CSR_READ_4(sc, DC_ROM);
71352ca7ee2SPyun YongHyeon 			if ((rval & DC_ULI_PHY_OP_DONE) != 0) {
71452ca7ee2SPyun YongHyeon 				return (rval & DC_ULI_PHY_DATA_MASK);
71552ca7ee2SPyun YongHyeon 			}
71652ca7ee2SPyun YongHyeon 		}
71752ca7ee2SPyun YongHyeon 		if (i == DC_TIMEOUT)
71852ca7ee2SPyun YongHyeon 			device_printf(dev, "phy read timed out\n");
71952ca7ee2SPyun YongHyeon 		return (0);
72052ca7ee2SPyun YongHyeon 	}
72152ca7ee2SPyun YongHyeon 
72296f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
72396f2e892SBill Paul 		switch (reg) {
72496f2e892SBill Paul 		case MII_BMCR:
72596f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
72696f2e892SBill Paul 			break;
72796f2e892SBill Paul 		case MII_BMSR:
72896f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
72996f2e892SBill Paul 			break;
73096f2e892SBill Paul 		case MII_PHYIDR1:
73196f2e892SBill Paul 			phy_reg = DC_AL_VENID;
73296f2e892SBill Paul 			break;
73396f2e892SBill Paul 		case MII_PHYIDR2:
73496f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
73596f2e892SBill Paul 			break;
73696f2e892SBill Paul 		case MII_ANAR:
73796f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
73896f2e892SBill Paul 			break;
73996f2e892SBill Paul 		case MII_ANLPAR:
74096f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
74196f2e892SBill Paul 			break;
74296f2e892SBill Paul 		case MII_ANER:
74396f2e892SBill Paul 			phy_reg = DC_AL_ANER;
74496f2e892SBill Paul 			break;
74596f2e892SBill Paul 		default:
74622f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
74722f6205dSJohn Baldwin 			    reg);
74896f2e892SBill Paul 			return (0);
74996f2e892SBill Paul 		}
75096f2e892SBill Paul 
75196f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
75296f2e892SBill Paul 		if (rval == 0xFFFF)
75396f2e892SBill Paul 			return (0);
75496f2e892SBill Paul 		return (rval);
75596f2e892SBill Paul 	}
75696f2e892SBill Paul 
757419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
758f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
759f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
760419146d9SBill Paul 	}
7618c1093fcSMarius Strobl 	rval = mii_bitbang_readreg(dev, &dc_mii_bitbang_ops, phy, reg);
762419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
763f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
76496f2e892SBill Paul 
7658c1093fcSMarius Strobl 	return (rval);
76696f2e892SBill Paul }
76796f2e892SBill Paul 
768e3d2833aSAlfred Perlstein static int
7690934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
77096f2e892SBill Paul {
77196f2e892SBill Paul 	struct dc_softc *sc;
772c85c4667SBill Paul 	int i, phy_reg = 0;
77396f2e892SBill Paul 
77496f2e892SBill Paul 	sc = device_get_softc(dev);
77596f2e892SBill Paul 
77696f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
77796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
77896f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
77996f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
78096f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
78196f2e892SBill Paul 				break;
78296f2e892SBill Paul 		}
78396f2e892SBill Paul 		return (0);
78496f2e892SBill Paul 	}
78596f2e892SBill Paul 
78652ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
78752ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
78852ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
78952ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
79052ca7ee2SPyun YongHyeon 		    ((data << DC_ULI_PHY_DATA_SHIFT) & DC_ULI_PHY_DATA_MASK) |
79152ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_WRITE);
79252ca7ee2SPyun YongHyeon 		DELAY(1);
79352ca7ee2SPyun YongHyeon 		return (0);
79452ca7ee2SPyun YongHyeon 	}
79552ca7ee2SPyun YongHyeon 
79696f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
79796f2e892SBill Paul 		switch (reg) {
79896f2e892SBill Paul 		case MII_BMCR:
79996f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
80096f2e892SBill Paul 			break;
80196f2e892SBill Paul 		case MII_BMSR:
80296f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
80396f2e892SBill Paul 			break;
80496f2e892SBill Paul 		case MII_PHYIDR1:
80596f2e892SBill Paul 			phy_reg = DC_AL_VENID;
80696f2e892SBill Paul 			break;
80796f2e892SBill Paul 		case MII_PHYIDR2:
80896f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
80996f2e892SBill Paul 			break;
81096f2e892SBill Paul 		case MII_ANAR:
81196f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
81296f2e892SBill Paul 			break;
81396f2e892SBill Paul 		case MII_ANLPAR:
81496f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
81596f2e892SBill Paul 			break;
81696f2e892SBill Paul 		case MII_ANER:
81796f2e892SBill Paul 			phy_reg = DC_AL_ANER;
81896f2e892SBill Paul 			break;
81996f2e892SBill Paul 		default:
82022f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
82122f6205dSJohn Baldwin 			    reg);
82296f2e892SBill Paul 			return (0);
82396f2e892SBill Paul 			break;
82496f2e892SBill Paul 		}
82596f2e892SBill Paul 
82696f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
82796f2e892SBill Paul 		return (0);
82896f2e892SBill Paul 	}
82996f2e892SBill Paul 
830419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
831f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
832f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
833419146d9SBill Paul 	}
8348c1093fcSMarius Strobl 	mii_bitbang_writereg(dev, &dc_mii_bitbang_ops, phy, reg, data);
835419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
836f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
83796f2e892SBill Paul 
83896f2e892SBill Paul 	return (0);
83996f2e892SBill Paul }
84096f2e892SBill Paul 
841e3d2833aSAlfred Perlstein static void
8420934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
84396f2e892SBill Paul {
84496f2e892SBill Paul 	struct dc_softc *sc;
845d314ebf5SPyun YongHyeon 	struct ifnet *ifp;
84696f2e892SBill Paul 	struct mii_data *mii;
847f43d9309SBill Paul 	struct ifmedia *ifm;
84896f2e892SBill Paul 
84996f2e892SBill Paul 	sc = device_get_softc(dev);
8505c1cfac4SBill Paul 
85196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
852d314ebf5SPyun YongHyeon 	ifp = sc->dc_ifp;
853d314ebf5SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
854d314ebf5SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
855d314ebf5SPyun YongHyeon 		return;
856d314ebf5SPyun YongHyeon 
857f43d9309SBill Paul 	ifm = &mii->mii_media;
85889b2411bSPyun YongHyeon 	if (DC_IS_DAVICOM(sc) && IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
859f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
860d314ebf5SPyun YongHyeon 		return;
86189b2411bSPyun YongHyeon 	} else if (!DC_IS_ADMTEK(sc))
86289b2411bSPyun YongHyeon 		dc_setcfg(sc, mii->mii_media_active);
863d314ebf5SPyun YongHyeon 
864d314ebf5SPyun YongHyeon 	sc->dc_link = 0;
865d314ebf5SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
866d314ebf5SPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
867d314ebf5SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
868d314ebf5SPyun YongHyeon 		case IFM_10_T:
869d314ebf5SPyun YongHyeon 		case IFM_100_TX:
870d314ebf5SPyun YongHyeon 			sc->dc_link = 1;
871d314ebf5SPyun YongHyeon 			break;
872d314ebf5SPyun YongHyeon 		}
873d314ebf5SPyun YongHyeon 	}
874f43d9309SBill Paul }
875f43d9309SBill Paul 
876f43d9309SBill Paul /*
877f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
878f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
879f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
880f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
881f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
882f43d9309SBill Paul  * with it itself. *sigh*
883f43d9309SBill Paul  */
884e3d2833aSAlfred Perlstein static void
8850934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
886f43d9309SBill Paul {
887f43d9309SBill Paul 	struct dc_softc *sc;
888f43d9309SBill Paul 	struct mii_data *mii;
889f43d9309SBill Paul 	struct ifmedia *ifm;
890f43d9309SBill Paul 	int rev;
891f43d9309SBill Paul 
8921e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
893f43d9309SBill Paul 
894f43d9309SBill Paul 	sc = device_get_softc(dev);
895f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
896f43d9309SBill Paul 	ifm = &mii->mii_media;
897f43d9309SBill Paul 
898f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
89945521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
90096f2e892SBill Paul }
90196f2e892SBill Paul 
90279d11e09SBill Paul #define	DC_BITS_512	9
90379d11e09SBill Paul #define	DC_BITS_128	7
90479d11e09SBill Paul #define	DC_BITS_64	6
90596f2e892SBill Paul 
9063373489bSWarner Losh static uint32_t
9073373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
90896f2e892SBill Paul {
9093373489bSWarner Losh 	uint32_t crc;
91096f2e892SBill Paul 
91196f2e892SBill Paul 	/* Compute CRC for the address value. */
9120e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
91396f2e892SBill Paul 
91479d11e09SBill Paul 	/*
91579d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
91679d11e09SBill Paul 	 * chips is only 128 bits wide.
91779d11e09SBill Paul 	 */
91879d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
91979d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
92096f2e892SBill Paul 
92179d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
92279d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
92379d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
92479d11e09SBill Paul 
925feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
926feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
927feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
928feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
9290934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
930feb78939SJonathan Chen 		else
9310934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
9320934f18aSMaxime Henrion 			    (12 << 4));
933feb78939SJonathan Chen 	}
934feb78939SJonathan Chen 
93579d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
93696f2e892SBill Paul }
93796f2e892SBill Paul 
93896f2e892SBill Paul /*
93996f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
94096f2e892SBill Paul  */
9413373489bSWarner Losh static uint32_t
9423373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
94396f2e892SBill Paul {
9440e939c0cSChristian Weisgerber 	uint32_t crc;
94596f2e892SBill Paul 
94696f2e892SBill Paul 	/* Compute CRC for the address value. */
9470e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
94896f2e892SBill Paul 
9490934f18aSMaxime Henrion 	/* Return the filter bit position. */
95096f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
95196f2e892SBill Paul }
95296f2e892SBill Paul 
95396f2e892SBill Paul /*
95496f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
95596f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
95696f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
95796f2e892SBill Paul  *
95896f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
95996f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
96096f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
96196f2e892SBill Paul  * we need that too.
96296f2e892SBill Paul  */
9632c876e15SPoul-Henning Kamp static void
9640934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
96596f2e892SBill Paul {
9668df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
96796f2e892SBill Paul 	struct dc_desc *sframe;
968ee320f98SPyun YongHyeon 	uint32_t h, *sp;
96996f2e892SBill Paul 	struct ifmultiaddr *ifma;
97096f2e892SBill Paul 	struct ifnet *ifp;
97196f2e892SBill Paul 	int i;
97296f2e892SBill Paul 
973fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
97496f2e892SBill Paul 
97596f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
97696f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
97796f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
9785f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
97956e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
9800934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
98196f2e892SBill Paul 
9825f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
983af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
984af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
98596f2e892SBill Paul 
98656e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
98796f2e892SBill Paul 
98896f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
98996f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
99096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99196f2e892SBill Paul 	else
99296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99396f2e892SBill Paul 
99496f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
99596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
99696f2e892SBill Paul 	else
99796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
99896f2e892SBill Paul 
999eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
10006817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
100196f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100296f2e892SBill Paul 			continue;
1003aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
100496f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1005af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
100696f2e892SBill Paul 	}
1007eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
100896f2e892SBill Paul 
100996f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1010aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1011af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
101296f2e892SBill Paul 	}
101396f2e892SBill Paul 
10148df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
10158df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10168df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
10178df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
10188df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
101996f2e892SBill Paul 
1020af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
10218c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
10228c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
10235f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
102496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
102596f2e892SBill Paul 
102696f2e892SBill Paul 	/*
102796f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
102896f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
102996f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
103096f2e892SBill Paul 	 * medicine.
103196f2e892SBill Paul 	 */
103296f2e892SBill Paul 	DELAY(10000);
103396f2e892SBill Paul 
1034b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
103596f2e892SBill Paul }
103696f2e892SBill Paul 
10372c876e15SPoul-Henning Kamp static void
10380934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
103996f2e892SBill Paul {
10402e3d4b79SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
104196f2e892SBill Paul 	struct ifnet *ifp;
10420934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
104396f2e892SBill Paul 	int h = 0;
1044ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
104596f2e892SBill Paul 
1046fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
104796f2e892SBill Paul 
10480934f18aSMaxime Henrion 	/* Init our MAC address. */
10498df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10502e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
10512e3d4b79SPyun YongHyeon 	    eaddr[1] << 8 | eaddr[0]);
10522e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
105396f2e892SBill Paul 
105496f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
105596f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
105696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
105796f2e892SBill Paul 	else
105896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
105996f2e892SBill Paul 
106096f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
106196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106296f2e892SBill Paul 	else
106396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106496f2e892SBill Paul 
10650934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
106696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
106796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
106896f2e892SBill Paul 
106996f2e892SBill Paul 	/*
107096f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
107196f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
107296f2e892SBill Paul 	 */
107396f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
107496f2e892SBill Paul 		return;
107596f2e892SBill Paul 
10760934f18aSMaxime Henrion 	/* Now program new ones. */
1077eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
10786817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
107996f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
108096f2e892SBill Paul 			continue;
1081acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1082aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1083aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1084acc1bcccSMartin Blapp 		else
1085aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1086aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
108796f2e892SBill Paul 		if (h < 32)
108896f2e892SBill Paul 			hashes[0] |= (1 << h);
108996f2e892SBill Paul 		else
109096f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
109196f2e892SBill Paul 	}
1092eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
109396f2e892SBill Paul 
109496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
109596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
109696f2e892SBill Paul }
109796f2e892SBill Paul 
10982c876e15SPoul-Henning Kamp static void
10990934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
110096f2e892SBill Paul {
11018df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
110296f2e892SBill Paul 	struct ifnet *ifp;
11030934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
110496f2e892SBill Paul 	int h = 0;
1105ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
110696f2e892SBill Paul 
1107fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
110896f2e892SBill Paul 
11098df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
11108df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
111196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
11128df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
111396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
11148df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
111596f2e892SBill Paul 
111696f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
111796f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
111896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
111996f2e892SBill Paul 	else
112096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112196f2e892SBill Paul 
112296f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
112396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112496f2e892SBill Paul 	else
112596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112696f2e892SBill Paul 
112796f2e892SBill Paul 	/*
112896f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
112996f2e892SBill Paul 	 * of broadcast frames.
113096f2e892SBill Paul 	 */
113196f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
113296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113396f2e892SBill Paul 	else
113496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113596f2e892SBill Paul 
113696f2e892SBill Paul 	/* first, zot all the existing hash bits */
113796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
113896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
113996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
114096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114196f2e892SBill Paul 
114296f2e892SBill Paul 	/*
114396f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
114496f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
114596f2e892SBill Paul 	 */
114696f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
114796f2e892SBill Paul 		return;
114896f2e892SBill Paul 
114996f2e892SBill Paul 	/* now program new ones */
1150eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
11516817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
115296f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
115396f2e892SBill Paul 			continue;
1154aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
115596f2e892SBill Paul 		if (h < 32)
115696f2e892SBill Paul 			hashes[0] |= (1 << h);
115796f2e892SBill Paul 		else
115896f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
115996f2e892SBill Paul 	}
1160eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
116196f2e892SBill Paul 
116296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
116396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
116496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
116596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
116696f2e892SBill Paul }
116796f2e892SBill Paul 
11682c876e15SPoul-Henning Kamp static void
116952ca7ee2SPyun YongHyeon dc_setfilt_uli(struct dc_softc *sc)
117052ca7ee2SPyun YongHyeon {
117152ca7ee2SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
117252ca7ee2SPyun YongHyeon 	struct ifnet *ifp;
117352ca7ee2SPyun YongHyeon 	struct ifmultiaddr *ifma;
117452ca7ee2SPyun YongHyeon 	struct dc_desc *sframe;
117552ca7ee2SPyun YongHyeon 	uint32_t filter, *sp;
117652ca7ee2SPyun YongHyeon 	uint8_t *ma;
117752ca7ee2SPyun YongHyeon 	int i, mcnt;
117852ca7ee2SPyun YongHyeon 
117952ca7ee2SPyun YongHyeon 	ifp = sc->dc_ifp;
118052ca7ee2SPyun YongHyeon 
118152ca7ee2SPyun YongHyeon 	i = sc->dc_cdata.dc_tx_prod;
118252ca7ee2SPyun YongHyeon 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
118352ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_cnt++;
118452ca7ee2SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
118552ca7ee2SPyun YongHyeon 	sp = sc->dc_cdata.dc_sbuf;
118652ca7ee2SPyun YongHyeon 	bzero(sp, DC_SFRAME_LEN);
118752ca7ee2SPyun YongHyeon 
118852ca7ee2SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
118952ca7ee2SPyun YongHyeon 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
119052ca7ee2SPyun YongHyeon 	    DC_TXCTL_TLINK | DC_FILTER_PERFECT | DC_TXCTL_FINT);
119152ca7ee2SPyun YongHyeon 
119252ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
119352ca7ee2SPyun YongHyeon 
119452ca7ee2SPyun YongHyeon 	/* Set station address. */
119552ca7ee2SPyun YongHyeon 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
119652ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[1] << 8 | eaddr[0]);
119752ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[3] << 8 | eaddr[2]);
119852ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[5] << 8 | eaddr[4]);
119952ca7ee2SPyun YongHyeon 
120052ca7ee2SPyun YongHyeon 	/* Set broadcast address. */
120152ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120252ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120352ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120452ca7ee2SPyun YongHyeon 
120552ca7ee2SPyun YongHyeon 	/* Extract current filter configuration. */
120652ca7ee2SPyun YongHyeon 	filter = CSR_READ_4(sc, DC_NETCFG);
120752ca7ee2SPyun YongHyeon 	filter &= ~(DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI);
120852ca7ee2SPyun YongHyeon 
120952ca7ee2SPyun YongHyeon 	/* Now build perfect filters. */
121052ca7ee2SPyun YongHyeon 	mcnt = 0;
121152ca7ee2SPyun YongHyeon 	if_maddr_rlock(ifp);
121252ca7ee2SPyun YongHyeon 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
121352ca7ee2SPyun YongHyeon 		if (ifma->ifma_addr->sa_family != AF_LINK)
121452ca7ee2SPyun YongHyeon 			continue;
121552ca7ee2SPyun YongHyeon 		if (mcnt >= DC_ULI_FILTER_NPERF) {
121652ca7ee2SPyun YongHyeon 			filter |= DC_NETCFG_RX_ALLMULTI;
121752ca7ee2SPyun YongHyeon 			break;
121852ca7ee2SPyun YongHyeon 		}
121952ca7ee2SPyun YongHyeon 		ma = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
122052ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[1] << 8 | ma[0]);
122152ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[3] << 8 | ma[2]);
122252ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[5] << 8 | ma[4]);
122352ca7ee2SPyun YongHyeon 		mcnt++;
122452ca7ee2SPyun YongHyeon 	}
122552ca7ee2SPyun YongHyeon 	if_maddr_runlock(ifp);
122652ca7ee2SPyun YongHyeon 
122752ca7ee2SPyun YongHyeon 	for (; mcnt < DC_ULI_FILTER_NPERF; mcnt++) {
122852ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
122952ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123052ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123152ca7ee2SPyun YongHyeon 	}
123252ca7ee2SPyun YongHyeon 
123352ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
123452ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
123552ca7ee2SPyun YongHyeon 		    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
123652ca7ee2SPyun YongHyeon 	if (ifp->if_flags & IFF_PROMISC)
123752ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI;
123852ca7ee2SPyun YongHyeon 	if (ifp->if_flags & IFF_ALLMULTI)
123952ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_ALLMULTI;
124052ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_NETCFG,
124152ca7ee2SPyun YongHyeon 	    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
124252ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
124352ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, filter);
124452ca7ee2SPyun YongHyeon 
124552ca7ee2SPyun YongHyeon 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
124652ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
124752ca7ee2SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
124852ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
124952ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
125052ca7ee2SPyun YongHyeon 
125152ca7ee2SPyun YongHyeon 	/*
125252ca7ee2SPyun YongHyeon 	 * Wait some time...
125352ca7ee2SPyun YongHyeon 	 */
125452ca7ee2SPyun YongHyeon 	DELAY(1000);
125552ca7ee2SPyun YongHyeon 
125652ca7ee2SPyun YongHyeon 	sc->dc_wdog_timer = 5;
125752ca7ee2SPyun YongHyeon }
125852ca7ee2SPyun YongHyeon 
125952ca7ee2SPyun YongHyeon static void
12600934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1261feb78939SJonathan Chen {
12628df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
12630934f18aSMaxime Henrion 	struct ifnet *ifp;
12640934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1265feb78939SJonathan Chen 	struct dc_desc *sframe;
1266ee320f98SPyun YongHyeon 	uint32_t h, *sp;
1267feb78939SJonathan Chen 	int i;
1268feb78939SJonathan Chen 
1269fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1270feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1271feb78939SJonathan Chen 
1272feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1273feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1274feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
12755f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
127656e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
12770934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1278feb78939SJonathan Chen 
12795f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1280af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1281af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1282feb78939SJonathan Chen 
128356e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1284feb78939SJonathan Chen 
1285feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1286feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1287feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1288feb78939SJonathan Chen 	else
1289feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1290feb78939SJonathan Chen 
1291feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1292feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1293feb78939SJonathan Chen 	else
1294feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1295feb78939SJonathan Chen 
1296eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
12976817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1298feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1299feb78939SJonathan Chen 			continue;
1300aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13011d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1302af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1303feb78939SJonathan Chen 	}
1304eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
1305feb78939SJonathan Chen 
1306feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1307aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1308af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1309feb78939SJonathan Chen 	}
1310feb78939SJonathan Chen 
13118df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
13128df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13138df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13148df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13158df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1316feb78939SJonathan Chen 
1317feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1318feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1319af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
13208c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
13218c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
13225f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1323feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1324feb78939SJonathan Chen 
1325feb78939SJonathan Chen 	/*
13260934f18aSMaxime Henrion 	 * Wait some time...
1327feb78939SJonathan Chen 	 */
1328feb78939SJonathan Chen 	DELAY(1000);
1329feb78939SJonathan Chen 
1330b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
1331feb78939SJonathan Chen }
1332feb78939SJonathan Chen 
1333e3d2833aSAlfred Perlstein static void
13340934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
133596f2e892SBill Paul {
13360934f18aSMaxime Henrion 
133796f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13381af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
133996f2e892SBill Paul 		dc_setfilt_21143(sc);
134096f2e892SBill Paul 
134196f2e892SBill Paul 	if (DC_IS_ASIX(sc))
134296f2e892SBill Paul 		dc_setfilt_asix(sc);
134396f2e892SBill Paul 
134496f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
134596f2e892SBill Paul 		dc_setfilt_admtek(sc);
134696f2e892SBill Paul 
134752ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
134852ca7ee2SPyun YongHyeon 		dc_setfilt_uli(sc);
134952ca7ee2SPyun YongHyeon 
1350feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1351feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
135296f2e892SBill Paul }
135396f2e892SBill Paul 
1354e3d2833aSAlfred Perlstein static void
13551da7683aSPyun YongHyeon dc_netcfg_wait(struct dc_softc *sc)
135696f2e892SBill Paul {
1357ee320f98SPyun YongHyeon 	uint32_t isr;
13581da7683aSPyun YongHyeon 	int i;
135996f2e892SBill Paul 
136096f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
136196f2e892SBill Paul 		isr = CSR_READ_4(sc, DC_ISR);
1362d467c136SBill Paul 		if (isr & DC_ISR_TX_IDLE &&
1363351267c1SMartin Blapp 		    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1364351267c1SMartin Blapp 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
136596f2e892SBill Paul 			break;
1366d467c136SBill Paul 		DELAY(10);
136796f2e892SBill Paul 	}
13687a6fab66SWarner Losh 	if (i == DC_TIMEOUT && bus_child_present(sc->dc_dev)) {
1369432120f2SMarius Strobl 		if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13706b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
13711da7683aSPyun YongHyeon 			    "%s: failed to force tx to idle state\n", __func__);
1372432120f2SMarius Strobl 		if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1373432120f2SMarius Strobl 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1374d0d67284SMarius Strobl 		    !DC_HAS_BROKEN_RXSTATE(sc))
1375432120f2SMarius Strobl 			device_printf(sc->dc_dev,
13761da7683aSPyun YongHyeon 			    "%s: failed to force rx to idle state\n", __func__);
1377432120f2SMarius Strobl 	}
137896f2e892SBill Paul }
137996f2e892SBill Paul 
13801da7683aSPyun YongHyeon /*
13811da7683aSPyun YongHyeon  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13821da7683aSPyun YongHyeon  * the netconfig register, we first have to put the transmit and/or
13831da7683aSPyun YongHyeon  * receive logic in the idle state.
13841da7683aSPyun YongHyeon  */
13851da7683aSPyun YongHyeon static void
13861da7683aSPyun YongHyeon dc_setcfg(struct dc_softc *sc, int media)
13871da7683aSPyun YongHyeon {
13881da7683aSPyun YongHyeon 	int restart = 0, watchdogreg;
13891da7683aSPyun YongHyeon 
13901da7683aSPyun YongHyeon 	if (IFM_SUBTYPE(media) == IFM_NONE)
13911da7683aSPyun YongHyeon 		return;
13921da7683aSPyun YongHyeon 
13931da7683aSPyun YongHyeon 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
13941da7683aSPyun YongHyeon 		restart = 1;
13951da7683aSPyun YongHyeon 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
13961da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
13971da7683aSPyun YongHyeon 	}
13981da7683aSPyun YongHyeon 
139996f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1400042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1401042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
140296f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1403bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14040934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14058273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14068273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14078273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14084c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1409bf645417SBill Paul 			} else {
1410bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1411bf645417SBill Paul 			}
141296f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141396f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
141496f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
141596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141696f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
141788d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
141896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
141996f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
142096f2e892SBill Paul 		} else {
142196f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
142296f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
142396f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
142496f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
142596f2e892SBill Paul 			}
1426318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1427318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1428318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
142996f2e892SBill Paul 		}
143096f2e892SBill Paul 	}
143196f2e892SBill Paul 
143296f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1433042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1434042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
143596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14360934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14374c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14388273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14398273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14408273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14418273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14424c2efe27SBill Paul 			} else {
14434c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14444c2efe27SBill Paul 			}
144596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
144696f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
144796f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
144896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
144988d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
145096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
145196f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
145296f2e892SBill Paul 		} else {
145396f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
145496f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
145596f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
145696f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
145796f2e892SBill Paul 			}
145896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1459318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14615c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14625c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14635c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14645c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14655c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14665c1cfac4SBill Paul 				else
14675c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14685c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14695c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14705c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14715c1cfac4SBill Paul 				DELAY(20000);
14725c1cfac4SBill Paul 			}
147396f2e892SBill Paul 		}
147496f2e892SBill Paul 	}
147596f2e892SBill Paul 
1476f43d9309SBill Paul 	/*
1477f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1478f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1479f43d9309SBill Paul 	 * on the external MII port.
1480f43d9309SBill Paul 	 */
1481f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
148245521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1483f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1484f43d9309SBill Paul 			sc->dc_link = 1;
1485f43d9309SBill Paul 		} else {
1486f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1487f43d9309SBill Paul 		}
1488f43d9309SBill Paul 	}
1489f43d9309SBill Paul 
149096f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
149196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149296f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149396f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149496f2e892SBill Paul 	} else {
149596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149696f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149796f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149896f2e892SBill Paul 	}
149996f2e892SBill Paul 
150096f2e892SBill Paul 	if (restart)
150196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
150296f2e892SBill Paul }
150396f2e892SBill Paul 
1504e3d2833aSAlfred Perlstein static void
15050934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
150696f2e892SBill Paul {
15070934f18aSMaxime Henrion 	int i;
150896f2e892SBill Paul 
150996f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
151096f2e892SBill Paul 
151196f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
151296f2e892SBill Paul 		DELAY(10);
151396f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
151496f2e892SBill Paul 			break;
151596f2e892SBill Paul 	}
151696f2e892SBill Paul 
15171af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
151852ca7ee2SPyun YongHyeon 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc) || DC_IS_ULI(sc)) {
151996f2e892SBill Paul 		DELAY(10000);
152096f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152196f2e892SBill Paul 		i = 0;
152296f2e892SBill Paul 	}
152396f2e892SBill Paul 
152496f2e892SBill Paul 	if (i == DC_TIMEOUT)
15256b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev, "reset never completed!\n");
152696f2e892SBill Paul 
152796f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
152896f2e892SBill Paul 	DELAY(1000);
152996f2e892SBill Paul 
153096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
153196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
153296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
153396f2e892SBill Paul 
153491cc2adbSBill Paul 	/*
153591cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
153691cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
153791cc2adbSBill Paul 	 * into a state where it will never come out of reset
153891cc2adbSBill Paul 	 * until we reset the whole chip again.
153991cc2adbSBill Paul 	 */
15405c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
154191cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1542d314ebf5SPyun YongHyeon 		CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFFFFFF);
15435c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15445c1cfac4SBill Paul 	}
154596f2e892SBill Paul }
154696f2e892SBill Paul 
1547ebc284ccSMarius Strobl static const struct dc_type *
15480934f18aSMaxime Henrion dc_devtype(device_t dev)
154996f2e892SBill Paul {
1550ebc284ccSMarius Strobl 	const struct dc_type *t;
1551ee320f98SPyun YongHyeon 	uint32_t devid;
1552ee320f98SPyun YongHyeon 	uint8_t rev;
155396f2e892SBill Paul 
155496f2e892SBill Paul 	t = dc_devs;
15551e2e70b1SJohn Baldwin 	devid = pci_get_devid(dev);
15561e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
155796f2e892SBill Paul 
155896f2e892SBill Paul 	while (t->dc_name != NULL) {
15591e2e70b1SJohn Baldwin 		if (devid == t->dc_devid && rev >= t->dc_minrev)
156096f2e892SBill Paul 			return (t);
156196f2e892SBill Paul 		t++;
156296f2e892SBill Paul 	}
156396f2e892SBill Paul 
156496f2e892SBill Paul 	return (NULL);
156596f2e892SBill Paul }
156696f2e892SBill Paul 
156796f2e892SBill Paul /*
156896f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
156996f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
157096f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
157196f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
157296f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
157396f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
157496f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
157596f2e892SBill Paul  */
1576e3d2833aSAlfred Perlstein static int
15770934f18aSMaxime Henrion dc_probe(device_t dev)
157896f2e892SBill Paul {
1579ebc284ccSMarius Strobl 	const struct dc_type *t;
158096f2e892SBill Paul 
158196f2e892SBill Paul 	t = dc_devtype(dev);
158296f2e892SBill Paul 
158396f2e892SBill Paul 	if (t != NULL) {
158496f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1585d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
158696f2e892SBill Paul 	}
158796f2e892SBill Paul 
158896f2e892SBill Paul 	return (ENXIO);
158996f2e892SBill Paul }
159096f2e892SBill Paul 
1591e3d2833aSAlfred Perlstein static void
15920934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
15935c1cfac4SBill Paul {
15945c1cfac4SBill Paul 	struct dc_mediainfo *m;
1595ee320f98SPyun YongHyeon 	uint8_t *p;
15965c1cfac4SBill Paul 	int i;
1597ee320f98SPyun YongHyeon 	uint32_t reg;
15985c1cfac4SBill Paul 
15995c1cfac4SBill Paul 	m = sc->dc_mi;
16005c1cfac4SBill Paul 
16015c1cfac4SBill Paul 	while (m != NULL) {
16025c1cfac4SBill Paul 		if (m->dc_media == media)
16035c1cfac4SBill Paul 			break;
16045c1cfac4SBill Paul 		m = m->dc_next;
16055c1cfac4SBill Paul 	}
16065c1cfac4SBill Paul 
16075c1cfac4SBill Paul 	if (m == NULL)
16085c1cfac4SBill Paul 		return;
16095c1cfac4SBill Paul 
16105c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16115c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16125c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16135c1cfac4SBill Paul 	}
16145c1cfac4SBill Paul 
16155c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16165c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16175c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16185c1cfac4SBill Paul 	}
16195c1cfac4SBill Paul }
16205c1cfac4SBill Paul 
1621abe4e865SPyun YongHyeon static int
16220934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16235c1cfac4SBill Paul {
16245c1cfac4SBill Paul 	struct dc_mediainfo *m;
16255c1cfac4SBill Paul 
16260934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1627abe4e865SPyun YongHyeon 	if (m == NULL) {
1628abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1629abe4e865SPyun YongHyeon 		return (ENOMEM);
1630abe4e865SPyun YongHyeon 	}
163187f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
163287f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16335c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
163487f4fa15SMartin Blapp 		break;
163587f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16365c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
163787f4fa15SMartin Blapp 		break;
163887f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16395c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
164087f4fa15SMartin Blapp 		break;
164187f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16425c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
164387f4fa15SMartin Blapp 		break;
164487f4fa15SMartin Blapp 	default:
164587f4fa15SMartin Blapp 		break;
164687f4fa15SMartin Blapp 	}
16475c1cfac4SBill Paul 
164887f4fa15SMartin Blapp 	/*
164987f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
165087f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
165187f4fa15SMartin Blapp 	 * supply Media Specific Data.
165287f4fa15SMartin Blapp 	 */
165387f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16545c1cfac4SBill Paul 		m->dc_gp_len = 2;
165587f4fa15SMartin Blapp 		m->dc_gp_ptr =
1656ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
165787f4fa15SMartin Blapp 	} else {
165887f4fa15SMartin Blapp 		m->dc_gp_len = 2;
165987f4fa15SMartin Blapp 		m->dc_gp_ptr =
1660ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
166187f4fa15SMartin Blapp 	}
16625c1cfac4SBill Paul 
16635c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16645c1cfac4SBill Paul 	sc->dc_mi = m;
16655c1cfac4SBill Paul 
16665c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
1667abe4e865SPyun YongHyeon 	return (0);
16685c1cfac4SBill Paul }
16695c1cfac4SBill Paul 
1670abe4e865SPyun YongHyeon static int
16710934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16725c1cfac4SBill Paul {
16735c1cfac4SBill Paul 	struct dc_mediainfo *m;
16745c1cfac4SBill Paul 
16750934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1676abe4e865SPyun YongHyeon 	if (m == NULL) {
1677abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1678abe4e865SPyun YongHyeon 		return (ENOMEM);
1679abe4e865SPyun YongHyeon 	}
16805c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16815c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16825c1cfac4SBill Paul 
16835c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16845c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
16855c1cfac4SBill Paul 
16865c1cfac4SBill Paul 	m->dc_gp_len = 2;
1687ee320f98SPyun YongHyeon 	m->dc_gp_ptr = (uint8_t *)&l->dc_sym_gpio_ctl;
16885c1cfac4SBill Paul 
16895c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16905c1cfac4SBill Paul 	sc->dc_mi = m;
16915c1cfac4SBill Paul 
16925c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
1693abe4e865SPyun YongHyeon 	return (0);
16945c1cfac4SBill Paul }
16955c1cfac4SBill Paul 
1696abe4e865SPyun YongHyeon static int
16970934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
16985c1cfac4SBill Paul {
16995c1cfac4SBill Paul 	struct dc_mediainfo *m;
1700ee320f98SPyun YongHyeon 	uint8_t *p;
17015c1cfac4SBill Paul 
17020934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1703abe4e865SPyun YongHyeon 	if (m == NULL) {
1704abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1705abe4e865SPyun YongHyeon 		return (ENOMEM);
1706abe4e865SPyun YongHyeon 	}
17075c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17085c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17095c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17105c1cfac4SBill Paul 
1711ee320f98SPyun YongHyeon 	p = (uint8_t *)l;
17125c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17135c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17145c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17155c1cfac4SBill Paul 	m->dc_reset_len = *p;
17165c1cfac4SBill Paul 	p++;
17175c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17185c1cfac4SBill Paul 
17195c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17205c1cfac4SBill Paul 	sc->dc_mi = m;
1721abe4e865SPyun YongHyeon 	return (0);
17225c1cfac4SBill Paul }
17235c1cfac4SBill Paul 
1724abe4e865SPyun YongHyeon static int
17250934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17263097aa70SWarner Losh {
17273097aa70SWarner Losh 	int size;
17283097aa70SWarner Losh 
1729abe4e865SPyun YongHyeon 	size = DC_ROM_SIZE(bits);
173052ca7ee2SPyun YongHyeon 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
1731abe4e865SPyun YongHyeon 	if (sc->dc_srom == NULL) {
1732abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate SROM buffer\n");
1733abe4e865SPyun YongHyeon 		return (ENOMEM);
1734abe4e865SPyun YongHyeon 	}
17353097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1736abe4e865SPyun YongHyeon 	return (0);
17373097aa70SWarner Losh }
17383097aa70SWarner Losh 
1739abe4e865SPyun YongHyeon static int
17400934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17415c1cfac4SBill Paul {
17425c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17435c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
1744abe4e865SPyun YongHyeon 	int error, have_mii, i, loff;
17455c1cfac4SBill Paul 	char *ptr;
17465c1cfac4SBill Paul 
1747f956e0b3SMartin Blapp 	have_mii = 0;
17485c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17495c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17505c1cfac4SBill Paul 
17515c1cfac4SBill Paul 	ptr = (char *)lhdr;
17525c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1753f956e0b3SMartin Blapp 	/*
1754f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1755f956e0b3SMartin Blapp 	 */
1756f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1757f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1758f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1759f956e0b3SMartin Blapp 		    have_mii++;
1760f956e0b3SMartin Blapp 
1761f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1762f956e0b3SMartin Blapp 		ptr++;
1763f956e0b3SMartin Blapp 	}
1764f956e0b3SMartin Blapp 
1765f956e0b3SMartin Blapp 	/*
1766f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1767f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1768f956e0b3SMartin Blapp 	 */
1769f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1770f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1771abe4e865SPyun YongHyeon 	error = 0;
17725c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17735c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17745c1cfac4SBill Paul 		switch (hdr->dc_type) {
17755c1cfac4SBill Paul 		case DC_EBLOCK_MII:
1776abe4e865SPyun YongHyeon 			error = dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17775c1cfac4SBill Paul 			break;
17785c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1779f956e0b3SMartin Blapp 			if (! have_mii)
1780abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sia(sc,
1781f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
17825c1cfac4SBill Paul 			break;
17835c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1784f956e0b3SMartin Blapp 			if (! have_mii)
1785abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sym(sc,
1786f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
17875c1cfac4SBill Paul 			break;
17885c1cfac4SBill Paul 		default:
17895c1cfac4SBill Paul 			/* Don't care. Yet. */
17905c1cfac4SBill Paul 			break;
17915c1cfac4SBill Paul 		}
17925c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
17935c1cfac4SBill Paul 		ptr++;
17945c1cfac4SBill Paul 	}
1795abe4e865SPyun YongHyeon 	return (error);
17965c1cfac4SBill Paul }
17975c1cfac4SBill Paul 
179856e5e7aeSMaxime Henrion static void
179956e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
180056e5e7aeSMaxime Henrion {
18015f14ee23SPyun YongHyeon 	bus_addr_t *paddr;
180256e5e7aeSMaxime Henrion 
1803ebc284ccSMarius Strobl 	KASSERT(nseg == 1,
1804ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
180556e5e7aeSMaxime Henrion 	paddr = arg;
180656e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
180756e5e7aeSMaxime Henrion }
180856e5e7aeSMaxime Henrion 
18095f14ee23SPyun YongHyeon static int
18105f14ee23SPyun YongHyeon dc_dma_alloc(struct dc_softc *sc)
18115f14ee23SPyun YongHyeon {
18125f14ee23SPyun YongHyeon 	int error, i;
18135f14ee23SPyun YongHyeon 
18145f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dc_dev), 1, 0,
18155f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
18165f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
18175f14ee23SPyun YongHyeon 	    NULL, NULL, &sc->dc_ptag);
18185f14ee23SPyun YongHyeon 	if (error) {
18195f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18205f14ee23SPyun YongHyeon 		    "failed to allocate parent DMA tag\n");
18215f14ee23SPyun YongHyeon 		goto fail;
18225f14ee23SPyun YongHyeon 	}
18235f14ee23SPyun YongHyeon 
18245f14ee23SPyun YongHyeon 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
18255f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18265f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_RX_LIST_SZ, 1,
18275f14ee23SPyun YongHyeon 	    DC_RX_LIST_SZ, 0, NULL, NULL, &sc->dc_rx_ltag);
18285f14ee23SPyun YongHyeon 	if (error) {
18295f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX list DMA tag\n");
18305f14ee23SPyun YongHyeon 		goto fail;
18315f14ee23SPyun YongHyeon 	}
18325f14ee23SPyun YongHyeon 
18335f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18345f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_TX_LIST_SZ, 1,
18355f14ee23SPyun YongHyeon 	    DC_TX_LIST_SZ, 0, NULL, NULL, &sc->dc_tx_ltag);
18365f14ee23SPyun YongHyeon 	if (error) {
18375f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX list DMA tag\n");
18385f14ee23SPyun YongHyeon 		goto fail;
18395f14ee23SPyun YongHyeon 	}
18405f14ee23SPyun YongHyeon 
18415f14ee23SPyun YongHyeon 	/* RX descriptor list. */
18425f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_rx_ltag,
18435f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_rx_list, BUS_DMA_NOWAIT |
18445f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_rx_lmap);
18455f14ee23SPyun YongHyeon 	if (error) {
18465f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18475f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for RX list\n");
18485f14ee23SPyun YongHyeon 		goto fail;
18495f14ee23SPyun YongHyeon 	}
18505f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_rx_ltag, sc->dc_rx_lmap,
18515f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_rx_list, DC_RX_LIST_SZ, dc_dma_map_addr,
18525f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_rx_list_paddr, BUS_DMA_NOWAIT);
18535f14ee23SPyun YongHyeon 	if (error) {
18545f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18555f14ee23SPyun YongHyeon 		    "failed to load DMA'able memory for RX list\n");
18565f14ee23SPyun YongHyeon 		goto fail;
18575f14ee23SPyun YongHyeon 	}
18585f14ee23SPyun YongHyeon 	/* TX descriptor list. */
18595f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_tx_ltag,
18605f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_tx_list, BUS_DMA_NOWAIT |
18615f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_tx_lmap);
18625f14ee23SPyun YongHyeon 	if (error) {
18635f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18645f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for TX list\n");
18655f14ee23SPyun YongHyeon 		goto fail;
18665f14ee23SPyun YongHyeon 	}
18675f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_tx_ltag, sc->dc_tx_lmap,
18685f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_tx_list, DC_TX_LIST_SZ, dc_dma_map_addr,
18695f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_tx_list_paddr, BUS_DMA_NOWAIT);
18705f14ee23SPyun YongHyeon 	if (error) {
18715f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18725f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for TX list\n");
18735f14ee23SPyun YongHyeon 		goto fail;
18745f14ee23SPyun YongHyeon 	}
18755f14ee23SPyun YongHyeon 
18765f14ee23SPyun YongHyeon 	/*
18775f14ee23SPyun YongHyeon 	 * Allocate a busdma tag and DMA safe memory for the multicast
18785f14ee23SPyun YongHyeon 	 * setup frame.
18795f14ee23SPyun YongHyeon 	 */
18805f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18815f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
18825f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
18835f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_stag);
18845f14ee23SPyun YongHyeon 	if (error) {
18855f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18865f14ee23SPyun YongHyeon 		    "failed to create DMA tag for setup frame\n");
18875f14ee23SPyun YongHyeon 		goto fail;
18885f14ee23SPyun YongHyeon 	}
18895f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
18905f14ee23SPyun YongHyeon 	    BUS_DMA_NOWAIT, &sc->dc_smap);
18915f14ee23SPyun YongHyeon 	if (error) {
18925f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18935f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for setup frame\n");
18945f14ee23SPyun YongHyeon 		goto fail;
18955f14ee23SPyun YongHyeon 	}
18965f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
18975f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
18985f14ee23SPyun YongHyeon 	if (error) {
18995f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19005f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for setup frame\n");
19015f14ee23SPyun YongHyeon 		goto fail;
19025f14ee23SPyun YongHyeon 	}
19035f14ee23SPyun YongHyeon 
19045f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for RX mbufs. */
19055f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_RXBUF_ALIGN, 0,
19065f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19075f14ee23SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->dc_rx_mtag);
19085f14ee23SPyun YongHyeon 	if (error) {
19095f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX mbuf tag\n");
19105f14ee23SPyun YongHyeon 		goto fail;
19115f14ee23SPyun YongHyeon 	}
19125f14ee23SPyun YongHyeon 
19135f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for TX mbufs. */
19145f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, 1, 0,
19155f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19165f14ee23SPyun YongHyeon 	    MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
19175f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_tx_mtag);
19185f14ee23SPyun YongHyeon 	if (error) {
19195f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX mbuf tag\n");
19205f14ee23SPyun YongHyeon 		goto fail;
19215f14ee23SPyun YongHyeon 	}
19225f14ee23SPyun YongHyeon 
19235f14ee23SPyun YongHyeon 	/* Create the TX/RX busdma maps. */
19245f14ee23SPyun YongHyeon 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
19255f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_tx_mtag, 0,
19265f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_tx_map[i]);
19275f14ee23SPyun YongHyeon 		if (error) {
19285f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19295f14ee23SPyun YongHyeon 			    "failed to create TX mbuf dmamap\n");
19305f14ee23SPyun YongHyeon 			goto fail;
19315f14ee23SPyun YongHyeon 		}
19325f14ee23SPyun YongHyeon 	}
19335f14ee23SPyun YongHyeon 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
19345f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_rx_mtag, 0,
19355f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_rx_map[i]);
19365f14ee23SPyun YongHyeon 		if (error) {
19375f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19385f14ee23SPyun YongHyeon 			    "failed to create RX mbuf dmamap\n");
19395f14ee23SPyun YongHyeon 			goto fail;
19405f14ee23SPyun YongHyeon 		}
19415f14ee23SPyun YongHyeon 	}
19425f14ee23SPyun YongHyeon 	error = bus_dmamap_create(sc->dc_rx_mtag, 0, &sc->dc_sparemap);
19435f14ee23SPyun YongHyeon 	if (error) {
19445f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19455f14ee23SPyun YongHyeon 		    "failed to create spare RX mbuf dmamap\n");
19465f14ee23SPyun YongHyeon 		goto fail;
19475f14ee23SPyun YongHyeon 	}
19485f14ee23SPyun YongHyeon 
19495f14ee23SPyun YongHyeon fail:
19505f14ee23SPyun YongHyeon 	return (error);
19515f14ee23SPyun YongHyeon }
19525f14ee23SPyun YongHyeon 
19535f14ee23SPyun YongHyeon static void
19545f14ee23SPyun YongHyeon dc_dma_free(struct dc_softc *sc)
19555f14ee23SPyun YongHyeon {
19565f14ee23SPyun YongHyeon 	int i;
19575f14ee23SPyun YongHyeon 
19585f14ee23SPyun YongHyeon 	/* RX buffers. */
19595f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19605f14ee23SPyun YongHyeon 		for (i = 0; i < DC_RX_LIST_CNT; i++) {
19615f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
19625f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_rx_mtag,
19635f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_rx_map[i]);
19645f14ee23SPyun YongHyeon 		}
19655f14ee23SPyun YongHyeon 		if (sc->dc_sparemap != NULL)
19665f14ee23SPyun YongHyeon 			bus_dmamap_destroy(sc->dc_rx_mtag, sc->dc_sparemap);
19675f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_mtag);
19685f14ee23SPyun YongHyeon 	}
19695f14ee23SPyun YongHyeon 
19705f14ee23SPyun YongHyeon 	/* TX buffers. */
19715f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19725f14ee23SPyun YongHyeon 		for (i = 0; i < DC_TX_LIST_CNT; i++) {
19735f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
19745f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_tx_mtag,
19755f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_tx_map[i]);
19765f14ee23SPyun YongHyeon 		}
19775f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_mtag);
19785f14ee23SPyun YongHyeon 	}
19795f14ee23SPyun YongHyeon 
19805f14ee23SPyun YongHyeon 	/* RX descriptor list. */
19815f14ee23SPyun YongHyeon 	if (sc->dc_rx_ltag) {
1982068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_rx_list_paddr != 0)
19835f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_ltag, sc->dc_rx_lmap);
1984068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_rx_list != NULL)
19855f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_rx_ltag, sc->dc_ldata.dc_rx_list,
19865f14ee23SPyun YongHyeon 			    sc->dc_rx_lmap);
19875f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_ltag);
19885f14ee23SPyun YongHyeon 	}
19895f14ee23SPyun YongHyeon 
19905f14ee23SPyun YongHyeon 	/* TX descriptor list. */
19915f14ee23SPyun YongHyeon 	if (sc->dc_tx_ltag) {
1992068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_tx_list_paddr != 0)
19935f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_ltag, sc->dc_tx_lmap);
1994068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_tx_list != NULL)
19955f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_tx_ltag, sc->dc_ldata.dc_tx_list,
19965f14ee23SPyun YongHyeon 			    sc->dc_tx_lmap);
19975f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_ltag);
19985f14ee23SPyun YongHyeon 	}
19995f14ee23SPyun YongHyeon 
20005f14ee23SPyun YongHyeon 	/* multicast setup frame. */
20015f14ee23SPyun YongHyeon 	if (sc->dc_stag) {
2002068d8643SJohn Baldwin 		if (sc->dc_saddr != 0)
20035f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_stag, sc->dc_smap);
2004068d8643SJohn Baldwin 		if (sc->dc_cdata.dc_sbuf != NULL)
20055f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf,
20065f14ee23SPyun YongHyeon 			    sc->dc_smap);
20075f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_stag);
20085f14ee23SPyun YongHyeon 	}
20095f14ee23SPyun YongHyeon }
20105f14ee23SPyun YongHyeon 
201196f2e892SBill Paul /*
201296f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
201396f2e892SBill Paul  * setup and ethernet/BPF attach.
201496f2e892SBill Paul  */
2015e3d2833aSAlfred Perlstein static int
20160934f18aSMaxime Henrion dc_attach(device_t dev)
201796f2e892SBill Paul {
20188df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
2019ee320f98SPyun YongHyeon 	uint32_t command;
202096f2e892SBill Paul 	struct dc_softc *sc;
202196f2e892SBill Paul 	struct ifnet *ifp;
2022b289c607SPyun YongHyeon 	struct dc_mediainfo *m;
2023ee320f98SPyun YongHyeon 	uint32_t reg, revision;
202452ca7ee2SPyun YongHyeon 	uint16_t *srom;
202552ca7ee2SPyun YongHyeon 	int error, mac_offset, n, phy, rid, tmp;
2026ee320f98SPyun YongHyeon 	uint8_t *mac;
202796f2e892SBill Paul 
202896f2e892SBill Paul 	sc = device_get_softc(dev);
20296b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
203096f2e892SBill Paul 
20316008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2032c8b27acaSJohn Baldwin 	    MTX_DEF);
2033c3e7434fSWarner Losh 
203496f2e892SBill Paul 	/*
203596f2e892SBill Paul 	 * Map control/status registers.
203696f2e892SBill Paul 	 */
203707f65363SBill Paul 	pci_enable_busmaster(dev);
203896f2e892SBill Paul 
203996f2e892SBill Paul 	rid = DC_RID;
20405f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
204196f2e892SBill Paul 
204296f2e892SBill Paul 	if (sc->dc_res == NULL) {
204322f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
204496f2e892SBill Paul 		error = ENXIO;
2045608654d4SNate Lawson 		goto fail;
204696f2e892SBill Paul 	}
204796f2e892SBill Paul 
204896f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
204996f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
205096f2e892SBill Paul 
20510934f18aSMaxime Henrion 	/* Allocate interrupt. */
205254f1f1d1SNate Lawson 	rid = 0;
20535f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
205454f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
205554f1f1d1SNate Lawson 
205654f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
205722f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
205854f1f1d1SNate Lawson 		error = ENXIO;
205954f1f1d1SNate Lawson 		goto fail;
206054f1f1d1SNate Lawson 	}
206154f1f1d1SNate Lawson 
206296f2e892SBill Paul 	/* Need this info to decide on a chip type. */
206396f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
20641e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
206596f2e892SBill Paul 
2066abe4e865SPyun YongHyeon 	error = 0;
20676d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
20681e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
20691e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
20701e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
20711e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
2072eecb3844SMartin Blapp 		dc_eeprom_width(sc);
2073eecb3844SMartin Blapp 
20741e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
20751e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
207696f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
207796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2078042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
20795c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
2080abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2081abe4e865SPyun YongHyeon 		if (error != 0)
2082abe4e865SPyun YongHyeon 			goto fail;
208396f2e892SBill Paul 		break;
20841e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
20851e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
20861e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
208796f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
2088318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
2089318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
20907dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
20914a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
20921e2e70b1SJohn Baldwin 
20930a46b1dcSBill Paul 		/* Increase the latency timer value. */
20941e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
209596f2e892SBill Paul 		break;
20961e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
209796f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
209896f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
209996f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
210096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2101abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2102abe4e865SPyun YongHyeon 		if (error != 0)
2103abe4e865SPyun YongHyeon 			goto fail;
210496f2e892SBill Paul 		break;
2105593a1aeaSMartin Blapp 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983):
21061e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
21071e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
21081e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
21091e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
21101e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
21111e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
21121e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
21131e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
21141e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
21151e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
21161e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
21171e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
211817762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
211917762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
2120593a1aeaSMartin Blapp 		sc->dc_type = DC_TYPE_AN983;
2121acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
212296f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
212396f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
212496f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2125129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
212696f2e892SBill Paul 		break;
21271e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
21281e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
212996f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
213096f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
213196f2e892SBill Paul 		}
2132318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
213396f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
2134318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
2135318b02fdSBill Paul 		}
2136318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
213796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
213896f2e892SBill Paul 		break;
21391e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
21401e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
214179d11e09SBill Paul 		/*
214279d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
214379d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
214479d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
214579d11e09SBill Paul 		 * get the right number of bits out of the
214679d11e09SBill Paul 		 * CRC routine.
214779d11e09SBill Paul 		 */
214879d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
214979d11e09SBill Paul 		    revision < DC_REVISION_98725)
215079d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
215196f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
215296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2153318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
215496f2e892SBill Paul 		break;
21551e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
2156ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
2157ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2158ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2159ead7cde9SBill Paul 		break;
21601e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
216196f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
216279d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
2163318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
216496f2e892SBill Paul 		break;
21651e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
216696f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
216791cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
216896f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
216996f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
2170abe4e865SPyun YongHyeon 		if (sc->dc_pnic_rx_buf == NULL) {
2171abe4e865SPyun YongHyeon 			device_printf(sc->dc_dev,
2172abe4e865SPyun YongHyeon 			    "Could not allocate PNIC RX buffer\n");
2173abe4e865SPyun YongHyeon 			error = ENOMEM;
2174abe4e865SPyun YongHyeon 			goto fail;
2175abe4e865SPyun YongHyeon 		}
217696f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
217796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
217896f2e892SBill Paul 		break;
21791e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
218096f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
218196f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
218296f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
218396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
218496f2e892SBill Paul 		break;
21851e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
2186feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
21872dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
21882dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2189feb78939SJonathan Chen 		/*
2190feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2191feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
21922dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2193feb78939SJonathan Chen 		 */
21943097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2195feb78939SJonathan Chen 		break;
21961e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
21971af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
21981af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
21991af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
22001af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2201abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2202abe4e865SPyun YongHyeon 		if (error != 0)
2203abe4e865SPyun YongHyeon 			goto fail;
22041af8bec7SBill Paul 		break;
220552ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261):
220652ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263):
220752ca7ee2SPyun YongHyeon 		if (sc->dc_info->dc_devid ==
220852ca7ee2SPyun YongHyeon 		    DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261))
220952ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5261;
221052ca7ee2SPyun YongHyeon 		else
221152ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5263;
221252ca7ee2SPyun YongHyeon 		/* TX buffers should be aligned on 4 byte boundary. */
221352ca7ee2SPyun YongHyeon 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
221452ca7ee2SPyun YongHyeon 		    DC_TX_ALIGN;
221552ca7ee2SPyun YongHyeon 		sc->dc_pmode = DC_PMODE_MII;
221652ca7ee2SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
221752ca7ee2SPyun YongHyeon 		if (error != 0)
221852ca7ee2SPyun YongHyeon 			goto fail;
221952ca7ee2SPyun YongHyeon 		break;
222096f2e892SBill Paul 	default:
22211e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
22221e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
222396f2e892SBill Paul 		break;
222496f2e892SBill Paul 	}
222596f2e892SBill Paul 
222696f2e892SBill Paul 	/* Save the cache line size. */
222788d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
222888d739dcSBill Paul 		sc->dc_cachesize = 0;
222988d739dcSBill Paul 	else
22301e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
223196f2e892SBill Paul 
223296f2e892SBill Paul 	/* Reset the adapter. */
223396f2e892SBill Paul 	dc_reset(sc);
223496f2e892SBill Paul 
223596f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2236feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
223796f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
223896f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
223996f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
224096f2e892SBill Paul 	}
224196f2e892SBill Paul 
224296f2e892SBill Paul 	/*
224396f2e892SBill Paul 	 * Try to learn something about the supported media.
224496f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
224596f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
224696f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
224796f2e892SBill Paul 	 * Intel 21143.
224896f2e892SBill Paul 	 */
2249abe4e865SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
2250abe4e865SPyun YongHyeon 		error = dc_parse_21143_srom(sc);
2251abe4e865SPyun YongHyeon 		if (error != 0)
2252abe4e865SPyun YongHyeon 			goto fail;
2253abe4e865SPyun YongHyeon 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
225496f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
225596f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
225696f2e892SBill Paul 		else
225796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
225896f2e892SBill Paul 	} else if (!sc->dc_pmode)
225996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
226096f2e892SBill Paul 
226196f2e892SBill Paul 	/*
226296f2e892SBill Paul 	 * Get station address from the EEPROM.
226396f2e892SBill Paul 	 */
226496f2e892SBill Paul 	switch(sc->dc_type) {
226596f2e892SBill Paul 	case DC_TYPE_98713:
226696f2e892SBill Paul 	case DC_TYPE_98713A:
226796f2e892SBill Paul 	case DC_TYPE_987x5:
226896f2e892SBill Paul 	case DC_TYPE_PNICII:
226996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
227096f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
227196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
227296f2e892SBill Paul 		break;
227396f2e892SBill Paul 	case DC_TYPE_PNIC:
227496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
227596f2e892SBill Paul 		break;
227696f2e892SBill Paul 	case DC_TYPE_DM9102:
2277ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2278ec6a7299SMaxime Henrion #ifdef __sparc64__
2279ec6a7299SMaxime Henrion 		/*
2280ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2281802cab03SMarius Strobl 		 * the EEPROM is all zero and we have to get it from the FCode.
2282ec6a7299SMaxime Henrion 		 */
2283802cab03SMarius Strobl 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
22848069c79dSRuslan Ermilov 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2285ec6a7299SMaxime Henrion #endif
2286ec6a7299SMaxime Henrion 		break;
228796f2e892SBill Paul 	case DC_TYPE_21143:
228896f2e892SBill Paul 	case DC_TYPE_ASIX:
228996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
229096f2e892SBill Paul 		break;
229196f2e892SBill Paul 	case DC_TYPE_AL981:
2292593a1aeaSMartin Blapp 	case DC_TYPE_AN983:
22932e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR0);
22942e3d4b79SPyun YongHyeon 		mac = (uint8_t *)&eaddr[0];
22952e3d4b79SPyun YongHyeon 		mac[0] = (reg >> 0) & 0xff;
22962e3d4b79SPyun YongHyeon 		mac[1] = (reg >> 8) & 0xff;
22972e3d4b79SPyun YongHyeon 		mac[2] = (reg >> 16) & 0xff;
22982e3d4b79SPyun YongHyeon 		mac[3] = (reg >> 24) & 0xff;
22992e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR1);
23002e3d4b79SPyun YongHyeon 		mac[4] = (reg >> 0) & 0xff;
23012e3d4b79SPyun YongHyeon 		mac[5] = (reg >> 8) & 0xff;
230296f2e892SBill Paul 		break;
23031af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
23040934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
23050934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
23061af8bec7SBill Paul 		break;
2307feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
23080934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2309e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2310e7b01d07SWarner Losh 		if (!mac) {
2311e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2312608654d4SNate Lawson 			error = ENXIO;
2313e7b01d07SWarner Losh 			goto fail;
2314e7b01d07SWarner Losh 		}
2315e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2316feb78939SJonathan Chen 		break;
231752ca7ee2SPyun YongHyeon 	case DC_TYPE_ULI_M5261:
231852ca7ee2SPyun YongHyeon 	case DC_TYPE_ULI_M5263:
231952ca7ee2SPyun YongHyeon 		srom = (uint16_t *)sc->dc_srom;
232052ca7ee2SPyun YongHyeon 		if (srom == NULL || *srom == 0xFFFF || *srom == 0) {
232152ca7ee2SPyun YongHyeon 			/*
232252ca7ee2SPyun YongHyeon 			 * No valid SROM present, read station address
232352ca7ee2SPyun YongHyeon 			 * from ID Table.
232452ca7ee2SPyun YongHyeon 			 */
232552ca7ee2SPyun YongHyeon 			device_printf(dev,
232652ca7ee2SPyun YongHyeon 			    "Reading station address from ID Table.\n");
232752ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_BUSCTL, 0x10000);
232852ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x01C0);
232952ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0000);
233052ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0010);
233152ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0000);
233252ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x0000);
233352ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x01B0);
233452ca7ee2SPyun YongHyeon 			mac = (uint8_t *)eaddr;
233552ca7ee2SPyun YongHyeon 			for (n = 0; n < ETHER_ADDR_LEN; n++)
233652ca7ee2SPyun YongHyeon 				mac[n] = (uint8_t)CSR_READ_4(sc, DC_10BTCTRL);
233752ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x0000);
233852ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_BUSCTL, 0x0000);
233952ca7ee2SPyun YongHyeon 			DELAY(10);
234052ca7ee2SPyun YongHyeon 		} else
234152ca7ee2SPyun YongHyeon 			dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3,
234252ca7ee2SPyun YongHyeon 			    0);
234352ca7ee2SPyun YongHyeon 		break;
234496f2e892SBill Paul 	default:
234596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
234696f2e892SBill Paul 		break;
234796f2e892SBill Paul 	}
234896f2e892SBill Paul 
234939d76ed6SPyun YongHyeon 	bcopy(eaddr, sc->dc_eaddr, sizeof(eaddr));
235039d76ed6SPyun YongHyeon 	/*
235139d76ed6SPyun YongHyeon 	 * If we still have invalid station address, see whether we can
235239d76ed6SPyun YongHyeon 	 * find station address for chip 0.  Some multi-port controllers
235339d76ed6SPyun YongHyeon 	 * just store station address for chip 0 if they have a shared
235439d76ed6SPyun YongHyeon 	 * SROM.
235539d76ed6SPyun YongHyeon 	 */
235639d76ed6SPyun YongHyeon 	if ((sc->dc_eaddr[0] == 0 && (sc->dc_eaddr[1] & ~0xffff) == 0) ||
235739d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[0] == 0xffffffff &&
235839d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[1] & 0xffff) == 0xffff)) {
2359b289c607SPyun YongHyeon 		error = dc_check_multiport(sc);
2360b289c607SPyun YongHyeon 		if (error == 0) {
236139d76ed6SPyun YongHyeon 			bcopy(sc->dc_eaddr, eaddr, sizeof(eaddr));
2362b289c607SPyun YongHyeon 			/* Extract media information. */
2363b289c607SPyun YongHyeon 			if (DC_IS_INTEL(sc) && sc->dc_srom != NULL) {
2364b289c607SPyun YongHyeon 				while (sc->dc_mi != NULL) {
2365b289c607SPyun YongHyeon 					m = sc->dc_mi->dc_next;
2366b289c607SPyun YongHyeon 					free(sc->dc_mi, M_DEVBUF);
2367b289c607SPyun YongHyeon 					sc->dc_mi = m;
2368b289c607SPyun YongHyeon 				}
2369b289c607SPyun YongHyeon 				error = dc_parse_21143_srom(sc);
2370b289c607SPyun YongHyeon 				if (error != 0)
2371b289c607SPyun YongHyeon 					goto fail;
2372b289c607SPyun YongHyeon 			}
2373b289c607SPyun YongHyeon 		} else if (error == ENOMEM)
2374b289c607SPyun YongHyeon 			goto fail;
2375b289c607SPyun YongHyeon 		else
2376b289c607SPyun YongHyeon 			error = 0;
237739d76ed6SPyun YongHyeon 	}
237839d76ed6SPyun YongHyeon 
23795f14ee23SPyun YongHyeon 	if ((error = dc_dma_alloc(sc)) != 0)
238056e5e7aeSMaxime Henrion 		goto fail;
238196f2e892SBill Paul 
2382fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2383fc74a9f9SBrooks Davis 	if (ifp == NULL) {
238422f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2385fc74a9f9SBrooks Davis 		error = ENOSPC;
2386fc74a9f9SBrooks Davis 		goto fail;
2387fc74a9f9SBrooks Davis 	}
238896f2e892SBill Paul 	ifp->if_softc = sc;
23899bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
23903d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
239196f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
239296f2e892SBill Paul 	ifp->if_start = dc_start;
239396f2e892SBill Paul 	ifp->if_init = dc_init;
2394cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2395cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2396cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
239796f2e892SBill Paul 
239896f2e892SBill Paul 	/*
23995c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
24005c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
24015c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
24025c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
24035c1cfac4SBill Paul 	 * driver instead.
240496f2e892SBill Paul 	 */
24058e5d93dbSMarius Strobl 	tmp = 0;
24065c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
24075c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
24085c1cfac4SBill Paul 		tmp = sc->dc_pmode;
24095c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
24105c1cfac4SBill Paul 	}
24115c1cfac4SBill Paul 
24126d431b17SWarner Losh 	/*
24136d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
24148e5d93dbSMarius Strobl 	 * to the MII.  This needs to be done before mii_attach so that
24156d431b17SWarner Losh 	 * we can actually see them.
24166d431b17SWarner Losh 	 */
24176d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
24186d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
24196d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24206d431b17SWarner Losh 		DELAY(10);
24216d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
24226d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24236d431b17SWarner Losh 		DELAY(10);
24246d431b17SWarner Losh 	}
24256d431b17SWarner Losh 
24268e5d93dbSMarius Strobl 	phy = MII_PHY_ANY;
24278e5d93dbSMarius Strobl 	/*
24288e5d93dbSMarius Strobl 	 * Note: both the AL981 and AN983 have internal PHYs, however the
24298e5d93dbSMarius Strobl 	 * AL981 provides direct access to the PHY registers while the AN983
24308e5d93dbSMarius Strobl 	 * uses a serial MII interface. The AN983's MII interface is also
24318e5d93dbSMarius Strobl 	 * buggy in that you can read from any MII address (0 to 31), but
24328e5d93dbSMarius Strobl 	 * only address 1 behaves normally. To deal with both cases, we
24338e5d93dbSMarius Strobl 	 * pretend that the PHY is at MII address 1.
24348e5d93dbSMarius Strobl 	 */
24358e5d93dbSMarius Strobl 	if (DC_IS_ADMTEK(sc))
24368e5d93dbSMarius Strobl 		phy = DC_ADMTEK_PHYADDR;
24378e5d93dbSMarius Strobl 
24388e5d93dbSMarius Strobl 	/*
24398e5d93dbSMarius Strobl 	 * Note: the ukphy probes of the RS7112 report a PHY at MII address
24408e5d93dbSMarius Strobl 	 * 0 (possibly HomePNA?) and 1 (ethernet) so we only respond to the
24418e5d93dbSMarius Strobl 	 * correct one.
24428e5d93dbSMarius Strobl 	 */
24438e5d93dbSMarius Strobl 	if (DC_IS_CONEXANT(sc))
24448e5d93dbSMarius Strobl 		phy = DC_CONEXANT_PHYADDR;
24458e5d93dbSMarius Strobl 
24468e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24478e5d93dbSMarius Strobl 	    dc_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
244896f2e892SBill Paul 
244996f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
24505c1cfac4SBill Paul 		sc->dc_pmode = tmp;
24515c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
245296f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2453042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
245478999dd1SBill Paul 		/*
245578999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
245678999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
245778999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
245878999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
245978999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
246078999dd1SBill Paul 		 */
24611e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
24621e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
246378999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
2464166e31d9SMarius Strobl 		error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
2465166e31d9SMarius Strobl 		    dc_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
2466166e31d9SMarius Strobl 		    MII_OFFSET_ANY, 0);
246796f2e892SBill Paul 	}
246896f2e892SBill Paul 
246996f2e892SBill Paul 	if (error) {
24708e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
247196f2e892SBill Paul 		goto fail;
247296f2e892SBill Paul 	}
247396f2e892SBill Paul 
2474028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2475028a8491SMartin Blapp 		/*
2476028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2477028a8491SMartin Blapp 		 */
2478028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2479028a8491SMartin Blapp 	}
2480028a8491SMartin Blapp 
248196f2e892SBill Paul 	/*
2482db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2483db40c1aeSDoug Ambrisko 	 */
24841bffa951SGleb Smirnoff 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
24859ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
248640929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2487e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2488e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2489e695984eSRuslan Ermilov #endif
2490db40c1aeSDoug Ambrisko 
2491c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2492b1d16143SMarius Strobl 	callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
249396f2e892SBill Paul 
2494608654d4SNate Lawson 	/*
2495608654d4SNate Lawson 	 * Call MI attach routine.
2496608654d4SNate Lawson 	 */
24978df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2498608654d4SNate Lawson 
249954f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2500c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2501ef544f63SPaolo Pisati 	    NULL, dc_intr, sc, &sc->dc_intrhand);
2502608654d4SNate Lawson 
2503608654d4SNate Lawson 	if (error) {
250422f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2505693f4477SNate Lawson 		ether_ifdetach(ifp);
250654f1f1d1SNate Lawson 		goto fail;
2507608654d4SNate Lawson 	}
2508510a809eSMike Smith 
250996f2e892SBill Paul fail:
251054f1f1d1SNate Lawson 	if (error)
251154f1f1d1SNate Lawson 		dc_detach(dev);
251296f2e892SBill Paul 	return (error);
251396f2e892SBill Paul }
251496f2e892SBill Paul 
2515693f4477SNate Lawson /*
2516693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2517693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2518693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2519693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2520693f4477SNate Lawson  * allocated.
2521693f4477SNate Lawson  */
2522e3d2833aSAlfred Perlstein static int
25230934f18aSMaxime Henrion dc_detach(device_t dev)
252496f2e892SBill Paul {
252596f2e892SBill Paul 	struct dc_softc *sc;
252696f2e892SBill Paul 	struct ifnet *ifp;
25275c1cfac4SBill Paul 	struct dc_mediainfo *m;
252896f2e892SBill Paul 
252996f2e892SBill Paul 	sc = device_get_softc(dev);
253059f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2531d1ce9105SBill Paul 
2532fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
253396f2e892SBill Paul 
253440929967SGleb Smirnoff #ifdef DEVICE_POLLING
2535166e31d9SMarius Strobl 	if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
253640929967SGleb Smirnoff 		ether_poll_deregister(ifp);
253740929967SGleb Smirnoff #endif
253840929967SGleb Smirnoff 
2539693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2540214073e5SWarner Losh 	if (device_is_attached(dev)) {
2541c8b27acaSJohn Baldwin 		DC_LOCK(sc);
254296f2e892SBill Paul 		dc_stop(sc);
2543c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2544c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
2545b1d16143SMarius Strobl 		callout_drain(&sc->dc_wdog_ch);
25469ef8b520SSam Leffler 		ether_ifdetach(ifp);
2547693f4477SNate Lawson 	}
2548693f4477SNate Lawson 	if (sc->dc_miibus)
254996f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
255054f1f1d1SNate Lawson 	bus_generic_detach(dev);
255196f2e892SBill Paul 
255254f1f1d1SNate Lawson 	if (sc->dc_intrhand)
255396f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
255454f1f1d1SNate Lawson 	if (sc->dc_irq)
255596f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
255654f1f1d1SNate Lawson 	if (sc->dc_res)
255796f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
255896f2e892SBill Paul 
2559166e31d9SMarius Strobl 	if (ifp != NULL)
25606a3033a8SWarner Losh 		if_free(ifp);
25616a3033a8SWarner Losh 
25625f14ee23SPyun YongHyeon 	dc_dma_free(sc);
256356e5e7aeSMaxime Henrion 
256496f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
256596f2e892SBill Paul 
25665c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
25675c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
25685c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
25695c1cfac4SBill Paul 		sc->dc_mi = m;
25705c1cfac4SBill Paul 	}
25717efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
25725c1cfac4SBill Paul 
2573d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
257496f2e892SBill Paul 
257596f2e892SBill Paul 	return (0);
257696f2e892SBill Paul }
257796f2e892SBill Paul 
257896f2e892SBill Paul /*
257996f2e892SBill Paul  * Initialize the transmit descriptors.
258096f2e892SBill Paul  */
2581e3d2833aSAlfred Perlstein static int
25820934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
258396f2e892SBill Paul {
258496f2e892SBill Paul 	struct dc_chain_data *cd;
258596f2e892SBill Paul 	struct dc_list_data *ld;
258601faf54bSLuigi Rizzo 	int i, nexti;
258796f2e892SBill Paul 
258896f2e892SBill Paul 	cd = &sc->dc_cdata;
25895f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
259096f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2591b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2592b3811c95SMaxime Henrion 			nexti = 0;
2593b3811c95SMaxime Henrion 		else
2594b3811c95SMaxime Henrion 			nexti = i + 1;
259552c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_status = 0;
259652c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_ctl = 0;
259752c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_data = 0;
2598af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
259996f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
260096f2e892SBill Paul 	}
260196f2e892SBill Paul 
260296f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
260306d23883SPyun YongHyeon 	cd->dc_tx_pkts = 0;
26045f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
260556e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
260696f2e892SBill Paul 	return (0);
260796f2e892SBill Paul }
260896f2e892SBill Paul 
260996f2e892SBill Paul /*
261096f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
261196f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
261296f2e892SBill Paul  * points back to the first.
261396f2e892SBill Paul  */
2614e3d2833aSAlfred Perlstein static int
26150934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
261696f2e892SBill Paul {
261796f2e892SBill Paul 	struct dc_chain_data *cd;
261896f2e892SBill Paul 	struct dc_list_data *ld;
261901faf54bSLuigi Rizzo 	int i, nexti;
262096f2e892SBill Paul 
262196f2e892SBill Paul 	cd = &sc->dc_cdata;
26225f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
262396f2e892SBill Paul 
262496f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
26255f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0)
262696f2e892SBill Paul 			return (ENOBUFS);
2627b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2628b3811c95SMaxime Henrion 			nexti = 0;
2629b3811c95SMaxime Henrion 		else
2630b3811c95SMaxime Henrion 			nexti = i + 1;
2631af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
263296f2e892SBill Paul 	}
263396f2e892SBill Paul 
263496f2e892SBill Paul 	cd->dc_rx_prod = 0;
26355f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
263656e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
263796f2e892SBill Paul 	return (0);
263896f2e892SBill Paul }
263996f2e892SBill Paul 
264096f2e892SBill Paul /*
264196f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
264296f2e892SBill Paul  */
2643e3d2833aSAlfred Perlstein static int
26445f14ee23SPyun YongHyeon dc_newbuf(struct dc_softc *sc, int i)
264596f2e892SBill Paul {
26465f14ee23SPyun YongHyeon 	struct mbuf *m;
26475f14ee23SPyun YongHyeon 	bus_dmamap_t map;
264882a67a70SMarius Strobl 	bus_dma_segment_t segs[1];
264982a67a70SMarius Strobl 	int error, nseg;
265096f2e892SBill Paul 
2651c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
26525f14ee23SPyun YongHyeon 	if (m == NULL)
265396f2e892SBill Paul 		return (ENOBUFS);
26545f14ee23SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
26555f14ee23SPyun YongHyeon 	m_adj(m, sizeof(u_int64_t));
265696f2e892SBill Paul 
265796f2e892SBill Paul 	/*
265896f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
265996f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
266096f2e892SBill Paul 	 * 82c169 chips.
266196f2e892SBill Paul 	 */
266296f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
26635f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
266496f2e892SBill Paul 
26655f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_rx_mtag, sc->dc_sparemap,
26665f14ee23SPyun YongHyeon 	    m, segs, &nseg, 0);
266756e5e7aeSMaxime Henrion 	if (error) {
26685f14ee23SPyun YongHyeon 		m_freem(m);
266956e5e7aeSMaxime Henrion 		return (error);
267056e5e7aeSMaxime Henrion 	}
26715f14ee23SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: wrong number of segments (%d)", __func__,
26725f14ee23SPyun YongHyeon 	    nseg));
26735f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_rx_chain[i] != NULL)
26745f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i]);
267596f2e892SBill Paul 
26765f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_rx_map[i];
26775f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
26785f14ee23SPyun YongHyeon 	sc->dc_sparemap = map;
26795f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_chain[i] = m;
26805f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
268156e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
26825f14ee23SPyun YongHyeon 
26835f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
26845f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_data =
26855f14ee23SPyun YongHyeon 	    htole32(DC_ADDR_LO(segs[0].ds_addr));
26865f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
26875f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
268856e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
268996f2e892SBill Paul 	return (0);
269096f2e892SBill Paul }
269196f2e892SBill Paul 
269296f2e892SBill Paul /*
269396f2e892SBill Paul  * Grrrrr.
269496f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
269596f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
269696f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
269796f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
269896f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
269996f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
270096f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
270196f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
270296f2e892SBill Paul  *
270396f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
270496f2e892SBill Paul  * Here's what we know:
270596f2e892SBill Paul  *
270696f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
270796f2e892SBill Paul  *   descriptors uploaded.
270896f2e892SBill Paul  *
270996f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
271096f2e892SBill Paul  *   total data upload.
271196f2e892SBill Paul  *
271296f2e892SBill Paul  * - We know the size of the desired received frame because it will be
271396f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
271496f2e892SBill Paul  *
271596f2e892SBill Paul  * Here's what we do:
271696f2e892SBill Paul  *
271796f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
271896f2e892SBill Paul  *   This means that we know that the buffer contents should be all
271996f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
272096f2e892SBill Paul  *
272196f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
272296f2e892SBill Paul  *   ethernet CRC at the end.
272396f2e892SBill Paul  *
272496f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
272596f2e892SBill Paul  *
272696f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
272796f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
272896f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
272996f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
273096f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
273196f2e892SBill Paul  *   we won't be fooled.
273296f2e892SBill Paul  *
273396f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
273496f2e892SBill Paul  *   that value from the current pointer location. This brings us
273596f2e892SBill Paul  *   to the start of the actual received packet.
273696f2e892SBill Paul  *
273796f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
273896f2e892SBill Paul  *   frame length.
273996f2e892SBill Paul  *
274096f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
274196f2e892SBill Paul  * the time.
274296f2e892SBill Paul  */
274396f2e892SBill Paul 
274496f2e892SBill Paul #define	DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2745e3d2833aSAlfred Perlstein static void
27460934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
274796f2e892SBill Paul {
274896f2e892SBill Paul 	struct dc_desc *cur_rx;
274996f2e892SBill Paul 	struct dc_desc *c = NULL;
275096f2e892SBill Paul 	struct mbuf *m = NULL;
275196f2e892SBill Paul 	unsigned char *ptr;
275296f2e892SBill Paul 	int i, total_len;
2753ee320f98SPyun YongHyeon 	uint32_t rxstat = 0;
275496f2e892SBill Paul 
275596f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
27565f14ee23SPyun YongHyeon 	cur_rx = &sc->dc_ldata.dc_rx_list[idx];
275796f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
27581edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
275996f2e892SBill Paul 
276096f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
276196f2e892SBill Paul 	while (1) {
27625f14ee23SPyun YongHyeon 		c = &sc->dc_ldata.dc_rx_list[i];
2763af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
276496f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
276596f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
276696f2e892SBill Paul 		ptr += DC_RXLEN;
276796f2e892SBill Paul 		/* If this is the last buffer, break out. */
276896f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
276996f2e892SBill Paul 			break;
27705f14ee23SPyun YongHyeon 		dc_discard_rxbuf(sc, i);
277196f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
277296f2e892SBill Paul 	}
277396f2e892SBill Paul 
277496f2e892SBill Paul 	/* Find the length of the actual receive frame. */
277596f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
277696f2e892SBill Paul 
277796f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
277896f2e892SBill Paul 	while (*ptr == 0x00)
277996f2e892SBill Paul 		ptr--;
278096f2e892SBill Paul 
278196f2e892SBill Paul 	/* Round off. */
278296f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
278396f2e892SBill Paul 		ptr -= 1;
278496f2e892SBill Paul 
278596f2e892SBill Paul 	/* Now find the start of the frame. */
278696f2e892SBill Paul 	ptr -= total_len;
278796f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
278896f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
278996f2e892SBill Paul 
279096f2e892SBill Paul 	/*
279196f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
279296f2e892SBill Paul 	 * the status word to make it look like a successful
279396f2e892SBill Paul 	 * frame reception.
279496f2e892SBill Paul 	 */
279596f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2796af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
279796f2e892SBill Paul }
279896f2e892SBill Paul 
279996f2e892SBill Paul /*
280073bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
280173bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
280273bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
280373bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
280473bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
280573bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
280673bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
280773bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
280873bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
280973bf949cSBill Paul  */
2810e3d2833aSAlfred Perlstein static int
28110934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
281273bf949cSBill Paul {
281373bf949cSBill Paul 	struct dc_desc *cur_rx;
28140934f18aSMaxime Henrion 	int i, pos;
281573bf949cSBill Paul 
281673bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
281773bf949cSBill Paul 
281873bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
28195f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[pos];
2820af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
282173bf949cSBill Paul 			break;
282273bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
282373bf949cSBill Paul 	}
282473bf949cSBill Paul 
282573bf949cSBill Paul 	/* If the ring really is empty, then just return. */
282673bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
282773bf949cSBill Paul 		return (0);
282873bf949cSBill Paul 
282973bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
283073bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
283173bf949cSBill Paul 
283273bf949cSBill Paul 	return (EAGAIN);
283373bf949cSBill Paul }
283473bf949cSBill Paul 
28355f14ee23SPyun YongHyeon static void
28365f14ee23SPyun YongHyeon dc_discard_rxbuf(struct dc_softc *sc, int i)
28375f14ee23SPyun YongHyeon {
28385f14ee23SPyun YongHyeon 	struct mbuf *m;
28395f14ee23SPyun YongHyeon 
28405f14ee23SPyun YongHyeon 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
28415f14ee23SPyun YongHyeon 		m = sc->dc_cdata.dc_rx_chain[i];
28425f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
28435f14ee23SPyun YongHyeon 	}
28445f14ee23SPyun YongHyeon 
28455f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
28465f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
28475f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_PREREAD |
28485f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
28495f14ee23SPyun YongHyeon }
28505f14ee23SPyun YongHyeon 
285173bf949cSBill Paul /*
285296f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
285396f2e892SBill Paul  * the higher level protocols.
285496f2e892SBill Paul  */
28551abcdbd1SAttilio Rao static int
28560934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
285796f2e892SBill Paul {
28585f14ee23SPyun YongHyeon 	struct mbuf *m;
285996f2e892SBill Paul 	struct ifnet *ifp;
286096f2e892SBill Paul 	struct dc_desc *cur_rx;
28611abcdbd1SAttilio Rao 	int i, total_len, rx_npkts;
2862ee320f98SPyun YongHyeon 	uint32_t rxstat;
286396f2e892SBill Paul 
28645120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
28655120abbfSSam Leffler 
2866fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
28671abcdbd1SAttilio Rao 	rx_npkts = 0;
286896f2e892SBill Paul 
28695f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_POSTREAD |
28705f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
28715f14ee23SPyun YongHyeon 	for (i = sc->dc_cdata.dc_rx_prod;
28725f14ee23SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
28735f14ee23SPyun YongHyeon 	    DC_INC(i, DC_RX_LIST_CNT)) {
2874e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
287540929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2876e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2877e4fc250cSLuigi Rizzo 				break;
2878e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2879e4fc250cSLuigi Rizzo 		}
28800934f18aSMaxime Henrion #endif
28815f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[i];
2882af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
28835f14ee23SPyun YongHyeon 		if ((rxstat & DC_RXSTAT_OWN) != 0)
28845f14ee23SPyun YongHyeon 			break;
288596f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
28865f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
288756e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
288896f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
2889848a02fcSPyun YongHyeon 		rx_npkts++;
289096f2e892SBill Paul 
289196f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
289296f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
289396f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
289496f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
28955f14ee23SPyun YongHyeon 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0)
289696f2e892SBill Paul 					continue;
289796f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2898af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
289996f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
290096f2e892SBill Paul 			}
290196f2e892SBill Paul 		}
290296f2e892SBill Paul 
290396f2e892SBill Paul 		/*
290496f2e892SBill Paul 		 * If an error occurs, update stats, clear the
290596f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
290696f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2907db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
29080934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
290996f2e892SBill Paul 		 */
2910db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2911db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2912db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2913db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2914db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
2915c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
291696f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
2917c8dfaf38SGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
29185f14ee23SPyun YongHyeon 				dc_discard_rxbuf(sc, i);
29195f14ee23SPyun YongHyeon 				if (rxstat & DC_RXSTAT_CRCERR)
292096f2e892SBill Paul 					continue;
29215f14ee23SPyun YongHyeon 				else {
29228f382a1fSPyun YongHyeon 					ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2923c8b27acaSJohn Baldwin 					dc_init_locked(sc);
29241abcdbd1SAttilio Rao 					return (rx_npkts);
292596f2e892SBill Paul 				}
292696f2e892SBill Paul 			}
2927db40c1aeSDoug Ambrisko 		}
292896f2e892SBill Paul 
292996f2e892SBill Paul 		/* No errors; receive the packet. */
293096f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2931432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
293201faf54bSLuigi Rizzo 		/*
2933432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
293401faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
293501faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
293601faf54bSLuigi Rizzo 		 * copy done in m_devget().
293701faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
293801faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
293901faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
294001faf54bSLuigi Rizzo 		 */
29415f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0) {
29425f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
2943c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
29445f14ee23SPyun YongHyeon 			continue;
29455f14ee23SPyun YongHyeon 		}
294601faf54bSLuigi Rizzo 		m->m_pkthdr.rcvif = ifp;
294701faf54bSLuigi Rizzo 		m->m_pkthdr.len = m->m_len = total_len;
29485f14ee23SPyun YongHyeon #else
294901faf54bSLuigi Rizzo 		{
29505f14ee23SPyun YongHyeon 			struct mbuf *m0;
29515f14ee23SPyun YongHyeon 
295201faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
295301faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
29545f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
295596f2e892SBill Paul 			if (m0 == NULL) {
2956c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
295796f2e892SBill Paul 				continue;
295896f2e892SBill Paul 			}
295996f2e892SBill Paul 			m = m0;
296001faf54bSLuigi Rizzo 		}
29615f14ee23SPyun YongHyeon #endif
296296f2e892SBill Paul 
2963c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
29645120abbfSSam Leffler 		DC_UNLOCK(sc);
29659ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
29665120abbfSSam Leffler 		DC_LOCK(sc);
296796f2e892SBill Paul 	}
296896f2e892SBill Paul 
296996f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
29701abcdbd1SAttilio Rao 	return (rx_npkts);
297196f2e892SBill Paul }
297296f2e892SBill Paul 
297396f2e892SBill Paul /*
297496f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
297596f2e892SBill Paul  * the list buffers.
297696f2e892SBill Paul  */
2977e3d2833aSAlfred Perlstein static void
29780934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
297996f2e892SBill Paul {
29805f14ee23SPyun YongHyeon 	struct dc_desc *cur_tx;
298196f2e892SBill Paul 	struct ifnet *ifp;
29825f14ee23SPyun YongHyeon 	int idx, setup;
2983ee320f98SPyun YongHyeon 	uint32_t ctl, txstat;
298496f2e892SBill Paul 
298506d23883SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt == 0)
298606d23883SPyun YongHyeon 		return;
298706d23883SPyun YongHyeon 
2988fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
298996f2e892SBill Paul 
299096f2e892SBill Paul 	/*
299196f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
299296f2e892SBill Paul 	 * frames that have been transmitted.
299396f2e892SBill Paul 	 */
2994cb94db27SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_POSTREAD |
29955f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
29965f14ee23SPyun YongHyeon 	setup = 0;
29975f14ee23SPyun YongHyeon 	for (idx = sc->dc_cdata.dc_tx_cons; idx != sc->dc_cdata.dc_tx_prod;
29985f14ee23SPyun YongHyeon 	    DC_INC(idx, DC_TX_LIST_CNT), sc->dc_cdata.dc_tx_cnt--) {
29995f14ee23SPyun YongHyeon 		cur_tx = &sc->dc_ldata.dc_tx_list[idx];
3000af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
3001af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
300296f2e892SBill Paul 
300396f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
300496f2e892SBill Paul 			break;
300596f2e892SBill Paul 
30065f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_chain[idx] == NULL)
30075f14ee23SPyun YongHyeon 			continue;
30085f14ee23SPyun YongHyeon 
3009af4358c7SMaxime Henrion 		if (ctl & DC_TXCTL_SETUP) {
30105f14ee23SPyun YongHyeon 			cur_tx->dc_ctl = htole32(ctl & ~DC_TXCTL_SETUP);
30115f14ee23SPyun YongHyeon 			setup++;
30125f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
30135f14ee23SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
301496f2e892SBill Paul 			/*
301596f2e892SBill Paul 			 * Yes, the PNIC is so brain damaged
301696f2e892SBill Paul 			 * that it will sometimes generate a TX
301796f2e892SBill Paul 			 * underrun error while DMAing the RX
301896f2e892SBill Paul 			 * filter setup frame. If we detect this,
301996f2e892SBill Paul 			 * we have to send the setup frame again,
302096f2e892SBill Paul 			 * or else the filter won't be programmed
302196f2e892SBill Paul 			 * correctly.
302296f2e892SBill Paul 			 */
302396f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
302496f2e892SBill Paul 				if (txstat & DC_TXSTAT_ERRSUM)
302596f2e892SBill Paul 					dc_setfilt(sc);
302696f2e892SBill Paul 			}
302796f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
302896f2e892SBill Paul 			continue;
302996f2e892SBill Paul 		}
303096f2e892SBill Paul 
303129a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
3032feb78939SJonathan Chen 			/*
3033feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
3034feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
303529a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
303629a2220aSBill Paul 			 * Who knows, but Conexant chips have the
303729a2220aSBill Paul 			 * same problem. Maybe they took lessons
303829a2220aSBill Paul 			 * from Xircom.
303929a2220aSBill Paul 			 */
3040feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
3041feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
3042feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
3043feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
3044feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
3045feb78939SJonathan Chen 		} else {
304696f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
304796f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
304896f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
304996f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
305096f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
3051feb78939SJonathan Chen 		}
305296f2e892SBill Paul 
305396f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
3054c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
305596f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
3056c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
305796f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
3058c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
305996f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
30608f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3061c8b27acaSJohn Baldwin 				dc_init_locked(sc);
306296f2e892SBill Paul 				return;
306396f2e892SBill Paul 			}
306452c43a47SPyun YongHyeon 		} else
3065c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3066c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_COLLISIONS, (txstat & DC_TXSTAT_COLLCNT) >> 3);
306796f2e892SBill Paul 
30685f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
306956e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
30705f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
307196f2e892SBill Paul 		m_freem(sc->dc_cdata.dc_tx_chain[idx]);
307296f2e892SBill Paul 		sc->dc_cdata.dc_tx_chain[idx] = NULL;
307396f2e892SBill Paul 	}
307496f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
307582a67a70SMarius Strobl 
30765f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
307713f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
30783e0e6726SMarius Strobl 		if (sc->dc_cdata.dc_tx_cnt == 0)
30793e0e6726SMarius Strobl 			sc->dc_wdog_timer = 0;
308096f2e892SBill Paul 	}
30815f14ee23SPyun YongHyeon 	if (setup > 0)
30825f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
30835f14ee23SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
30845f14ee23SPyun YongHyeon }
308596f2e892SBill Paul 
3086e3d2833aSAlfred Perlstein static void
30870934f18aSMaxime Henrion dc_tick(void *xsc)
308896f2e892SBill Paul {
308996f2e892SBill Paul 	struct dc_softc *sc;
309096f2e892SBill Paul 	struct mii_data *mii;
309196f2e892SBill Paul 	struct ifnet *ifp;
3092ee320f98SPyun YongHyeon 	uint32_t r;
309396f2e892SBill Paul 
309496f2e892SBill Paul 	sc = xsc;
3095c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3096fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
309796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
309896f2e892SBill Paul 
309906d23883SPyun YongHyeon 	/*
310006d23883SPyun YongHyeon 	 * Reclaim transmitted frames for controllers that do
310106d23883SPyun YongHyeon 	 * not generate TX completion interrupt for every frame.
310206d23883SPyun YongHyeon 	 */
310306d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR)
310406d23883SPyun YongHyeon 		dc_txeof(sc);
310506d23883SPyun YongHyeon 
310696f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
3107318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
3108318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
3109318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3110318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
311196f2e892SBill Paul 				sc->dc_link = 0;
3112318b02fdSBill Paul 				mii_mediachg(mii);
3113318b02fdSBill Paul 			}
3114318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3115318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
3116318b02fdSBill Paul 				sc->dc_link = 0;
3117318b02fdSBill Paul 				mii_mediachg(mii);
3118318b02fdSBill Paul 			}
3119d675147eSBill Paul 			if (sc->dc_link == 0)
312096f2e892SBill Paul 				mii_tick(mii);
312196f2e892SBill Paul 		} else {
3122d0d67284SMarius Strobl 			/*
3123d0d67284SMarius Strobl 			 * For NICs which never report DC_RXSTATE_WAIT, we
3124d0d67284SMarius Strobl 			 * have to bite the bullet...
3125d0d67284SMarius Strobl 			 */
3126d0d67284SMarius Strobl 			if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
3127d0d67284SMarius Strobl 			    DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
3128d314ebf5SPyun YongHyeon 			    sc->dc_cdata.dc_tx_cnt == 0)
312996f2e892SBill Paul 				mii_tick(mii);
3130259b8d84SMartin Blapp 		}
313196f2e892SBill Paul 	} else
313296f2e892SBill Paul 		mii_tick(mii);
313396f2e892SBill Paul 
313496f2e892SBill Paul 	/*
313596f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
313696f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
313796f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
313896f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
313996f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
314096f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
314196f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
314296f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
314396f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
314496f2e892SBill Paul 	 * a screeching halt for several seconds.
314596f2e892SBill Paul 	 *
314696f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
314796f2e892SBill Paul 	 * any packets until a link has been established. After the
314896f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
314996f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
315096f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
315196f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
315296f2e892SBill Paul 	 */
3153d314ebf5SPyun YongHyeon 	if (sc->dc_link != 0 && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3154c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
315596f2e892SBill Paul 
3156318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
3157b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3158318b02fdSBill Paul 	else
3159b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
316096f2e892SBill Paul }
316196f2e892SBill Paul 
3162d467c136SBill Paul /*
3163d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
3164d467c136SBill Paul  * or switch to store and forward mode if we have to.
3165d467c136SBill Paul  */
3166e3d2833aSAlfred Perlstein static void
31670934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
3168d467c136SBill Paul {
3169d9efae03SPyun YongHyeon 	uint32_t netcfg, isr;
3170d9efae03SPyun YongHyeon 	int i, reinit;
3171d467c136SBill Paul 
3172d9efae03SPyun YongHyeon 	reinit = 0;
3173d9efae03SPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
3174d9efae03SPyun YongHyeon 	device_printf(sc->dc_dev, "TX underrun -- ");
3175d9efae03SPyun YongHyeon 	if ((sc->dc_flags & DC_TX_STORENFWD) == 0) {
3176d9efae03SPyun YongHyeon 		if (sc->dc_txthresh + DC_TXTHRESH_INC > DC_TXTHRESH_MAX) {
3177d9efae03SPyun YongHyeon 			printf("using store and forward mode\n");
3178d9efae03SPyun YongHyeon 			netcfg |= DC_NETCFG_STORENFWD;
3179d9efae03SPyun YongHyeon 		} else {
3180d9efae03SPyun YongHyeon 			printf("increasing TX threshold\n");
3181d9efae03SPyun YongHyeon 			sc->dc_txthresh += DC_TXTHRESH_INC;
3182d9efae03SPyun YongHyeon 			netcfg &= ~DC_NETCFG_TX_THRESH;
3183d9efae03SPyun YongHyeon 			netcfg |= sc->dc_txthresh;
31848f382a1fSPyun YongHyeon 		}
3185d467c136SBill Paul 
3186d467c136SBill Paul 		if (DC_IS_INTEL(sc)) {
3187d467c136SBill Paul 			/*
3188d467c136SBill Paul 			 * The real 21143 requires that the transmitter be idle
3189d467c136SBill Paul 			 * in order to change the transmit threshold or store
3190d467c136SBill Paul 			 * and forward state.
3191d467c136SBill Paul 			 */
3192d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg & ~DC_NETCFG_TX_ON);
3193d467c136SBill Paul 
3194d467c136SBill Paul 			for (i = 0; i < DC_TIMEOUT; i++) {
3195d467c136SBill Paul 				isr = CSR_READ_4(sc, DC_ISR);
3196d467c136SBill Paul 				if (isr & DC_ISR_TX_IDLE)
3197d467c136SBill Paul 					break;
3198d467c136SBill Paul 				DELAY(10);
3199d467c136SBill Paul 			}
3200d467c136SBill Paul 			if (i == DC_TIMEOUT) {
32016b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
3202432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
3203432120f2SMarius Strobl 				    __func__);
3204d9efae03SPyun YongHyeon 				reinit++;
3205d9efae03SPyun YongHyeon 			}
3206d9efae03SPyun YongHyeon 		}
3207d9efae03SPyun YongHyeon 	} else {
3208d9efae03SPyun YongHyeon 		printf("resetting\n");
3209d9efae03SPyun YongHyeon 		reinit++;
3210d9efae03SPyun YongHyeon 	}
3211d9efae03SPyun YongHyeon 
3212d9efae03SPyun YongHyeon 	if (reinit == 0) {
3213d9efae03SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, netcfg);
3214d9efae03SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3215d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg | DC_NETCFG_TX_ON);
3216d9efae03SPyun YongHyeon 	} else {
32178f382a1fSPyun YongHyeon 		sc->dc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3218c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3219d467c136SBill Paul 	}
3220d467c136SBill Paul }
3221d467c136SBill Paul 
3222e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3223e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3224e4fc250cSLuigi Rizzo 
32251abcdbd1SAttilio Rao static int
3226e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3227e4fc250cSLuigi Rizzo {
3228e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
32291abcdbd1SAttilio Rao 	int rx_npkts = 0;
3230e4fc250cSLuigi Rizzo 
323140929967SGleb Smirnoff 	DC_LOCK(sc);
323240929967SGleb Smirnoff 
323340929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
323440929967SGleb Smirnoff 		DC_UNLOCK(sc);
32351abcdbd1SAttilio Rao 		return (rx_npkts);
3236e4fc250cSLuigi Rizzo 	}
323740929967SGleb Smirnoff 
3238e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
32391abcdbd1SAttilio Rao 	rx_npkts = dc_rxeof(sc);
3240e4fc250cSLuigi Rizzo 	dc_txeof(sc);
324113f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
324213f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3243c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3244e4fc250cSLuigi Rizzo 
3245e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3246ee320f98SPyun YongHyeon 		uint32_t	status;
3247e4fc250cSLuigi Rizzo 
3248e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3249e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3250e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3251e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
32525120abbfSSam Leffler 		if (!status) {
32535120abbfSSam Leffler 			DC_UNLOCK(sc);
32541abcdbd1SAttilio Rao 			return (rx_npkts);
32555120abbfSSam Leffler 		}
3256e4fc250cSLuigi Rizzo 		/* ack what we have */
3257e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3258e4fc250cSLuigi Rizzo 
3259e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3260ee320f98SPyun YongHyeon 			uint32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3261c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, (r & 0xffff) + ((r >> 17) & 0x7ff));
3262e4fc250cSLuigi Rizzo 
3263e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3264e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3265e4fc250cSLuigi Rizzo 		}
3266e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3267e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3268e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3269e4fc250cSLuigi Rizzo 
3270e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3271e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3272e4fc250cSLuigi Rizzo 
3273e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
32746b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
32758f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3276c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3277e4fc250cSLuigi Rizzo 		}
3278e4fc250cSLuigi Rizzo 	}
32795120abbfSSam Leffler 	DC_UNLOCK(sc);
32801abcdbd1SAttilio Rao 	return (rx_npkts);
3281e4fc250cSLuigi Rizzo }
3282e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3283e4fc250cSLuigi Rizzo 
3284e3d2833aSAlfred Perlstein static void
32850934f18aSMaxime Henrion dc_intr(void *arg)
328696f2e892SBill Paul {
328796f2e892SBill Paul 	struct dc_softc *sc;
328896f2e892SBill Paul 	struct ifnet *ifp;
3289ee320f98SPyun YongHyeon 	uint32_t r, status;
3290848a02fcSPyun YongHyeon 	int n;
329196f2e892SBill Paul 
329296f2e892SBill Paul 	sc = arg;
3293d2a1864bSWarner Losh 
32940934f18aSMaxime Henrion 	if (sc->suspended)
3295e8388e14SMitsuru IWASAKI 		return;
3296e8388e14SMitsuru IWASAKI 
3297d1ce9105SBill Paul 	DC_LOCK(sc);
3298a84b4e80SPyun YongHyeon 	status = CSR_READ_4(sc, DC_ISR);
3299a84b4e80SPyun YongHyeon 	if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0) {
3300a84b4e80SPyun YongHyeon 		DC_UNLOCK(sc);
3301a84b4e80SPyun YongHyeon 		return;
3302a84b4e80SPyun YongHyeon 	}
3303fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3304e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
330540929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
330640929967SGleb Smirnoff 		DC_UNLOCK(sc);
330740929967SGleb Smirnoff 		return;
3308e4fc250cSLuigi Rizzo 	}
33090934f18aSMaxime Henrion #endif
331096f2e892SBill Paul 	/* Disable interrupts. */
331196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
331296f2e892SBill Paul 
3313a84b4e80SPyun YongHyeon 	for (n = 16; n > 0; n--) {
3314a84b4e80SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3315a84b4e80SPyun YongHyeon 			break;
3316a84b4e80SPyun YongHyeon 		/* Ack interrupts. */
331796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
331896f2e892SBill Paul 
331973bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
3320848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
332173bf949cSBill Paul 				while (dc_rx_resync(sc))
332273bf949cSBill Paul 					dc_rxeof(sc);
332373bf949cSBill Paul 			}
332473bf949cSBill Paul 		}
332596f2e892SBill Paul 
332696f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
332796f2e892SBill Paul 			dc_txeof(sc);
332896f2e892SBill Paul 
332996f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
333096f2e892SBill Paul 			dc_txeof(sc);
333196f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
333296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
333396f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
333496f2e892SBill Paul 			}
333596f2e892SBill Paul 		}
333696f2e892SBill Paul 
3337d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3338d467c136SBill Paul 			dc_tx_underrun(sc);
333996f2e892SBill Paul 
334096f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
334173bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
334226b40a65SPyun YongHyeon 			r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3343c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, (r & 0xffff) + ((r >> 17) & 0x7ff));
3344848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
334573bf949cSBill Paul 				while (dc_rx_resync(sc))
334673bf949cSBill Paul 					dc_rxeof(sc);
334773bf949cSBill Paul 			}
334873bf949cSBill Paul 		}
334996f2e892SBill Paul 
3350a84b4e80SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3351a84b4e80SPyun YongHyeon 			dc_start_locked(ifp);
3352a84b4e80SPyun YongHyeon 
335396f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
33548f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3355c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3356a84b4e80SPyun YongHyeon 			DC_UNLOCK(sc);
3357a84b4e80SPyun YongHyeon 			return;
335896f2e892SBill Paul 		}
3359a84b4e80SPyun YongHyeon 		status = CSR_READ_4(sc, DC_ISR);
3360a84b4e80SPyun YongHyeon 		if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0)
3361a84b4e80SPyun YongHyeon 			break;
336296f2e892SBill Paul 	}
336396f2e892SBill Paul 
336496f2e892SBill Paul 	/* Re-enable interrupts. */
3365a84b4e80SPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
336696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
336796f2e892SBill Paul 
3368d1ce9105SBill Paul 	DC_UNLOCK(sc);
336996f2e892SBill Paul }
337096f2e892SBill Paul 
337196f2e892SBill Paul /*
337296f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
337396f2e892SBill Paul  * pointers to the fragment pointers.
337496f2e892SBill Paul  */
3375e3d2833aSAlfred Perlstein static int
3376a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
337796f2e892SBill Paul {
3378ebc284ccSMarius Strobl 	bus_dma_segment_t segs[DC_MAXFRAGS];
33795f14ee23SPyun YongHyeon 	bus_dmamap_t map;
3380ebc284ccSMarius Strobl 	struct dc_desc *f;
338196f2e892SBill Paul 	struct mbuf *m;
3382993a741aSMarius Strobl 	int cur, defragged, error, first, frag, i, idx, nseg;
3383cda97c50SMike Silbersack 
3384993a741aSMarius Strobl 	m = NULL;
3385993a741aSMarius Strobl 	defragged = 0;
3386993a741aSMarius Strobl 	if (sc->dc_flags & DC_TX_COALESCE &&
3387993a741aSMarius Strobl 	    ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3388c6499eccSGleb Smirnoff 		m = m_defrag(*m_head, M_NOWAIT);
3389993a741aSMarius Strobl 		defragged = 1;
3390993a741aSMarius Strobl 	} else {
3391cda97c50SMike Silbersack 		/*
3392993a741aSMarius Strobl 		 * Count the number of frags in this chain to see if we
3393993a741aSMarius Strobl 		 * need to m_collapse.  Since the descriptor list is shared
3394993a741aSMarius Strobl 		 * by all packets, we'll m_collapse long chains so that they
3395cda97c50SMike Silbersack 		 * do not use up the entire list, even if they would fit.
3396cda97c50SMike Silbersack 		 */
3397993a741aSMarius Strobl 		i = 0;
3398a10c0e45SMike Silbersack 		for (m = *m_head; m != NULL; m = m->m_next)
3399993a741aSMarius Strobl 			i++;
3400993a741aSMarius Strobl 		if (i > DC_TX_LIST_CNT / 4 ||
3401993a741aSMarius Strobl 		    DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3402993a741aSMarius Strobl 		    DC_TX_LIST_RSVD) {
3403c6499eccSGleb Smirnoff 			m = m_collapse(*m_head, M_NOWAIT, DC_MAXFRAGS);
3404993a741aSMarius Strobl 			defragged = 1;
3405993a741aSMarius Strobl 		}
3406993a741aSMarius Strobl 	}
3407993a741aSMarius Strobl 	if (defragged != 0) {
340882a67a70SMarius Strobl 		if (m == NULL) {
340982a67a70SMarius Strobl 			m_freem(*m_head);
341082a67a70SMarius Strobl 			*m_head = NULL;
3411cda97c50SMike Silbersack 			return (ENOBUFS);
341282a67a70SMarius Strobl 		}
3413a10c0e45SMike Silbersack 		*m_head = m;
3414cda97c50SMike Silbersack 	}
3415993a741aSMarius Strobl 
341656e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
34175f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3418ebc284ccSMarius Strobl 	    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3419ebc284ccSMarius Strobl 	if (error == EFBIG) {
3420c6499eccSGleb Smirnoff 		if (defragged != 0 || (m = m_collapse(*m_head, M_NOWAIT,
3421993a741aSMarius Strobl 		    DC_MAXFRAGS)) == NULL) {
3422ebc284ccSMarius Strobl 			m_freem(*m_head);
342382a67a70SMarius Strobl 			*m_head = NULL;
3424993a741aSMarius Strobl 			return (defragged != 0 ? error : ENOBUFS);
342582a67a70SMarius Strobl 		}
3426ebc284ccSMarius Strobl 		*m_head = m;
34275f14ee23SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3428ebc284ccSMarius Strobl 		    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3429ebc284ccSMarius Strobl 		if (error != 0) {
3430ebc284ccSMarius Strobl 			m_freem(*m_head);
3431ebc284ccSMarius Strobl 			*m_head = NULL;
3432ebc284ccSMarius Strobl 			return (error);
343382a67a70SMarius Strobl 		}
3434ebc284ccSMarius Strobl 	} else if (error != 0)
3435ebc284ccSMarius Strobl 		return (error);
3436ebc284ccSMarius Strobl 	KASSERT(nseg <= DC_MAXFRAGS,
3437ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
3438ebc284ccSMarius Strobl 	if (nseg == 0) {
3439ebc284ccSMarius Strobl 		m_freem(*m_head);
3440ebc284ccSMarius Strobl 		*m_head = NULL;
3441ebc284ccSMarius Strobl 		return (EIO);
3442ebc284ccSMarius Strobl 	}
3443ebc284ccSMarius Strobl 
34445f14ee23SPyun YongHyeon 	/* Check descriptor overruns. */
34455f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt + nseg > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
34465f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
34475f14ee23SPyun YongHyeon 		return (ENOBUFS);
34485f14ee23SPyun YongHyeon 	}
34495f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
34505f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
34515f14ee23SPyun YongHyeon 
3452ebc284ccSMarius Strobl 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3453ebc284ccSMarius Strobl 	for (i = 0; i < nseg; i++) {
3454ebc284ccSMarius Strobl 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3455ebc284ccSMarius Strobl 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3456ebc284ccSMarius Strobl 		    (first != sc->dc_cdata.dc_tx_first)) {
34575f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_mtag,
3458ebc284ccSMarius Strobl 			    sc->dc_cdata.dc_tx_map[first]);
3459ebc284ccSMarius Strobl 			m_freem(*m_head);
3460ebc284ccSMarius Strobl 			*m_head = NULL;
3461ebc284ccSMarius Strobl 			return (ENOBUFS);
3462ebc284ccSMarius Strobl 		}
3463ebc284ccSMarius Strobl 
34645f14ee23SPyun YongHyeon 		f = &sc->dc_ldata.dc_tx_list[frag];
3465ebc284ccSMarius Strobl 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3466ebc284ccSMarius Strobl 		if (i == 0) {
3467ebc284ccSMarius Strobl 			f->dc_status = 0;
3468ebc284ccSMarius Strobl 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3469ebc284ccSMarius Strobl 		} else
3470ebc284ccSMarius Strobl 			f->dc_status = htole32(DC_TXSTAT_OWN);
34715f14ee23SPyun YongHyeon 		f->dc_data = htole32(DC_ADDR_LO(segs[i].ds_addr));
3472ebc284ccSMarius Strobl 		cur = frag;
3473ebc284ccSMarius Strobl 		DC_INC(frag, DC_TX_LIST_CNT);
3474ebc284ccSMarius Strobl 	}
3475ebc284ccSMarius Strobl 
3476ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_prod = frag;
3477ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_cnt += nseg;
3478ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_chain[cur] = *m_head;
34795f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3480ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
34815f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[first].dc_ctl |=
3482ebc284ccSMarius Strobl 		    htole32(DC_TXCTL_FINT);
3483ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
34845f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
348506d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR &&
348606d23883SPyun YongHyeon 	    ++sc->dc_cdata.dc_tx_pkts >= 8) {
348706d23883SPyun YongHyeon 		sc->dc_cdata.dc_tx_pkts = 0;
34885f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
348906d23883SPyun YongHyeon 	}
34905f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3491ebc284ccSMarius Strobl 
34925f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
34935f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
34945f14ee23SPyun YongHyeon 
34955f14ee23SPyun YongHyeon 	/*
34965f14ee23SPyun YongHyeon 	 * Swap the last and the first dmamaps to ensure the map for
34975f14ee23SPyun YongHyeon 	 * this transmission is placed at the last descriptor.
34985f14ee23SPyun YongHyeon 	 */
34995f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_tx_map[cur];
35005f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[cur] = sc->dc_cdata.dc_tx_map[first];
35015f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[first] = map;
35025f14ee23SPyun YongHyeon 
350396f2e892SBill Paul 	return (0);
350496f2e892SBill Paul }
350596f2e892SBill Paul 
3506e3d2833aSAlfred Perlstein static void
35070934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
350896f2e892SBill Paul {
350996f2e892SBill Paul 	struct dc_softc *sc;
3510c8b27acaSJohn Baldwin 
3511c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3512c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3513c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3514c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3515c8b27acaSJohn Baldwin }
3516c8b27acaSJohn Baldwin 
3517ebc284ccSMarius Strobl /*
3518ebc284ccSMarius Strobl  * Main transmit routine
3519ebc284ccSMarius Strobl  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3520ebc284ccSMarius Strobl  * regions directly in the transmit lists.  We also save a copy of the
3521ebc284ccSMarius Strobl  * pointers since the transmit list fragment pointers are physical
3522ebc284ccSMarius Strobl  * addresses.
3523ebc284ccSMarius Strobl  */
3524c8b27acaSJohn Baldwin static void
3525c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3526c8b27acaSJohn Baldwin {
3527c8b27acaSJohn Baldwin 	struct dc_softc *sc;
35285f14ee23SPyun YongHyeon 	struct mbuf *m_head;
35295f14ee23SPyun YongHyeon 	int queued;
353096f2e892SBill Paul 
353196f2e892SBill Paul 	sc = ifp->if_softc;
353296f2e892SBill Paul 
3533c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
353496f2e892SBill Paul 
353576d40c85SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
353676d40c85SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->dc_link == 0)
3537d1ce9105SBill Paul 		return;
353896f2e892SBill Paul 
35395f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
354096f2e892SBill Paul 
35415f14ee23SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
35425f14ee23SPyun YongHyeon 		/*
35435f14ee23SPyun YongHyeon 		 * If there's no way we can send any packets, return now.
35445f14ee23SPyun YongHyeon 		 */
35455f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
35465f14ee23SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
35475f14ee23SPyun YongHyeon 			break;
35485f14ee23SPyun YongHyeon 		}
3549cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
355096f2e892SBill Paul 		if (m_head == NULL)
355196f2e892SBill Paul 			break;
355296f2e892SBill Paul 
3553a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
355482a67a70SMarius Strobl 			if (m_head == NULL)
355582a67a70SMarius Strobl 				break;
3556cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
355713f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
355896f2e892SBill Paul 			break;
355996f2e892SBill Paul 		}
356096f2e892SBill Paul 
3561cbaf877fSBrian Feldman 		queued++;
356296f2e892SBill Paul 		/*
356396f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
356496f2e892SBill Paul 		 * to him.
356596f2e892SBill Paul 		 */
35669ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
356796f2e892SBill Paul 	}
356896f2e892SBill Paul 
3569cbaf877fSBrian Feldman 	if (queued > 0) {
357096f2e892SBill Paul 		/* Transmit */
357196f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
357296f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
357396f2e892SBill Paul 
357496f2e892SBill Paul 		/*
357596f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
357696f2e892SBill Paul 		 */
3577b1d16143SMarius Strobl 		sc->dc_wdog_timer = 5;
3578cbaf877fSBrian Feldman 	}
357996f2e892SBill Paul }
358096f2e892SBill Paul 
3581e3d2833aSAlfred Perlstein static void
35820934f18aSMaxime Henrion dc_init(void *xsc)
358396f2e892SBill Paul {
358496f2e892SBill Paul 	struct dc_softc *sc = xsc;
3585c8b27acaSJohn Baldwin 
3586c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3587c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3588c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3589c8b27acaSJohn Baldwin }
3590c8b27acaSJohn Baldwin 
3591c8b27acaSJohn Baldwin static void
3592c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3593c8b27acaSJohn Baldwin {
3594fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
359596f2e892SBill Paul 	struct mii_data *mii;
3596d314ebf5SPyun YongHyeon 	struct ifmedia *ifm;
359796f2e892SBill Paul 
3598c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
359996f2e892SBill Paul 
36008f382a1fSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
36018f382a1fSPyun YongHyeon 		return;
36028f382a1fSPyun YongHyeon 
360396f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
360496f2e892SBill Paul 
360596f2e892SBill Paul 	/*
360696f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
360796f2e892SBill Paul 	 */
360896f2e892SBill Paul 	dc_stop(sc);
360996f2e892SBill Paul 	dc_reset(sc);
3610d314ebf5SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
3611d314ebf5SPyun YongHyeon 		ifm = &mii->mii_media;
3612d314ebf5SPyun YongHyeon 		dc_apply_fixup(sc, ifm->ifm_media);
3613d314ebf5SPyun YongHyeon 	}
361496f2e892SBill Paul 
361596f2e892SBill Paul 	/*
361696f2e892SBill Paul 	 * Set cache alignment and burst length.
361796f2e892SBill Paul 	 */
361852ca7ee2SPyun YongHyeon 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc) || DC_IS_ULI(sc))
361996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
362096f2e892SBill Paul 	else
362196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3622935fe010SLuigi Rizzo 	/*
3623935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3624935fe010SLuigi Rizzo 	 */
3625935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3626935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
362796f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
362896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
362996f2e892SBill Paul 	} else {
363096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
363196f2e892SBill Paul 	}
363296f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
363396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
363496f2e892SBill Paul 	switch(sc->dc_cachesize) {
363596f2e892SBill Paul 	case 32:
363696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
363796f2e892SBill Paul 		break;
363896f2e892SBill Paul 	case 16:
363996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
364096f2e892SBill Paul 		break;
364196f2e892SBill Paul 	case 8:
364296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
364396f2e892SBill Paul 		break;
364496f2e892SBill Paul 	case 0:
364596f2e892SBill Paul 	default:
364696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
364796f2e892SBill Paul 		break;
364896f2e892SBill Paul 	}
364996f2e892SBill Paul 
365096f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
365196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365296f2e892SBill Paul 	else {
3653d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
365496f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365596f2e892SBill Paul 		} else {
365696f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365796f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
365896f2e892SBill Paul 		}
365996f2e892SBill Paul 	}
366096f2e892SBill Paul 
366196f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
366296f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
366396f2e892SBill Paul 
366496f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
366596f2e892SBill Paul 		/*
366696f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
366796f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
366896f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
366996f2e892SBill Paul 		 * document the meaning of these bits so there's no way
367096f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
367196f2e892SBill Paul 		 * number all its own; the rest all use a different one.
367296f2e892SBill Paul 		 */
367396f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
367496f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
367596f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
367696f2e892SBill Paul 		else
367796f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
367896f2e892SBill Paul 	}
367996f2e892SBill Paul 
3680feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3681feb78939SJonathan Chen 		/*
3682feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3683feb78939SJonathan Chen 		 * can talk to the MII.
3684feb78939SJonathan Chen 		 */
3685feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3686feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3687feb78939SJonathan Chen 		DELAY(10);
3688feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3689feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3690feb78939SJonathan Chen 		DELAY(10);
3691feb78939SJonathan Chen 	}
3692feb78939SJonathan Chen 
369396f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3694d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
369596f2e892SBill Paul 
369696f2e892SBill Paul 	/* Init circular RX list. */
369796f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
36986b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
369922f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
370096f2e892SBill Paul 		dc_stop(sc);
370196f2e892SBill Paul 		return;
370296f2e892SBill Paul 	}
370396f2e892SBill Paul 
370496f2e892SBill Paul 	/*
370556e5e7aeSMaxime Henrion 	 * Init TX descriptors.
370696f2e892SBill Paul 	 */
370796f2e892SBill Paul 	dc_list_tx_init(sc);
370896f2e892SBill Paul 
370996f2e892SBill Paul 	/*
371096f2e892SBill Paul 	 * Load the address of the RX list.
371196f2e892SBill Paul 	 */
371256e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
371356e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
371496f2e892SBill Paul 
371596f2e892SBill Paul 	/*
371696f2e892SBill Paul 	 * Enable interrupts.
371796f2e892SBill Paul 	 */
3718e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3719e4fc250cSLuigi Rizzo 	/*
3720e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3721e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3722e4fc250cSLuigi Rizzo 	 * after a reset.
3723e4fc250cSLuigi Rizzo 	 */
372440929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3725e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3726e4fc250cSLuigi Rizzo 	else
3727e4fc250cSLuigi Rizzo #endif
372896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
372996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
373096f2e892SBill Paul 
373152ca7ee2SPyun YongHyeon 	/* Initialize TX jabber and RX watchdog timer. */
373252ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
373352ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_WATCHDOG, DC_WDOG_JABBERCLK |
373452ca7ee2SPyun YongHyeon 		    DC_WDOG_HOSTUNJAB);
373552ca7ee2SPyun YongHyeon 
373696f2e892SBill Paul 	/* Enable transmitter. */
373796f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
373896f2e892SBill Paul 
373996f2e892SBill Paul 	/*
3740918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3741918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3742918434c8SBill Paul 	 * link and activity indications.
3743918434c8SBill Paul 	 */
374478999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3745918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3746918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
374778999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3748918434c8SBill Paul 	}
3749918434c8SBill Paul 
3750918434c8SBill Paul 	/*
375196f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
375296f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
375396f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
375496f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
375596f2e892SBill Paul 	 */
375696f2e892SBill Paul 	dc_setfilt(sc);
375796f2e892SBill Paul 
375896f2e892SBill Paul 	/* Enable receiver. */
375996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
376096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
376196f2e892SBill Paul 
376213f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
376313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
376496f2e892SBill Paul 
3765d7e9ac75SPyun YongHyeon 	dc_ifmedia_upd_locked(sc);
3766d314ebf5SPyun YongHyeon 
376726b40a65SPyun YongHyeon 	/* Clear missed frames and overflow counter. */
376826b40a65SPyun YongHyeon 	CSR_READ_4(sc, DC_FRAMESDISCARDED);
376926b40a65SPyun YongHyeon 
3770857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
377145521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3772857fd445SBill Paul 		sc->dc_link = 1;
3773857fd445SBill Paul 	else {
3774318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3775b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3776318b02fdSBill Paul 		else
3777b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3778857fd445SBill Paul 	}
3779b1d16143SMarius Strobl 
3780b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
3781b1d16143SMarius Strobl 	callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
378296f2e892SBill Paul }
378396f2e892SBill Paul 
378496f2e892SBill Paul /*
378596f2e892SBill Paul  * Set media options.
378696f2e892SBill Paul  */
3787e3d2833aSAlfred Perlstein static int
37880934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
378996f2e892SBill Paul {
379096f2e892SBill Paul 	struct dc_softc *sc;
3791d7e9ac75SPyun YongHyeon 	int error;
379296f2e892SBill Paul 
379396f2e892SBill Paul 	sc = ifp->if_softc;
3794c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3795d7e9ac75SPyun YongHyeon 	error = dc_ifmedia_upd_locked(sc);
3796d7e9ac75SPyun YongHyeon 	DC_UNLOCK(sc);
3797d7e9ac75SPyun YongHyeon 	return (error);
3798d7e9ac75SPyun YongHyeon }
3799f43d9309SBill Paul 
3800d7e9ac75SPyun YongHyeon static int
3801d7e9ac75SPyun YongHyeon dc_ifmedia_upd_locked(struct dc_softc *sc)
3802d7e9ac75SPyun YongHyeon {
3803d7e9ac75SPyun YongHyeon 	struct mii_data *mii;
3804d7e9ac75SPyun YongHyeon 	struct ifmedia *ifm;
3805d7e9ac75SPyun YongHyeon 	int error;
3806d7e9ac75SPyun YongHyeon 
3807d7e9ac75SPyun YongHyeon 	DC_LOCK_ASSERT(sc);
3808d7e9ac75SPyun YongHyeon 
3809d7e9ac75SPyun YongHyeon 	sc->dc_link = 0;
3810d7e9ac75SPyun YongHyeon 	mii = device_get_softc(sc->dc_miibus);
3811d7e9ac75SPyun YongHyeon 	error = mii_mediachg(mii);
3812d7e9ac75SPyun YongHyeon 	if (error == 0) {
3813d7e9ac75SPyun YongHyeon 		ifm = &mii->mii_media;
3814d314ebf5SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3815d314ebf5SPyun YongHyeon 			dc_setcfg(sc, ifm->ifm_media);
3816d314ebf5SPyun YongHyeon 		else if (DC_IS_DAVICOM(sc) &&
381745521525SPoul-Henning Kamp 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3818f43d9309SBill Paul 			dc_setcfg(sc, ifm->ifm_media);
3819d7e9ac75SPyun YongHyeon 	}
382096f2e892SBill Paul 
3821d7e9ac75SPyun YongHyeon 	return (error);
382296f2e892SBill Paul }
382396f2e892SBill Paul 
382496f2e892SBill Paul /*
382596f2e892SBill Paul  * Report current media status.
382696f2e892SBill Paul  */
3827e3d2833aSAlfred Perlstein static void
38280934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
382996f2e892SBill Paul {
383096f2e892SBill Paul 	struct dc_softc *sc;
383196f2e892SBill Paul 	struct mii_data *mii;
3832f43d9309SBill Paul 	struct ifmedia *ifm;
383396f2e892SBill Paul 
383496f2e892SBill Paul 	sc = ifp->if_softc;
383596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3836c8b27acaSJohn Baldwin 	DC_LOCK(sc);
383796f2e892SBill Paul 	mii_pollstat(mii);
3838f43d9309SBill Paul 	ifm = &mii->mii_media;
3839f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
384045521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3841f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3842f43d9309SBill Paul 			ifmr->ifm_status = 0;
3843432120f2SMarius Strobl 			DC_UNLOCK(sc);
3844f43d9309SBill Paul 			return;
3845f43d9309SBill Paul 		}
3846f43d9309SBill Paul 	}
384796f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
384896f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3849c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
385096f2e892SBill Paul }
385196f2e892SBill Paul 
3852e3d2833aSAlfred Perlstein static int
38530934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
385496f2e892SBill Paul {
385596f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
385696f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
385796f2e892SBill Paul 	struct mii_data *mii;
3858d1ce9105SBill Paul 	int error = 0;
385996f2e892SBill Paul 
386096f2e892SBill Paul 	switch (command) {
386196f2e892SBill Paul 	case SIOCSIFFLAGS:
3862c8b27acaSJohn Baldwin 		DC_LOCK(sc);
386396f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
38645d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
38655d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
38665d6dfbbbSLuigi Rizzo 
386713f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
38685d6dfbbbSLuigi Rizzo 				if (need_setfilt)
386996f2e892SBill Paul 					dc_setfilt(sc);
38705d6dfbbbSLuigi Rizzo 			} else {
38718f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3872c8b27acaSJohn Baldwin 				dc_init_locked(sc);
387396f2e892SBill Paul 			}
387496f2e892SBill Paul 		} else {
387513f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
387696f2e892SBill Paul 				dc_stop(sc);
387796f2e892SBill Paul 		}
387896f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3879c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
388096f2e892SBill Paul 		break;
388196f2e892SBill Paul 	case SIOCADDMULTI:
388296f2e892SBill Paul 	case SIOCDELMULTI:
3883c8b27acaSJohn Baldwin 		DC_LOCK(sc);
388424507bc1SPyun YongHyeon 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
388596f2e892SBill Paul 			dc_setfilt(sc);
3886c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
388796f2e892SBill Paul 		break;
388896f2e892SBill Paul 	case SIOCGIFMEDIA:
388996f2e892SBill Paul 	case SIOCSIFMEDIA:
389096f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
389196f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
389296f2e892SBill Paul 		break;
3893e695984eSRuslan Ermilov 	case SIOCSIFCAP:
389440929967SGleb Smirnoff #ifdef DEVICE_POLLING
389540929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
389640929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
389740929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
389840929967SGleb Smirnoff 			if (error)
389940929967SGleb Smirnoff 				return(error);
3900c8b27acaSJohn Baldwin 			DC_LOCK(sc);
390140929967SGleb Smirnoff 			/* Disable interrupts */
390240929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
390340929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3904c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
390540929967SGleb Smirnoff 			return (error);
390640929967SGleb Smirnoff 		}
390740929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
390840929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
390940929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
391040929967SGleb Smirnoff 			/* Enable interrupts. */
391140929967SGleb Smirnoff 			DC_LOCK(sc);
391240929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
391340929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
391440929967SGleb Smirnoff 			DC_UNLOCK(sc);
391540929967SGleb Smirnoff 			return (error);
391640929967SGleb Smirnoff 		}
391740929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3918e695984eSRuslan Ermilov 		break;
391996f2e892SBill Paul 	default:
39209ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
392196f2e892SBill Paul 		break;
392296f2e892SBill Paul 	}
392396f2e892SBill Paul 
392496f2e892SBill Paul 	return (error);
392596f2e892SBill Paul }
392696f2e892SBill Paul 
3927e3d2833aSAlfred Perlstein static void
3928b1d16143SMarius Strobl dc_watchdog(void *xsc)
392996f2e892SBill Paul {
3930b1d16143SMarius Strobl 	struct dc_softc *sc = xsc;
3931b1d16143SMarius Strobl 	struct ifnet *ifp;
393296f2e892SBill Paul 
3933b1d16143SMarius Strobl 	DC_LOCK_ASSERT(sc);
393496f2e892SBill Paul 
3935b1d16143SMarius Strobl 	if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3936b1d16143SMarius Strobl 		callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3937b1d16143SMarius Strobl 		return;
3938b1d16143SMarius Strobl 	}
3939d1ce9105SBill Paul 
3940b1d16143SMarius Strobl 	ifp = sc->dc_ifp;
3941c8dfaf38SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3942b1d16143SMarius Strobl 	device_printf(sc->dc_dev, "watchdog timeout\n");
394396f2e892SBill Paul 
39448f382a1fSPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3945c8b27acaSJohn Baldwin 	dc_init_locked(sc);
394696f2e892SBill Paul 
3947cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3948c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
394996f2e892SBill Paul }
395096f2e892SBill Paul 
395196f2e892SBill Paul /*
395296f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
395396f2e892SBill Paul  * RX and TX lists.
395496f2e892SBill Paul  */
3955e3d2833aSAlfred Perlstein static void
39560934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
395796f2e892SBill Paul {
395896f2e892SBill Paul 	struct ifnet *ifp;
3959b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3960b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3961b3811c95SMaxime Henrion 	int i;
39621da7683aSPyun YongHyeon 	uint32_t ctl, netcfg;
396396f2e892SBill Paul 
3964c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3965d1ce9105SBill Paul 
3966fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
39675f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
3968b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
396996f2e892SBill Paul 
3970b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
3971b1d16143SMarius Strobl 	callout_stop(&sc->dc_wdog_ch);
3972b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
39731da7683aSPyun YongHyeon 	sc->dc_link = 0;
397496f2e892SBill Paul 
397513f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
39763b3ec200SPeter Wemm 
39771da7683aSPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
39781da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39791da7683aSPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
39801da7683aSPyun YongHyeon 		   netcfg & ~(DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
398196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
39821da7683aSPyun YongHyeon 	/* Wait the completion of TX/RX SM. */
39831da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39841da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
39851da7683aSPyun YongHyeon 
398696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
398796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
398896f2e892SBill Paul 
398996f2e892SBill Paul 	/*
399096f2e892SBill Paul 	 * Free data in the RX lists.
399196f2e892SBill Paul 	 */
399296f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3993b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
39945f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_rx_mtag,
39955f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i], BUS_DMASYNC_POSTREAD);
39965f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_mtag,
39975f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i]);
399856e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
399956e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
400096f2e892SBill Paul 		}
400196f2e892SBill Paul 	}
40025f14ee23SPyun YongHyeon 	bzero(ld->dc_rx_list, DC_RX_LIST_SZ);
40035f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
40045f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
400596f2e892SBill Paul 
400696f2e892SBill Paul 	/*
400796f2e892SBill Paul 	 * Free the TX list buffers.
400896f2e892SBill Paul 	 */
400996f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
4010b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
4011af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
40125f14ee23SPyun YongHyeon 			if (ctl & DC_TXCTL_SETUP) {
40135f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
40145f14ee23SPyun YongHyeon 				    BUS_DMASYNC_POSTWRITE);
40155f14ee23SPyun YongHyeon 			} else {
40165f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_tx_mtag,
40175f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i], BUS_DMASYNC_POSTWRITE);
40185f14ee23SPyun YongHyeon 				bus_dmamap_unload(sc->dc_tx_mtag,
40195f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i]);
402056e5e7aeSMaxime Henrion 				m_freem(cd->dc_tx_chain[i]);
40215f14ee23SPyun YongHyeon 			}
4022b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
402396f2e892SBill Paul 		}
402496f2e892SBill Paul 	}
40255f14ee23SPyun YongHyeon 	bzero(ld->dc_tx_list, DC_TX_LIST_SZ);
40265f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
40275f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
402896f2e892SBill Paul }
402996f2e892SBill Paul 
403096f2e892SBill Paul /*
4031e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
4032e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
4033e8388e14SMitsuru IWASAKI  * resume.
4034e8388e14SMitsuru IWASAKI  */
4035e3d2833aSAlfred Perlstein static int
40360934f18aSMaxime Henrion dc_suspend(device_t dev)
4037e8388e14SMitsuru IWASAKI {
4038e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4039e8388e14SMitsuru IWASAKI 
4040e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4041c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4042e8388e14SMitsuru IWASAKI 	dc_stop(sc);
4043e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
4044c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4045e8388e14SMitsuru IWASAKI 
4046e8388e14SMitsuru IWASAKI 	return (0);
4047e8388e14SMitsuru IWASAKI }
4048e8388e14SMitsuru IWASAKI 
4049e8388e14SMitsuru IWASAKI /*
4050e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
4051e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
4052e8388e14SMitsuru IWASAKI  * appropriate.
4053e8388e14SMitsuru IWASAKI  */
4054e3d2833aSAlfred Perlstein static int
40550934f18aSMaxime Henrion dc_resume(device_t dev)
4056e8388e14SMitsuru IWASAKI {
4057e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4058e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
4059e8388e14SMitsuru IWASAKI 
4060e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4061fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
4062e8388e14SMitsuru IWASAKI 
4063e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
4064c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4065e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
4066c8b27acaSJohn Baldwin 		dc_init_locked(sc);
4067e8388e14SMitsuru IWASAKI 
4068e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
4069c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4070e8388e14SMitsuru IWASAKI 
4071e8388e14SMitsuru IWASAKI 	return (0);
4072e8388e14SMitsuru IWASAKI }
4073e8388e14SMitsuru IWASAKI 
4074e8388e14SMitsuru IWASAKI /*
407596f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
407696f2e892SBill Paul  * get confused by errant DMAs when rebooting.
407796f2e892SBill Paul  */
40786a087a87SPyun YongHyeon static int
40790934f18aSMaxime Henrion dc_shutdown(device_t dev)
408096f2e892SBill Paul {
408196f2e892SBill Paul 	struct dc_softc *sc;
408296f2e892SBill Paul 
408396f2e892SBill Paul 	sc = device_get_softc(dev);
408496f2e892SBill Paul 
4085c8b27acaSJohn Baldwin 	DC_LOCK(sc);
408696f2e892SBill Paul 	dc_stop(sc);
4087c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
40886a087a87SPyun YongHyeon 
40896a087a87SPyun YongHyeon 	return (0);
409096f2e892SBill Paul }
409139d76ed6SPyun YongHyeon 
409239d76ed6SPyun YongHyeon static int
409339d76ed6SPyun YongHyeon dc_check_multiport(struct dc_softc *sc)
409439d76ed6SPyun YongHyeon {
409539d76ed6SPyun YongHyeon 	struct dc_softc *dsc;
409639d76ed6SPyun YongHyeon 	devclass_t dc;
409739d76ed6SPyun YongHyeon 	device_t child;
409839d76ed6SPyun YongHyeon 	uint8_t *eaddr;
409939d76ed6SPyun YongHyeon 	int unit;
410039d76ed6SPyun YongHyeon 
410139d76ed6SPyun YongHyeon 	dc = devclass_find("dc");
410239d76ed6SPyun YongHyeon 	for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
410339d76ed6SPyun YongHyeon 		child = devclass_get_device(dc, unit);
410439d76ed6SPyun YongHyeon 		if (child == NULL)
410539d76ed6SPyun YongHyeon 			continue;
410639d76ed6SPyun YongHyeon 		if (child == sc->dc_dev)
410739d76ed6SPyun YongHyeon 			continue;
410839d76ed6SPyun YongHyeon 		if (device_get_parent(child) != device_get_parent(sc->dc_dev))
410939d76ed6SPyun YongHyeon 			continue;
411039d76ed6SPyun YongHyeon 		if (unit > device_get_unit(sc->dc_dev))
411139d76ed6SPyun YongHyeon 			continue;
4112b289c607SPyun YongHyeon 		if (device_is_attached(child) == 0)
4113b289c607SPyun YongHyeon 			continue;
411439d76ed6SPyun YongHyeon 		dsc = device_get_softc(child);
4115b289c607SPyun YongHyeon 		device_printf(sc->dc_dev,
4116b289c607SPyun YongHyeon 		    "Using station address of %s as base\n",
411739d76ed6SPyun YongHyeon 		    device_get_nameunit(child));
411839d76ed6SPyun YongHyeon 		bcopy(dsc->dc_eaddr, sc->dc_eaddr, ETHER_ADDR_LEN);
411939d76ed6SPyun YongHyeon 		eaddr = (uint8_t *)sc->dc_eaddr;
412039d76ed6SPyun YongHyeon 		eaddr[5]++;
4121b289c607SPyun YongHyeon 		/* Prepare SROM to parse again. */
4122b289c607SPyun YongHyeon 		if (DC_IS_INTEL(sc) && dsc->dc_srom != NULL &&
4123b289c607SPyun YongHyeon 		    sc->dc_romwidth != 0) {
4124b289c607SPyun YongHyeon 			free(sc->dc_srom, M_DEVBUF);
4125b289c607SPyun YongHyeon 			sc->dc_romwidth = dsc->dc_romwidth;
4126b289c607SPyun YongHyeon 			sc->dc_srom = malloc(DC_ROM_SIZE(sc->dc_romwidth),
4127b289c607SPyun YongHyeon 			    M_DEVBUF, M_NOWAIT);
4128b289c607SPyun YongHyeon 			if (sc->dc_srom == NULL) {
4129b289c607SPyun YongHyeon 				device_printf(sc->dc_dev,
4130b289c607SPyun YongHyeon 				    "Could not allocate SROM buffer\n");
4131b289c607SPyun YongHyeon 				return (ENOMEM);
4132b289c607SPyun YongHyeon 			}
4133b289c607SPyun YongHyeon 			bcopy(dsc->dc_srom, sc->dc_srom,
4134b289c607SPyun YongHyeon 			    DC_ROM_SIZE(sc->dc_romwidth));
4135b289c607SPyun YongHyeon 		}
413639d76ed6SPyun YongHyeon 		return (0);
413739d76ed6SPyun YongHyeon 	}
413839d76ed6SPyun YongHyeon 	return (ENOENT);
413939d76ed6SPyun YongHyeon }
4140