xref: /freebsd/sys/dev/dc/if_dc.c (revision 4e62c3cafa4c4e41efd6f87b7fe559cf819cf3e4)
160727d8bSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro 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>
3696f2e892SBill Paul /*
3796f2e892SBill Paul  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
3896f2e892SBill Paul  * series chips and several workalikes including the following:
3996f2e892SBill Paul  *
40ead7cde9SBill Paul  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
4196f2e892SBill Paul  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
4296f2e892SBill Paul  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
4396f2e892SBill Paul  * ASIX Electronics AX88140A (www.asix.com.tw)
4496f2e892SBill Paul  * ASIX Electronics AX88141 (www.asix.com.tw)
4596f2e892SBill Paul  * ADMtek AL981 (www.admtek.com.tw)
46593a1aeaSMartin Blapp  * ADMtek AN983 (www.admtek.com.tw)
47a2d61e43SWarner Losh  * ADMtek CardBus AN985 (www.admtek.com.tw)
48a2d61e43SWarner Losh  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek CardBus AN985
4988d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
509ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
51feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
521d5e5310SBill Paul  * Abocom FE2500
531af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
547eac366bSMartin Blapp  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
5596f2e892SBill Paul  *
5696f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5796f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5896f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5996f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
6096f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
6196f2e892SBill Paul  * instead of 512.
6296f2e892SBill Paul  *
6396f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
6496f2e892SBill Paul  * Electrical Engineering Department
6596f2e892SBill Paul  * Columbia University, New York City
6696f2e892SBill Paul  */
6796f2e892SBill Paul /*
6896f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6996f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
7096f2e892SBill Paul  * three kinds of media attachments:
7196f2e892SBill Paul  *
7296f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
7396f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7496f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7596f2e892SBill Paul  * o 10baseT port.
7696f2e892SBill Paul  * o AUI/BNC port.
7796f2e892SBill Paul  *
7896f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7996f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
8096f2e892SBill Paul  * autosensing configuration.
8196f2e892SBill Paul  *
8296f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
8396f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8496f2e892SBill Paul  * handled separately due to its different register offsets and the
8596f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8696f2e892SBill Paul  * here, but I'm not thrilled about it.
8796f2e892SBill Paul  *
8896f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8996f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
9096f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
9196f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
9296f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
9396f2e892SBill Paul  */
9496f2e892SBill Paul 
95f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
96f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
97f0796cd2SGleb Smirnoff #endif
98f0796cd2SGleb Smirnoff 
9996f2e892SBill Paul #include <sys/param.h>
100af4358c7SMaxime Henrion #include <sys/endian.h>
10196f2e892SBill Paul #include <sys/systm.h>
10296f2e892SBill Paul #include <sys/sockio.h>
10396f2e892SBill Paul #include <sys/mbuf.h>
10496f2e892SBill Paul #include <sys/malloc.h>
10596f2e892SBill Paul #include <sys/kernel.h>
106f11d01c3SPoul-Henning Kamp #include <sys/module.h>
10796f2e892SBill Paul #include <sys/socket.h>
10896f2e892SBill Paul 
10996f2e892SBill Paul #include <net/if.h>
11076039bc8SGleb Smirnoff #include <net/if_var.h>
11196f2e892SBill Paul #include <net/if_arp.h>
11296f2e892SBill Paul #include <net/ethernet.h>
11396f2e892SBill Paul #include <net/if_dl.h>
11496f2e892SBill Paul #include <net/if_media.h>
115db40c1aeSDoug Ambrisko #include <net/if_types.h>
116db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
11796f2e892SBill Paul 
11896f2e892SBill Paul #include <net/bpf.h>
11996f2e892SBill Paul 
12096f2e892SBill Paul #include <machine/bus.h>
12196f2e892SBill Paul #include <machine/resource.h>
12296f2e892SBill Paul #include <sys/bus.h>
12396f2e892SBill Paul #include <sys/rman.h>
12496f2e892SBill Paul 
12596f2e892SBill Paul #include <dev/mii/mii.h>
1268c1093fcSMarius Strobl #include <dev/mii/mii_bitbang.h>
12796f2e892SBill Paul #include <dev/mii/miivar.h>
12896f2e892SBill Paul 
12919b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
13019b7ffd1SWarner Losh #include <dev/pci/pcivar.h>
13196f2e892SBill Paul 
13296f2e892SBill Paul #define	DC_USEIOSPACE
13396f2e892SBill Paul 
1346a3033a8SWarner Losh #include <dev/dc/if_dcreg.h>
13596f2e892SBill Paul 
136f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, pci, 1, 1, 1);
137f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, ether, 1, 1, 1);
13895a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
13995a16455SPeter Wemm 
140919ccba7SWarner Losh /*
141919ccba7SWarner Losh  * "device miibus" is required in kernel config.  See GENERIC if you get
142919ccba7SWarner Losh  * errors here.
143919ccba7SWarner Losh  */
14496f2e892SBill Paul #include "miibus_if.h"
14596f2e892SBill Paul 
14696f2e892SBill Paul /*
14796f2e892SBill Paul  * Various supported device vendors/types and their names.
14896f2e892SBill Paul  */
14929658c96SDimitry Andric static const struct dc_type dc_devs[] = {
1501e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143), 0,
15196f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
1521e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009), 0,
15338deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
1541e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100), 0,
15596f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
1561e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), DC_REVISION_DM9102A,
15788d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
1581e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), 0,
1591e2e70b1SJohn Baldwin 		"Davicom DM9102 10/100BaseTX" },
1601e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981), 0,
16196f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
162593a1aeaSMartin Blapp 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983), 0,
163593a1aeaSMartin Blapp 		"ADMtek AN983 10/100BaseTX" },
1641e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
165a2d61e43SWarner Losh 		"ADMtek AN985 CardBus 10/100BaseTX or clone" },
1661e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
167e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
1681e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
169e351d778SMartin Blapp 		"ADMtek ADM9513 10/100BaseTX" },
1701e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), DC_REVISION_88141,
17196f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
1721e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), 0,
1731e2e70b1SJohn Baldwin 		"ASIX AX88140A 10/100BaseTX" },
1741e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), DC_REVISION_98713A,
17596f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
1761e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), 0,
1771e2e70b1SJohn Baldwin 		"Macronix 98713 10/100BaseTX" },
1781e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), DC_REVISION_98713A,
17996f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1801e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), 0,
18196f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1821e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98725,
18396f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
1841e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98715AEC_C,
1851e2e70b1SJohn Baldwin 		"Macronix 98715AEC-C 10/100BaseTX" },
1861e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), 0,
1871e2e70b1SJohn Baldwin 		"Macronix 98715/98715A 10/100BaseTX" },
1881e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727), 0,
189ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
1901e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115), 0,
19196f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
1921e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), DC_REVISION_82C169,
19396f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1941e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), 0,
1951e2e70b1SJohn Baldwin 		"82c168 PNIC 10/100BaseTX" },
1961e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217), 0,
1979ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
1981e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242), 0,
199fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
2001e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201), 0,
201feb78939SJonathan Chen 		"Xircom X3201 10/100BaseTX" },
2021e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD), 0,
2039be0993cSJohn Baldwin 		"Neteasy DRP-32TXD Cardbus 10/100" },
2041e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500), 0,
2051d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
2061e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX), 0,
207773c505fSMIHIRA Sanpei Yoshiro 		"Abocom FE2500MX 10/100BaseTX" },
2081e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112), 0,
2091af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
2101e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX), 0,
211948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
2121e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T), 0,
21397f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2141e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB), 0,
2157eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
2161e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120), 0,
217e7b9ab3aSBill Paul 		"Microsoft MN-120 CardBus 10/100" },
2181e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130), 0,
219e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
22017762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08), 0,
22117762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22217762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09), 0,
22317762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22452ca7ee2SPyun YongHyeon 	{ DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261), 0,
22552ca7ee2SPyun YongHyeon 		"ULi M5261 FastEthernet" },
22652ca7ee2SPyun YongHyeon 	{ DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263), 0,
22752ca7ee2SPyun YongHyeon 		"ULi M5263 FastEthernet" },
22896f2e892SBill Paul 	{ 0, 0, NULL }
22996f2e892SBill Paul };
23096f2e892SBill Paul 
231e51a25f8SAlfred Perlstein static int dc_probe(device_t);
232e51a25f8SAlfred Perlstein static int dc_attach(device_t);
233e51a25f8SAlfred Perlstein static int dc_detach(device_t);
234e8388e14SMitsuru IWASAKI static int dc_suspend(device_t);
235e8388e14SMitsuru IWASAKI static int dc_resume(device_t);
236ebc284ccSMarius Strobl static const struct dc_type *dc_devtype(device_t);
2375f14ee23SPyun YongHyeon static void dc_discard_rxbuf(struct dc_softc *, int);
2385f14ee23SPyun YongHyeon static int dc_newbuf(struct dc_softc *, int);
239a10c0e45SMike Silbersack static int dc_encap(struct dc_softc *, struct mbuf **);
240e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war(struct dc_softc *, int);
241e51a25f8SAlfred Perlstein static int dc_rx_resync(struct dc_softc *);
2421abcdbd1SAttilio Rao static int dc_rxeof(struct dc_softc *);
243e51a25f8SAlfred Perlstein static void dc_txeof(struct dc_softc *);
244e51a25f8SAlfred Perlstein static void dc_tick(void *);
245e51a25f8SAlfred Perlstein static void dc_tx_underrun(struct dc_softc *);
246e51a25f8SAlfred Perlstein static void dc_intr(void *);
247*91f3f3fcSJustin Hibbits static void dc_start(if_t);
248*91f3f3fcSJustin Hibbits static void dc_start_locked(if_t);
249*91f3f3fcSJustin Hibbits static int dc_ioctl(if_t, u_long, caddr_t);
250e51a25f8SAlfred Perlstein static void dc_init(void *);
251c8b27acaSJohn Baldwin static void dc_init_locked(struct dc_softc *);
252e51a25f8SAlfred Perlstein static void dc_stop(struct dc_softc *);
253b1d16143SMarius Strobl static void dc_watchdog(void *);
2546a087a87SPyun YongHyeon static int dc_shutdown(device_t);
255*91f3f3fcSJustin Hibbits static int dc_ifmedia_upd(if_t);
256d7e9ac75SPyun YongHyeon static int dc_ifmedia_upd_locked(struct dc_softc *);
257*91f3f3fcSJustin Hibbits static void dc_ifmedia_sts(if_t, struct ifmediareq *);
25896f2e892SBill Paul 
2595f14ee23SPyun YongHyeon static int dc_dma_alloc(struct dc_softc *);
2605f14ee23SPyun YongHyeon static void dc_dma_free(struct dc_softc *);
2615f14ee23SPyun YongHyeon static void dc_dma_map_addr(void *, bus_dma_segment_t *, int, int);
2625f14ee23SPyun YongHyeon 
263e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
264e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
265e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
266ee320f98SPyun YongHyeon static void dc_eeprom_getword(struct dc_softc *, int, uint16_t *);
267ee320f98SPyun YongHyeon static void dc_eeprom_getword_pnic(struct dc_softc *, int, uint16_t *);
268ee320f98SPyun YongHyeon static void dc_eeprom_getword_xircom(struct dc_softc *, int, uint16_t *);
2693097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
270e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
27196f2e892SBill Paul 
272e51a25f8SAlfred Perlstein static int dc_miibus_readreg(device_t, int, int);
273e51a25f8SAlfred Perlstein static int dc_miibus_writereg(device_t, int, int, int);
274e51a25f8SAlfred Perlstein static void dc_miibus_statchg(device_t);
275e51a25f8SAlfred Perlstein static void dc_miibus_mediainit(device_t);
27696f2e892SBill Paul 
277e51a25f8SAlfred Perlstein static void dc_setcfg(struct dc_softc *, int);
2781da7683aSPyun YongHyeon static void dc_netcfg_wait(struct dc_softc *);
2793373489bSWarner Losh static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
2803373489bSWarner Losh static uint32_t dc_mchash_be(const uint8_t *);
281e51a25f8SAlfred Perlstein static void dc_setfilt_21143(struct dc_softc *);
282e51a25f8SAlfred Perlstein static void dc_setfilt_asix(struct dc_softc *);
283e51a25f8SAlfred Perlstein static void dc_setfilt_admtek(struct dc_softc *);
28452ca7ee2SPyun YongHyeon static void dc_setfilt_uli(struct dc_softc *);
285e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
28696f2e892SBill Paul 
287e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
28896f2e892SBill Paul 
289e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
290e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
291e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
29296f2e892SBill Paul 
293abe4e865SPyun YongHyeon static int dc_read_srom(struct dc_softc *, int);
294abe4e865SPyun YongHyeon static int dc_parse_21143_srom(struct dc_softc *);
295abe4e865SPyun YongHyeon static int dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
296abe4e865SPyun YongHyeon static int dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
297abe4e865SPyun YongHyeon static int dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
298e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
29939d76ed6SPyun YongHyeon static int dc_check_multiport(struct dc_softc *);
3005c1cfac4SBill Paul 
3018c1093fcSMarius Strobl /*
3028c1093fcSMarius Strobl  * MII bit-bang glue
3038c1093fcSMarius Strobl  */
3048c1093fcSMarius Strobl static uint32_t dc_mii_bitbang_read(device_t);
3058c1093fcSMarius Strobl static void dc_mii_bitbang_write(device_t, uint32_t);
3068c1093fcSMarius Strobl 
3078c1093fcSMarius Strobl static const struct mii_bitbang_ops dc_mii_bitbang_ops = {
3088c1093fcSMarius Strobl 	dc_mii_bitbang_read,
3098c1093fcSMarius Strobl 	dc_mii_bitbang_write,
3108c1093fcSMarius Strobl 	{
3118c1093fcSMarius Strobl 		DC_SIO_MII_DATAOUT,	/* MII_BIT_MDO */
3128c1093fcSMarius Strobl 		DC_SIO_MII_DATAIN,	/* MII_BIT_MDI */
3138c1093fcSMarius Strobl 		DC_SIO_MII_CLK,		/* MII_BIT_MDC */
3148c1093fcSMarius Strobl 		0,			/* MII_BIT_DIR_HOST_PHY */
3158c1093fcSMarius Strobl 		DC_SIO_MII_DIR,		/* MII_BIT_DIR_PHY_HOST */
3168c1093fcSMarius Strobl 	}
3178c1093fcSMarius Strobl };
3188c1093fcSMarius Strobl 
31996f2e892SBill Paul #ifdef DC_USEIOSPACE
32096f2e892SBill Paul #define	DC_RES			SYS_RES_IOPORT
32196f2e892SBill Paul #define	DC_RID			DC_PCI_CFBIO
32296f2e892SBill Paul #else
32396f2e892SBill Paul #define	DC_RES			SYS_RES_MEMORY
32496f2e892SBill Paul #define	DC_RID			DC_PCI_CFBMA
32596f2e892SBill Paul #endif
32696f2e892SBill Paul 
32796f2e892SBill Paul static device_method_t dc_methods[] = {
32896f2e892SBill Paul 	/* Device interface */
32996f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
33096f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
33196f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
332e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
333e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
33496f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
33596f2e892SBill Paul 
33696f2e892SBill Paul 	/* MII interface */
33796f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
33896f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
33996f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
340f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
34196f2e892SBill Paul 
3424b7ec270SMarius Strobl 	DEVMETHOD_END
34396f2e892SBill Paul };
34496f2e892SBill Paul 
34596f2e892SBill Paul static driver_t dc_driver = {
34696f2e892SBill Paul 	"dc",
34796f2e892SBill Paul 	dc_methods,
34896f2e892SBill Paul 	sizeof(struct dc_softc)
34996f2e892SBill Paul };
35096f2e892SBill Paul 
35164450e3aSJohn Baldwin DRIVER_MODULE_ORDERED(dc, pci, dc_driver, NULL, NULL, SI_ORDER_ANY);
3524d0f1c56SWarner Losh MODULE_PNP_INFO("W32:vendor/device;U8:revision;D:#", pci, dc, dc_devs,
353329e817fSWarner Losh     nitems(dc_devs) - 1);
3543e38757dSJohn Baldwin DRIVER_MODULE(miibus, dc, miibus_driver, NULL, NULL);
35596f2e892SBill Paul 
35696f2e892SBill Paul #define	DC_SETBIT(sc, reg, x)				\
35796f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
35896f2e892SBill Paul 
35996f2e892SBill Paul #define	DC_CLRBIT(sc, reg, x)				\
36096f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
36196f2e892SBill Paul 
36296f2e892SBill Paul #define	SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
36396f2e892SBill Paul #define	SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
36496f2e892SBill Paul 
365e3d2833aSAlfred Perlstein static void
dc_delay(struct dc_softc * sc)3660934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
36796f2e892SBill Paul {
36896f2e892SBill Paul 	int idx;
36996f2e892SBill Paul 
37096f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
37196f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
37296f2e892SBill Paul }
37396f2e892SBill Paul 
3742c876e15SPoul-Henning Kamp static void
dc_eeprom_width(struct dc_softc * sc)3750934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3763097aa70SWarner Losh {
3773097aa70SWarner Losh 	int i;
3783097aa70SWarner Losh 
3793097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3803097aa70SWarner Losh 	dc_eeprom_idle(sc);
3813097aa70SWarner Losh 
3823097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3833097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3843097aa70SWarner Losh 	dc_delay(sc);
3853097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3863097aa70SWarner Losh 	dc_delay(sc);
3873097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3883097aa70SWarner Losh 	dc_delay(sc);
3893097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3903097aa70SWarner Losh 	dc_delay(sc);
3913097aa70SWarner Losh 
3923097aa70SWarner Losh 	for (i = 3; i--;) {
3933097aa70SWarner Losh 		if (6 & (1 << i))
3943097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3953097aa70SWarner Losh 		else
3963097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3973097aa70SWarner Losh 		dc_delay(sc);
3983097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3993097aa70SWarner Losh 		dc_delay(sc);
4003097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4013097aa70SWarner Losh 		dc_delay(sc);
4023097aa70SWarner Losh 	}
4033097aa70SWarner Losh 
4043097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
4053097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4063097aa70SWarner Losh 		dc_delay(sc);
4073097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
4083097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4093097aa70SWarner Losh 			dc_delay(sc);
4103097aa70SWarner Losh 			break;
4113097aa70SWarner Losh 		}
4123097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4133097aa70SWarner Losh 		dc_delay(sc);
4143097aa70SWarner Losh 	}
4153097aa70SWarner Losh 
4163097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4173097aa70SWarner Losh 	dc_eeprom_idle(sc);
4183097aa70SWarner Losh 
4193097aa70SWarner Losh 	if (i < 4 || i > 12)
4203097aa70SWarner Losh 		sc->dc_romwidth = 6;
4213097aa70SWarner Losh 	else
4223097aa70SWarner Losh 		sc->dc_romwidth = i;
4233097aa70SWarner Losh 
4243097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4253097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4263097aa70SWarner Losh 	dc_delay(sc);
4273097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4283097aa70SWarner Losh 	dc_delay(sc);
4293097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4303097aa70SWarner Losh 	dc_delay(sc);
4313097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4323097aa70SWarner Losh 	dc_delay(sc);
4333097aa70SWarner Losh 
4343097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4353097aa70SWarner Losh 	dc_eeprom_idle(sc);
4363097aa70SWarner Losh }
4373097aa70SWarner Losh 
438e3d2833aSAlfred Perlstein static void
dc_eeprom_idle(struct dc_softc * sc)4390934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
44096f2e892SBill Paul {
4410934f18aSMaxime Henrion 	int i;
44296f2e892SBill Paul 
44396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
44496f2e892SBill Paul 	dc_delay(sc);
44596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
44696f2e892SBill Paul 	dc_delay(sc);
44796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44896f2e892SBill Paul 	dc_delay(sc);
44996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
45096f2e892SBill Paul 	dc_delay(sc);
45196f2e892SBill Paul 
45296f2e892SBill Paul 	for (i = 0; i < 25; i++) {
45396f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45496f2e892SBill Paul 		dc_delay(sc);
45596f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45696f2e892SBill Paul 		dc_delay(sc);
45796f2e892SBill Paul 	}
45896f2e892SBill Paul 
45996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46096f2e892SBill Paul 	dc_delay(sc);
46196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
46296f2e892SBill Paul 	dc_delay(sc);
46396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
46496f2e892SBill Paul }
46596f2e892SBill Paul 
46696f2e892SBill Paul /*
46796f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
46896f2e892SBill Paul  */
469e3d2833aSAlfred Perlstein static void
dc_eeprom_putbyte(struct dc_softc * sc,int addr)4700934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
47196f2e892SBill Paul {
4720934f18aSMaxime Henrion 	int d, i;
47396f2e892SBill Paul 
4743097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4753097aa70SWarner Losh 	for (i = 3; i--; ) {
4763097aa70SWarner Losh 		if (d & (1 << i))
4773097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
47896f2e892SBill Paul 		else
4793097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4803097aa70SWarner Losh 		dc_delay(sc);
4813097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4823097aa70SWarner Losh 		dc_delay(sc);
4833097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4843097aa70SWarner Losh 		dc_delay(sc);
4853097aa70SWarner Losh 	}
48696f2e892SBill Paul 
48796f2e892SBill Paul 	/*
48896f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
48996f2e892SBill Paul 	 */
4903097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4913097aa70SWarner Losh 		if (addr & (1 << i)) {
49296f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
49396f2e892SBill Paul 		} else {
49496f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
49596f2e892SBill Paul 		}
49696f2e892SBill Paul 		dc_delay(sc);
49796f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
49896f2e892SBill Paul 		dc_delay(sc);
49996f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
50096f2e892SBill Paul 		dc_delay(sc);
50196f2e892SBill Paul 	}
50296f2e892SBill Paul }
50396f2e892SBill Paul 
50496f2e892SBill Paul /*
50596f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
50696f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
50796f2e892SBill Paul  * the EEPROM.
50896f2e892SBill Paul  */
509e3d2833aSAlfred Perlstein static void
dc_eeprom_getword_pnic(struct dc_softc * sc,int addr,uint16_t * dest)510ee320f98SPyun YongHyeon dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, uint16_t *dest)
51196f2e892SBill Paul {
5120934f18aSMaxime Henrion 	int i;
513ee320f98SPyun YongHyeon 	uint32_t r;
51496f2e892SBill Paul 
51596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
51696f2e892SBill Paul 
51796f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
51896f2e892SBill Paul 		DELAY(1);
51996f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
52096f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
521ee320f98SPyun YongHyeon 			*dest = (uint16_t)(r & 0xFFFF);
52296f2e892SBill Paul 			return;
52396f2e892SBill Paul 		}
52496f2e892SBill Paul 	}
52596f2e892SBill Paul }
52696f2e892SBill Paul 
52796f2e892SBill Paul /*
52896f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
529feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
530feb78939SJonathan Chen  * the EEPROM, too.
531feb78939SJonathan Chen  */
532e3d2833aSAlfred Perlstein static void
dc_eeprom_getword_xircom(struct dc_softc * sc,int addr,uint16_t * dest)533ee320f98SPyun YongHyeon dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, uint16_t *dest)
534feb78939SJonathan Chen {
5350934f18aSMaxime Henrion 
536feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
537feb78939SJonathan Chen 
538feb78939SJonathan Chen 	addr *= 2;
539feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
540ee320f98SPyun YongHyeon 	*dest = (uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
541feb78939SJonathan Chen 	addr += 1;
542feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
543ee320f98SPyun YongHyeon 	*dest |= ((uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
544feb78939SJonathan Chen 
545feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
546feb78939SJonathan Chen }
547feb78939SJonathan Chen 
548feb78939SJonathan Chen /*
549feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
55096f2e892SBill Paul  */
551e3d2833aSAlfred Perlstein static void
dc_eeprom_getword(struct dc_softc * sc,int addr,uint16_t * dest)552ee320f98SPyun YongHyeon dc_eeprom_getword(struct dc_softc *sc, int addr, uint16_t *dest)
55396f2e892SBill Paul {
5540934f18aSMaxime Henrion 	int i;
555ee320f98SPyun YongHyeon 	uint16_t word = 0;
55696f2e892SBill Paul 
55796f2e892SBill Paul 	/* Force EEPROM to idle state. */
55896f2e892SBill Paul 	dc_eeprom_idle(sc);
55996f2e892SBill Paul 
56096f2e892SBill Paul 	/* Enter EEPROM access mode. */
56196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
56296f2e892SBill Paul 	dc_delay(sc);
56396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
56496f2e892SBill Paul 	dc_delay(sc);
56596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
56696f2e892SBill Paul 	dc_delay(sc);
56796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
56896f2e892SBill Paul 	dc_delay(sc);
56996f2e892SBill Paul 
57096f2e892SBill Paul 	/*
57196f2e892SBill Paul 	 * Send address of word we want to read.
57296f2e892SBill Paul 	 */
57396f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
57496f2e892SBill Paul 
57596f2e892SBill Paul 	/*
57696f2e892SBill Paul 	 * Start reading bits from EEPROM.
57796f2e892SBill Paul 	 */
57896f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
57996f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
58096f2e892SBill Paul 		dc_delay(sc);
58196f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
58296f2e892SBill Paul 			word |= i;
58396f2e892SBill Paul 		dc_delay(sc);
58496f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
58596f2e892SBill Paul 		dc_delay(sc);
58696f2e892SBill Paul 	}
58796f2e892SBill Paul 
58896f2e892SBill Paul 	/* Turn off EEPROM access mode. */
58996f2e892SBill Paul 	dc_eeprom_idle(sc);
59096f2e892SBill Paul 
59196f2e892SBill Paul 	*dest = word;
59296f2e892SBill Paul }
59396f2e892SBill Paul 
59496f2e892SBill Paul /*
59596f2e892SBill Paul  * Read a sequence of words from the EEPROM.
59696f2e892SBill Paul  */
597e3d2833aSAlfred Perlstein static void
dc_read_eeprom(struct dc_softc * sc,caddr_t dest,int off,int cnt,int be)5988c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
59996f2e892SBill Paul {
60096f2e892SBill Paul 	int i;
601ee320f98SPyun YongHyeon 	uint16_t word = 0, *ptr;
60296f2e892SBill Paul 
60396f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
60496f2e892SBill Paul 		if (DC_IS_PNIC(sc))
60596f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
606feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
607feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
60896f2e892SBill Paul 		else
60996f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
610ee320f98SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
6118c7ff1f3SMaxime Henrion 		if (be)
6128c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
61396f2e892SBill Paul 		else
6148c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
61596f2e892SBill Paul 	}
61696f2e892SBill Paul }
61796f2e892SBill Paul 
61896f2e892SBill Paul /*
6198c1093fcSMarius Strobl  * Write the MII serial port for the MII bit-bang module.
62096f2e892SBill Paul  */
621e3d2833aSAlfred Perlstein static void
dc_mii_bitbang_write(device_t dev,uint32_t val)6228c1093fcSMarius Strobl dc_mii_bitbang_write(device_t dev, uint32_t val)
62396f2e892SBill Paul {
6248c1093fcSMarius Strobl 	struct dc_softc *sc;
6250934f18aSMaxime Henrion 
6268c1093fcSMarius Strobl 	sc = device_get_softc(dev);
62796f2e892SBill Paul 
6288c1093fcSMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, val);
62915578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
63015578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
63196f2e892SBill Paul }
63296f2e892SBill Paul 
63396f2e892SBill Paul /*
6348c1093fcSMarius Strobl  * Read the MII serial port for the MII bit-bang module.
63596f2e892SBill Paul  */
6368c1093fcSMarius Strobl static uint32_t
dc_mii_bitbang_read(device_t dev)6378c1093fcSMarius Strobl dc_mii_bitbang_read(device_t dev)
63896f2e892SBill Paul {
6398c1093fcSMarius Strobl 	struct dc_softc *sc;
6408c1093fcSMarius Strobl 	uint32_t val;
6410934f18aSMaxime Henrion 
6428c1093fcSMarius Strobl 	sc = device_get_softc(dev);
6438c1093fcSMarius Strobl 
6448c1093fcSMarius Strobl 	val = CSR_READ_4(sc, DC_SIO);
64515578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
64615578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
64796f2e892SBill Paul 
6488c1093fcSMarius Strobl 	return (val);
64996f2e892SBill Paul }
65096f2e892SBill Paul 
651e3d2833aSAlfred Perlstein static int
dc_miibus_readreg(device_t dev,int phy,int reg)6520934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
65396f2e892SBill Paul {
65496f2e892SBill Paul 	struct dc_softc *sc;
655c85c4667SBill Paul 	int i, rval, phy_reg = 0;
65696f2e892SBill Paul 
65796f2e892SBill Paul 	sc = device_get_softc(dev);
65896f2e892SBill Paul 
6595c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
66096f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
66196f2e892SBill Paul 			switch (reg) {
66296f2e892SBill Paul 			case MII_BMSR:
66396f2e892SBill Paul 			/*
66496f2e892SBill Paul 			 * Fake something to make the probe
66596f2e892SBill Paul 			 * code think there's a PHY here.
66696f2e892SBill Paul 			 */
66796f2e892SBill Paul 				return (BMSR_MEDIAMASK);
66896f2e892SBill Paul 			case MII_PHYIDR1:
66996f2e892SBill Paul 				if (DC_IS_PNIC(sc))
67096f2e892SBill Paul 					return (DC_VENDORID_LO);
67196f2e892SBill Paul 				return (DC_VENDORID_DEC);
67296f2e892SBill Paul 			case MII_PHYIDR2:
67396f2e892SBill Paul 				if (DC_IS_PNIC(sc))
67496f2e892SBill Paul 					return (DC_DEVICEID_82C168);
67596f2e892SBill Paul 				return (DC_DEVICEID_21143);
67696f2e892SBill Paul 			default:
67796f2e892SBill Paul 				return (0);
67896f2e892SBill Paul 			}
67996f2e892SBill Paul 		} else
68096f2e892SBill Paul 			return (0);
68196f2e892SBill Paul 	}
68296f2e892SBill Paul 
68396f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
68496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
68596f2e892SBill Paul 		    (phy << 23) | (reg << 18));
68696f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
68796f2e892SBill Paul 			DELAY(1);
68896f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
68996f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
69096f2e892SBill Paul 				rval &= 0xFFFF;
69196f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
69296f2e892SBill Paul 			}
69396f2e892SBill Paul 		}
69496f2e892SBill Paul 		return (0);
69596f2e892SBill Paul 	}
69696f2e892SBill Paul 
69752ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
69852ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
69952ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
70052ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
70152ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_READ);
70252ca7ee2SPyun YongHyeon 		for (i = 0; i < DC_TIMEOUT; i++) {
70352ca7ee2SPyun YongHyeon 			DELAY(1);
70452ca7ee2SPyun YongHyeon 			rval = CSR_READ_4(sc, DC_ROM);
70552ca7ee2SPyun YongHyeon 			if ((rval & DC_ULI_PHY_OP_DONE) != 0) {
70652ca7ee2SPyun YongHyeon 				return (rval & DC_ULI_PHY_DATA_MASK);
70752ca7ee2SPyun YongHyeon 			}
70852ca7ee2SPyun YongHyeon 		}
70952ca7ee2SPyun YongHyeon 		if (i == DC_TIMEOUT)
71052ca7ee2SPyun YongHyeon 			device_printf(dev, "phy read timed out\n");
71152ca7ee2SPyun YongHyeon 		return (0);
71252ca7ee2SPyun YongHyeon 	}
71352ca7ee2SPyun YongHyeon 
71496f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
71596f2e892SBill Paul 		switch (reg) {
71696f2e892SBill Paul 		case MII_BMCR:
71796f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
71896f2e892SBill Paul 			break;
71996f2e892SBill Paul 		case MII_BMSR:
72096f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
72196f2e892SBill Paul 			break;
72296f2e892SBill Paul 		case MII_PHYIDR1:
72396f2e892SBill Paul 			phy_reg = DC_AL_VENID;
72496f2e892SBill Paul 			break;
72596f2e892SBill Paul 		case MII_PHYIDR2:
72696f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
72796f2e892SBill Paul 			break;
72896f2e892SBill Paul 		case MII_ANAR:
72996f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
73096f2e892SBill Paul 			break;
73196f2e892SBill Paul 		case MII_ANLPAR:
73296f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
73396f2e892SBill Paul 			break;
73496f2e892SBill Paul 		case MII_ANER:
73596f2e892SBill Paul 			phy_reg = DC_AL_ANER;
73696f2e892SBill Paul 			break;
73796f2e892SBill Paul 		default:
73822f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
73922f6205dSJohn Baldwin 			    reg);
74096f2e892SBill Paul 			return (0);
74196f2e892SBill Paul 		}
74296f2e892SBill Paul 
74396f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
74496f2e892SBill Paul 		if (rval == 0xFFFF)
74596f2e892SBill Paul 			return (0);
74696f2e892SBill Paul 		return (rval);
74796f2e892SBill Paul 	}
74896f2e892SBill Paul 
749419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
750f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
751f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
752419146d9SBill Paul 	}
7538c1093fcSMarius Strobl 	rval = mii_bitbang_readreg(dev, &dc_mii_bitbang_ops, phy, reg);
754419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
755f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
75696f2e892SBill Paul 
7578c1093fcSMarius Strobl 	return (rval);
75896f2e892SBill Paul }
75996f2e892SBill Paul 
760e3d2833aSAlfred Perlstein static int
dc_miibus_writereg(device_t dev,int phy,int reg,int data)7610934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
76296f2e892SBill Paul {
76396f2e892SBill Paul 	struct dc_softc *sc;
764c85c4667SBill Paul 	int i, phy_reg = 0;
76596f2e892SBill Paul 
76696f2e892SBill Paul 	sc = device_get_softc(dev);
76796f2e892SBill Paul 
76896f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
76996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
77096f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
77196f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
77296f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
77396f2e892SBill Paul 				break;
77496f2e892SBill Paul 		}
77596f2e892SBill Paul 		return (0);
77696f2e892SBill Paul 	}
77796f2e892SBill Paul 
77852ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
77952ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
78052ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
78152ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
78252ca7ee2SPyun YongHyeon 		    ((data << DC_ULI_PHY_DATA_SHIFT) & DC_ULI_PHY_DATA_MASK) |
78352ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_WRITE);
78452ca7ee2SPyun YongHyeon 		DELAY(1);
78552ca7ee2SPyun YongHyeon 		return (0);
78652ca7ee2SPyun YongHyeon 	}
78752ca7ee2SPyun YongHyeon 
78896f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
78996f2e892SBill Paul 		switch (reg) {
79096f2e892SBill Paul 		case MII_BMCR:
79196f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
79296f2e892SBill Paul 			break;
79396f2e892SBill Paul 		case MII_BMSR:
79496f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
79596f2e892SBill Paul 			break;
79696f2e892SBill Paul 		case MII_PHYIDR1:
79796f2e892SBill Paul 			phy_reg = DC_AL_VENID;
79896f2e892SBill Paul 			break;
79996f2e892SBill Paul 		case MII_PHYIDR2:
80096f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
80196f2e892SBill Paul 			break;
80296f2e892SBill Paul 		case MII_ANAR:
80396f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
80496f2e892SBill Paul 			break;
80596f2e892SBill Paul 		case MII_ANLPAR:
80696f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
80796f2e892SBill Paul 			break;
80896f2e892SBill Paul 		case MII_ANER:
80996f2e892SBill Paul 			phy_reg = DC_AL_ANER;
81096f2e892SBill Paul 			break;
81196f2e892SBill Paul 		default:
81222f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
81322f6205dSJohn Baldwin 			    reg);
81496f2e892SBill Paul 			return (0);
81596f2e892SBill Paul 			break;
81696f2e892SBill Paul 		}
81796f2e892SBill Paul 
81896f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
81996f2e892SBill Paul 		return (0);
82096f2e892SBill Paul 	}
82196f2e892SBill Paul 
822419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
823f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
824f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
825419146d9SBill Paul 	}
8268c1093fcSMarius Strobl 	mii_bitbang_writereg(dev, &dc_mii_bitbang_ops, phy, reg, data);
827419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
828f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
82996f2e892SBill Paul 
83096f2e892SBill Paul 	return (0);
83196f2e892SBill Paul }
83296f2e892SBill Paul 
833e3d2833aSAlfred Perlstein static void
dc_miibus_statchg(device_t dev)8340934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
83596f2e892SBill Paul {
83696f2e892SBill Paul 	struct dc_softc *sc;
837*91f3f3fcSJustin Hibbits 	if_t ifp;
83896f2e892SBill Paul 	struct mii_data *mii;
839f43d9309SBill Paul 	struct ifmedia *ifm;
84096f2e892SBill Paul 
84196f2e892SBill Paul 	sc = device_get_softc(dev);
8425c1cfac4SBill Paul 
84396f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
844d314ebf5SPyun YongHyeon 	ifp = sc->dc_ifp;
845d314ebf5SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
846*91f3f3fcSJustin Hibbits 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
847d314ebf5SPyun YongHyeon 		return;
848d314ebf5SPyun YongHyeon 
849f43d9309SBill Paul 	ifm = &mii->mii_media;
85089b2411bSPyun YongHyeon 	if (DC_IS_DAVICOM(sc) && IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
851f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
852d314ebf5SPyun YongHyeon 		return;
85389b2411bSPyun YongHyeon 	} else if (!DC_IS_ADMTEK(sc))
85489b2411bSPyun YongHyeon 		dc_setcfg(sc, mii->mii_media_active);
855d314ebf5SPyun YongHyeon 
856d314ebf5SPyun YongHyeon 	sc->dc_link = 0;
857d314ebf5SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
858d314ebf5SPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
859d314ebf5SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
860d314ebf5SPyun YongHyeon 		case IFM_10_T:
861d314ebf5SPyun YongHyeon 		case IFM_100_TX:
862d314ebf5SPyun YongHyeon 			sc->dc_link = 1;
863d314ebf5SPyun YongHyeon 			break;
864d314ebf5SPyun YongHyeon 		}
865d314ebf5SPyun YongHyeon 	}
866f43d9309SBill Paul }
867f43d9309SBill Paul 
868f43d9309SBill Paul /*
869f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
870f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
871f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
872f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
873f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
874f43d9309SBill Paul  * with it itself. *sigh*
875f43d9309SBill Paul  */
876e3d2833aSAlfred Perlstein static void
dc_miibus_mediainit(device_t dev)8770934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
878f43d9309SBill Paul {
879f43d9309SBill Paul 	struct dc_softc *sc;
880f43d9309SBill Paul 	struct mii_data *mii;
881f43d9309SBill Paul 	struct ifmedia *ifm;
882f43d9309SBill Paul 	int rev;
883f43d9309SBill Paul 
8841e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
885f43d9309SBill Paul 
886f43d9309SBill Paul 	sc = device_get_softc(dev);
887f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
888f43d9309SBill Paul 	ifm = &mii->mii_media;
889f43d9309SBill Paul 
890f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
89145521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
89296f2e892SBill Paul }
89396f2e892SBill Paul 
89479d11e09SBill Paul #define	DC_BITS_512	9
89579d11e09SBill Paul #define	DC_BITS_128	7
89679d11e09SBill Paul #define	DC_BITS_64	6
89796f2e892SBill Paul 
8983373489bSWarner Losh static uint32_t
dc_mchash_le(struct dc_softc * sc,const uint8_t * addr)8993373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
90096f2e892SBill Paul {
9013373489bSWarner Losh 	uint32_t crc;
90296f2e892SBill Paul 
90396f2e892SBill Paul 	/* Compute CRC for the address value. */
9040e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
90596f2e892SBill Paul 
90679d11e09SBill Paul 	/*
90779d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
90879d11e09SBill Paul 	 * chips is only 128 bits wide.
90979d11e09SBill Paul 	 */
91079d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
91179d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
91296f2e892SBill Paul 
91379d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
91479d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
91579d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
91679d11e09SBill Paul 
917feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
918feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
919feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
920feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
9210934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
922feb78939SJonathan Chen 		else
9230934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
9240934f18aSMaxime Henrion 			    (12 << 4));
925feb78939SJonathan Chen 	}
926feb78939SJonathan Chen 
92779d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
92896f2e892SBill Paul }
92996f2e892SBill Paul 
93096f2e892SBill Paul /*
93196f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
93296f2e892SBill Paul  */
9333373489bSWarner Losh static uint32_t
dc_mchash_be(const uint8_t * addr)9343373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
93596f2e892SBill Paul {
9360e939c0cSChristian Weisgerber 	uint32_t crc;
93796f2e892SBill Paul 
93896f2e892SBill Paul 	/* Compute CRC for the address value. */
9390e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
94096f2e892SBill Paul 
9410934f18aSMaxime Henrion 	/* Return the filter bit position. */
94296f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
94396f2e892SBill Paul }
94496f2e892SBill Paul 
94596f2e892SBill Paul /*
94696f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
94796f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
94896f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
94996f2e892SBill Paul  *
95096f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
95196f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
95296f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
95396f2e892SBill Paul  * we need that too.
95496f2e892SBill Paul  */
95551e80820SGleb Smirnoff static u_int
dc_hash_maddr_21143(void * arg,struct sockaddr_dl * sdl,u_int cnt)95651e80820SGleb Smirnoff dc_hash_maddr_21143(void *arg, struct sockaddr_dl *sdl, u_int cnt)
95751e80820SGleb Smirnoff {
95851e80820SGleb Smirnoff 	struct dc_softc *sc = arg;
95951e80820SGleb Smirnoff 	uint32_t h;
96051e80820SGleb Smirnoff 
96151e80820SGleb Smirnoff 	h = dc_mchash_le(sc, LLADDR(sdl));
96251e80820SGleb Smirnoff 	sc->dc_cdata.dc_sbuf[h >> 4] |= htole32(1 << (h & 0xF));
96351e80820SGleb Smirnoff 
96451e80820SGleb Smirnoff 	return (1);
96551e80820SGleb Smirnoff }
96651e80820SGleb Smirnoff 
9672c876e15SPoul-Henning Kamp static void
dc_setfilt_21143(struct dc_softc * sc)9680934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
96996f2e892SBill Paul {
9708df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
97196f2e892SBill Paul 	struct dc_desc *sframe;
972ee320f98SPyun YongHyeon 	uint32_t h, *sp;
973*91f3f3fcSJustin Hibbits 	if_t ifp;
97496f2e892SBill Paul 	int i;
97596f2e892SBill Paul 
976fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
97796f2e892SBill Paul 
97896f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
97996f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
98096f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
9815f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
98256e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
9830934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
98496f2e892SBill Paul 
9855f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
986af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
987af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
98896f2e892SBill Paul 
98956e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
99096f2e892SBill Paul 
99196f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
992*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC)
99396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99496f2e892SBill Paul 	else
99596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99696f2e892SBill Paul 
997*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_ALLMULTI)
99896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
99996f2e892SBill Paul 	else
100096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100196f2e892SBill Paul 
100251e80820SGleb Smirnoff 	if_foreach_llmaddr(ifp, dc_hash_maddr_21143, sp);
100396f2e892SBill Paul 
1004*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_BROADCAST) {
1005*91f3f3fcSJustin Hibbits 		h = dc_mchash_le(sc, if_getbroadcastaddr(ifp));
1006af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
100796f2e892SBill Paul 	}
100896f2e892SBill Paul 
10098df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
1010*91f3f3fcSJustin Hibbits 	bcopy(if_getlladdr(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10118df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
10128df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
10138df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
101496f2e892SBill Paul 
1015af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
10168c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
10178c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
10185f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
101996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
102096f2e892SBill Paul 
102196f2e892SBill Paul 	/*
102296f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
102396f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
102496f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
102596f2e892SBill Paul 	 * medicine.
102696f2e892SBill Paul 	 */
102796f2e892SBill Paul 	DELAY(10000);
102896f2e892SBill Paul 
1029b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
103096f2e892SBill Paul }
103196f2e892SBill Paul 
103251e80820SGleb Smirnoff static u_int
dc_hash_maddr_admtek_be(void * arg,struct sockaddr_dl * sdl,u_int cnt)103351e80820SGleb Smirnoff dc_hash_maddr_admtek_be(void *arg, struct sockaddr_dl *sdl, u_int cnt)
103451e80820SGleb Smirnoff {
103551e80820SGleb Smirnoff 	uint32_t *hashes = arg;
103651e80820SGleb Smirnoff 	int h = 0;
103751e80820SGleb Smirnoff 
103851e80820SGleb Smirnoff 	h = dc_mchash_be(LLADDR(sdl));
103951e80820SGleb Smirnoff 	if (h < 32)
104051e80820SGleb Smirnoff 		hashes[0] |= (1 << h);
104151e80820SGleb Smirnoff 	else
104251e80820SGleb Smirnoff 		hashes[1] |= (1 << (h - 32));
104351e80820SGleb Smirnoff 
104451e80820SGleb Smirnoff 	return (1);
104551e80820SGleb Smirnoff }
104651e80820SGleb Smirnoff 
104751e80820SGleb Smirnoff struct dc_hash_maddr_admtek_le_ctx {
104851e80820SGleb Smirnoff 	struct dc_softc *sc;
104951e80820SGleb Smirnoff 	uint32_t hashes[2];
105051e80820SGleb Smirnoff };
105151e80820SGleb Smirnoff 
105251e80820SGleb Smirnoff static u_int
dc_hash_maddr_admtek_le(void * arg,struct sockaddr_dl * sdl,u_int cnt)105351e80820SGleb Smirnoff dc_hash_maddr_admtek_le(void *arg, struct sockaddr_dl *sdl, u_int cnt)
105451e80820SGleb Smirnoff {
105551e80820SGleb Smirnoff 	struct dc_hash_maddr_admtek_le_ctx *ctx = arg;
105651e80820SGleb Smirnoff 	int h = 0;
105751e80820SGleb Smirnoff 
105851e80820SGleb Smirnoff 	h = dc_mchash_le(ctx->sc, LLADDR(sdl));
105951e80820SGleb Smirnoff 	if (h < 32)
106051e80820SGleb Smirnoff 		ctx->hashes[0] |= (1 << h);
106151e80820SGleb Smirnoff 	else
106251e80820SGleb Smirnoff 		ctx->hashes[1] |= (1 << (h - 32));
106351e80820SGleb Smirnoff 
106451e80820SGleb Smirnoff 	return (1);
106551e80820SGleb Smirnoff }
106651e80820SGleb Smirnoff 
10672c876e15SPoul-Henning Kamp static void
dc_setfilt_admtek(struct dc_softc * sc)10680934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
106996f2e892SBill Paul {
10702e3d4b79SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
1071*91f3f3fcSJustin Hibbits 	if_t ifp;
107251e80820SGleb Smirnoff 	struct dc_hash_maddr_admtek_le_ctx ctx = { sc, { 0, 0 }};
107396f2e892SBill Paul 
1074fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
107596f2e892SBill Paul 
10760934f18aSMaxime Henrion 	/* Init our MAC address. */
1077*91f3f3fcSJustin Hibbits 	bcopy(if_getlladdr(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10782e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
10792e3d4b79SPyun YongHyeon 	    eaddr[1] << 8 | eaddr[0]);
10802e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
108196f2e892SBill Paul 
108296f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
1083*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC)
108496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
108596f2e892SBill Paul 	else
108696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
108796f2e892SBill Paul 
1088*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_ALLMULTI)
108996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
109096f2e892SBill Paul 	else
109196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
109296f2e892SBill Paul 
10930934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
109496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
109596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
109696f2e892SBill Paul 
109796f2e892SBill Paul 	/*
109896f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
109996f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
110096f2e892SBill Paul 	 */
1101*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & (IFF_PROMISC | IFF_ALLMULTI))
110296f2e892SBill Paul 		return;
110396f2e892SBill Paul 
11040934f18aSMaxime Henrion 	/* Now program new ones. */
1105acc1bcccSMartin Blapp 	if (DC_IS_CENTAUR(sc))
110651e80820SGleb Smirnoff 		if_foreach_llmaddr(ifp, dc_hash_maddr_admtek_le, &ctx);
1107acc1bcccSMartin Blapp 	else
110851e80820SGleb Smirnoff 		if_foreach_llmaddr(ifp, dc_hash_maddr_admtek_be, &ctx.hashes);
110996f2e892SBill Paul 
111051e80820SGleb Smirnoff 	CSR_WRITE_4(sc, DC_AL_MAR0, ctx.hashes[0]);
111151e80820SGleb Smirnoff 	CSR_WRITE_4(sc, DC_AL_MAR1, ctx.hashes[1]);
111296f2e892SBill Paul }
111396f2e892SBill Paul 
11142c876e15SPoul-Henning Kamp static void
dc_setfilt_asix(struct dc_softc * sc)11150934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
111696f2e892SBill Paul {
11178df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
1118*91f3f3fcSJustin Hibbits 	if_t ifp;
1119ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
112096f2e892SBill Paul 
1121fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
112296f2e892SBill Paul 
11238df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
1124*91f3f3fcSJustin Hibbits 	bcopy(if_getlladdr(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
112596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
11268df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
112796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
11288df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
112996f2e892SBill Paul 
113096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
1131*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC)
113296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
113396f2e892SBill Paul 	else
113496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
113596f2e892SBill Paul 
1136*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_ALLMULTI)
113796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
113896f2e892SBill Paul 	else
113996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
114096f2e892SBill Paul 
114196f2e892SBill Paul 	/*
114296f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
114396f2e892SBill Paul 	 * of broadcast frames.
114496f2e892SBill Paul 	 */
1145*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_BROADCAST)
114696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
114796f2e892SBill Paul 	else
114896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
114996f2e892SBill Paul 
115096f2e892SBill Paul 	/* first, zot all the existing hash bits */
115196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
115296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
115396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
115496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
115596f2e892SBill Paul 
115696f2e892SBill Paul 	/*
115796f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
115896f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
115996f2e892SBill Paul 	 */
1160*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & (IFF_PROMISC | IFF_ALLMULTI))
116196f2e892SBill Paul 		return;
116296f2e892SBill Paul 
116396f2e892SBill Paul 	/* now program new ones */
116451e80820SGleb Smirnoff 	if_foreach_llmaddr(ifp, dc_hash_maddr_admtek_be, hashes);
116596f2e892SBill Paul 
116696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
116796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
116896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
116996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
117096f2e892SBill Paul }
117196f2e892SBill Paul 
117251e80820SGleb Smirnoff static u_int
dc_hash_maddr_uli(void * arg,struct sockaddr_dl * sdl,u_int mcnt)117351e80820SGleb Smirnoff dc_hash_maddr_uli(void *arg, struct sockaddr_dl *sdl, u_int mcnt)
117451e80820SGleb Smirnoff {
117551e80820SGleb Smirnoff 	uint32_t **sp = arg;
117651e80820SGleb Smirnoff 	uint8_t *ma;
117751e80820SGleb Smirnoff 
117851e80820SGleb Smirnoff 	if (mcnt == DC_ULI_FILTER_NPERF)
117951e80820SGleb Smirnoff 		return (0);
118051e80820SGleb Smirnoff 	ma = LLADDR(sdl);
118151e80820SGleb Smirnoff 	*(*sp)++ = DC_SP_MAC(ma[1] << 8 | ma[0]);
118251e80820SGleb Smirnoff 	*(*sp)++ = DC_SP_MAC(ma[3] << 8 | ma[2]);
118351e80820SGleb Smirnoff 	*(*sp)++ = DC_SP_MAC(ma[5] << 8 | ma[4]);
118451e80820SGleb Smirnoff 
118551e80820SGleb Smirnoff 	return (1);
118651e80820SGleb Smirnoff }
118751e80820SGleb Smirnoff 
11882c876e15SPoul-Henning Kamp static void
dc_setfilt_uli(struct dc_softc * sc)118952ca7ee2SPyun YongHyeon dc_setfilt_uli(struct dc_softc *sc)
119052ca7ee2SPyun YongHyeon {
119152ca7ee2SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
1192*91f3f3fcSJustin Hibbits 	if_t ifp;
119352ca7ee2SPyun YongHyeon 	struct dc_desc *sframe;
119452ca7ee2SPyun YongHyeon 	uint32_t filter, *sp;
119552ca7ee2SPyun YongHyeon 	int i, mcnt;
119652ca7ee2SPyun YongHyeon 
119752ca7ee2SPyun YongHyeon 	ifp = sc->dc_ifp;
119852ca7ee2SPyun YongHyeon 
119952ca7ee2SPyun YongHyeon 	i = sc->dc_cdata.dc_tx_prod;
120052ca7ee2SPyun YongHyeon 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
120152ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_cnt++;
120252ca7ee2SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
120352ca7ee2SPyun YongHyeon 	sp = sc->dc_cdata.dc_sbuf;
120452ca7ee2SPyun YongHyeon 	bzero(sp, DC_SFRAME_LEN);
120552ca7ee2SPyun YongHyeon 
120652ca7ee2SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
120752ca7ee2SPyun YongHyeon 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
120852ca7ee2SPyun YongHyeon 	    DC_TXCTL_TLINK | DC_FILTER_PERFECT | DC_TXCTL_FINT);
120952ca7ee2SPyun YongHyeon 
121052ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
121152ca7ee2SPyun YongHyeon 
121252ca7ee2SPyun YongHyeon 	/* Set station address. */
1213*91f3f3fcSJustin Hibbits 	bcopy(if_getlladdr(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
121452ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[1] << 8 | eaddr[0]);
121552ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[3] << 8 | eaddr[2]);
121652ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[5] << 8 | eaddr[4]);
121752ca7ee2SPyun YongHyeon 
121852ca7ee2SPyun YongHyeon 	/* Set broadcast address. */
121952ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
122052ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
122152ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
122252ca7ee2SPyun YongHyeon 
122352ca7ee2SPyun YongHyeon 	/* Extract current filter configuration. */
122452ca7ee2SPyun YongHyeon 	filter = CSR_READ_4(sc, DC_NETCFG);
122552ca7ee2SPyun YongHyeon 	filter &= ~(DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI);
122652ca7ee2SPyun YongHyeon 
122752ca7ee2SPyun YongHyeon 	/* Now build perfect filters. */
122851e80820SGleb Smirnoff 	mcnt = if_foreach_llmaddr(ifp, dc_hash_maddr_uli, &sp);
122952ca7ee2SPyun YongHyeon 
123051e80820SGleb Smirnoff 	if (mcnt == DC_ULI_FILTER_NPERF)
123151e80820SGleb Smirnoff 		filter |= DC_NETCFG_RX_ALLMULTI;
123251e80820SGleb Smirnoff 	else
123352ca7ee2SPyun YongHyeon 		for (; mcnt < DC_ULI_FILTER_NPERF; mcnt++) {
123452ca7ee2SPyun YongHyeon 			*sp++ = DC_SP_MAC(0xFFFF);
123552ca7ee2SPyun YongHyeon 			*sp++ = DC_SP_MAC(0xFFFF);
123652ca7ee2SPyun YongHyeon 			*sp++ = DC_SP_MAC(0xFFFF);
123752ca7ee2SPyun YongHyeon 		}
123852ca7ee2SPyun YongHyeon 
123952ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
124052ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
124152ca7ee2SPyun YongHyeon 		    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1242*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC)
124352ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI;
1244*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_ALLMULTI)
124552ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_ALLMULTI;
124652ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_NETCFG,
124752ca7ee2SPyun YongHyeon 	    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
124852ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
124952ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, filter);
125052ca7ee2SPyun YongHyeon 
125152ca7ee2SPyun YongHyeon 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
125252ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
125352ca7ee2SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
125452ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
125552ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
125652ca7ee2SPyun YongHyeon 
125752ca7ee2SPyun YongHyeon 	/*
125852ca7ee2SPyun YongHyeon 	 * Wait some time...
125952ca7ee2SPyun YongHyeon 	 */
126052ca7ee2SPyun YongHyeon 	DELAY(1000);
126152ca7ee2SPyun YongHyeon 
126252ca7ee2SPyun YongHyeon 	sc->dc_wdog_timer = 5;
126352ca7ee2SPyun YongHyeon }
126452ca7ee2SPyun YongHyeon 
126551e80820SGleb Smirnoff static u_int
dc_hash_maddr_xircom(void * arg,struct sockaddr_dl * sdl,u_int cnt)126651e80820SGleb Smirnoff dc_hash_maddr_xircom(void *arg, struct sockaddr_dl *sdl, u_int cnt)
126751e80820SGleb Smirnoff {
126851e80820SGleb Smirnoff 	struct dc_softc *sc = arg;
126951e80820SGleb Smirnoff 	uint32_t h;
127051e80820SGleb Smirnoff 
127151e80820SGleb Smirnoff 	h = dc_mchash_le(sc, LLADDR(sdl));
127251e80820SGleb Smirnoff 	sc->dc_cdata.dc_sbuf[h >> 4] |= htole32(1 << (h & 0xF));
127351e80820SGleb Smirnoff 	return (1);
127451e80820SGleb Smirnoff }
127551e80820SGleb Smirnoff 
127652ca7ee2SPyun YongHyeon static void
dc_setfilt_xircom(struct dc_softc * sc)12770934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1278feb78939SJonathan Chen {
12798df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
1280*91f3f3fcSJustin Hibbits 	if_t ifp;
1281feb78939SJonathan Chen 	struct dc_desc *sframe;
1282ee320f98SPyun YongHyeon 	uint32_t h, *sp;
1283feb78939SJonathan Chen 	int i;
1284feb78939SJonathan Chen 
1285fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1286feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1287feb78939SJonathan Chen 
1288feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1289feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1290feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
12915f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
129256e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
12930934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1294feb78939SJonathan Chen 
12955f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1296af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1297af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1298feb78939SJonathan Chen 
129956e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1300feb78939SJonathan Chen 
1301feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1302*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_PROMISC)
1303feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1304feb78939SJonathan Chen 	else
1305feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1306feb78939SJonathan Chen 
1307*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_ALLMULTI)
1308feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1309feb78939SJonathan Chen 	else
1310feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1311feb78939SJonathan Chen 
131251e80820SGleb Smirnoff 	if_foreach_llmaddr(ifp, dc_hash_maddr_xircom, &sp);
1313feb78939SJonathan Chen 
1314*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_BROADCAST) {
1315*91f3f3fcSJustin Hibbits 		h = dc_mchash_le(sc, if_getbroadcastaddr(ifp));
1316af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1317feb78939SJonathan Chen 	}
1318feb78939SJonathan Chen 
13198df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
1320*91f3f3fcSJustin Hibbits 	bcopy(if_getlladdr(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13218df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13228df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13238df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1324feb78939SJonathan Chen 
1325feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1326feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1327af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
13288c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
13298c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
13305f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1331feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1332feb78939SJonathan Chen 
1333feb78939SJonathan Chen 	/*
13340934f18aSMaxime Henrion 	 * Wait some time...
1335feb78939SJonathan Chen 	 */
1336feb78939SJonathan Chen 	DELAY(1000);
1337feb78939SJonathan Chen 
1338b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
1339feb78939SJonathan Chen }
1340feb78939SJonathan Chen 
1341e3d2833aSAlfred Perlstein static void
dc_setfilt(struct dc_softc * sc)13420934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
134396f2e892SBill Paul {
13440934f18aSMaxime Henrion 
134596f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13461af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
134796f2e892SBill Paul 		dc_setfilt_21143(sc);
134896f2e892SBill Paul 
134996f2e892SBill Paul 	if (DC_IS_ASIX(sc))
135096f2e892SBill Paul 		dc_setfilt_asix(sc);
135196f2e892SBill Paul 
135296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
135396f2e892SBill Paul 		dc_setfilt_admtek(sc);
135496f2e892SBill Paul 
135552ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
135652ca7ee2SPyun YongHyeon 		dc_setfilt_uli(sc);
135752ca7ee2SPyun YongHyeon 
1358feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1359feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
136096f2e892SBill Paul }
136196f2e892SBill Paul 
1362e3d2833aSAlfred Perlstein static void
dc_netcfg_wait(struct dc_softc * sc)13631da7683aSPyun YongHyeon dc_netcfg_wait(struct dc_softc *sc)
136496f2e892SBill Paul {
1365ee320f98SPyun YongHyeon 	uint32_t isr;
13661da7683aSPyun YongHyeon 	int i;
136796f2e892SBill Paul 
136896f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
136996f2e892SBill Paul 		isr = CSR_READ_4(sc, DC_ISR);
1370d467c136SBill Paul 		if (isr & DC_ISR_TX_IDLE &&
1371351267c1SMartin Blapp 		    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1372351267c1SMartin Blapp 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
137396f2e892SBill Paul 			break;
1374d467c136SBill Paul 		DELAY(10);
137596f2e892SBill Paul 	}
13767a6fab66SWarner Losh 	if (i == DC_TIMEOUT && bus_child_present(sc->dc_dev)) {
1377432120f2SMarius Strobl 		if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13786b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
13791da7683aSPyun YongHyeon 			    "%s: failed to force tx to idle state\n", __func__);
1380432120f2SMarius Strobl 		if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1381432120f2SMarius Strobl 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1382d0d67284SMarius Strobl 		    !DC_HAS_BROKEN_RXSTATE(sc))
1383432120f2SMarius Strobl 			device_printf(sc->dc_dev,
13841da7683aSPyun YongHyeon 			    "%s: failed to force rx to idle state\n", __func__);
1385432120f2SMarius Strobl 	}
138696f2e892SBill Paul }
138796f2e892SBill Paul 
13881da7683aSPyun YongHyeon /*
13891da7683aSPyun YongHyeon  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13901da7683aSPyun YongHyeon  * the netconfig register, we first have to put the transmit and/or
13911da7683aSPyun YongHyeon  * receive logic in the idle state.
13921da7683aSPyun YongHyeon  */
13931da7683aSPyun YongHyeon static void
dc_setcfg(struct dc_softc * sc,int media)13941da7683aSPyun YongHyeon dc_setcfg(struct dc_softc *sc, int media)
13951da7683aSPyun YongHyeon {
13961da7683aSPyun YongHyeon 	int restart = 0, watchdogreg;
13971da7683aSPyun YongHyeon 
13981da7683aSPyun YongHyeon 	if (IFM_SUBTYPE(media) == IFM_NONE)
13991da7683aSPyun YongHyeon 		return;
14001da7683aSPyun YongHyeon 
14011da7683aSPyun YongHyeon 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
14021da7683aSPyun YongHyeon 		restart = 1;
14031da7683aSPyun YongHyeon 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
14041da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
14051da7683aSPyun YongHyeon 	}
14061da7683aSPyun YongHyeon 
140796f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1408042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1409042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
141096f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1411bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14120934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14138273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14148273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14158273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14164c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1417bf645417SBill Paul 			} else {
1418bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1419bf645417SBill Paul 			}
142096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
142196f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
142296f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
142396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
142496f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
142588d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
142696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
142796f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
142896f2e892SBill Paul 		} else {
142996f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
143096f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
143196f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
143296f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
143396f2e892SBill Paul 			}
1434318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1435318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1436318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
143796f2e892SBill Paul 		}
143896f2e892SBill Paul 	}
143996f2e892SBill Paul 
144096f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1441042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1442042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
144396f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14440934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14454c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14468273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14478273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14488273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14498273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14504c2efe27SBill Paul 			} else {
14514c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14524c2efe27SBill Paul 			}
145396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
145496f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
145596f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
145696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
145788d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
145896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
145996f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
146096f2e892SBill Paul 		} else {
146196f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
146296f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
146396f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
146496f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
146596f2e892SBill Paul 			}
146696f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1467318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14695c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14705c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14715c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14725c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14735c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14745c1cfac4SBill Paul 				else
14755c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14765c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14775c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14785c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14795c1cfac4SBill Paul 				DELAY(20000);
14805c1cfac4SBill Paul 			}
148196f2e892SBill Paul 		}
148296f2e892SBill Paul 	}
148396f2e892SBill Paul 
1484f43d9309SBill Paul 	/*
1485f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1486f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1487f43d9309SBill Paul 	 * on the external MII port.
1488f43d9309SBill Paul 	 */
1489f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
149045521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1491f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1492f43d9309SBill Paul 			sc->dc_link = 1;
1493f43d9309SBill Paul 		} else {
1494f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1495f43d9309SBill Paul 		}
1496f43d9309SBill Paul 	}
1497f43d9309SBill Paul 
149896f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
149996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
150096f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
150196f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
150296f2e892SBill Paul 	} else {
150396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
150496f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
150596f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
150696f2e892SBill Paul 	}
150796f2e892SBill Paul 
150896f2e892SBill Paul 	if (restart)
150996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
151096f2e892SBill Paul }
151196f2e892SBill Paul 
1512e3d2833aSAlfred Perlstein static void
dc_reset(struct dc_softc * sc)15130934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
151496f2e892SBill Paul {
15150934f18aSMaxime Henrion 	int i;
151696f2e892SBill Paul 
151796f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
151896f2e892SBill Paul 
151996f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
152096f2e892SBill Paul 		DELAY(10);
152196f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
152296f2e892SBill Paul 			break;
152396f2e892SBill Paul 	}
152496f2e892SBill Paul 
15251af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
152652ca7ee2SPyun YongHyeon 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc) || DC_IS_ULI(sc)) {
152796f2e892SBill Paul 		DELAY(10000);
152896f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152996f2e892SBill Paul 		i = 0;
153096f2e892SBill Paul 	}
153196f2e892SBill Paul 
153296f2e892SBill Paul 	if (i == DC_TIMEOUT)
15336b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev, "reset never completed!\n");
153496f2e892SBill Paul 
153596f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
153696f2e892SBill Paul 	DELAY(1000);
153796f2e892SBill Paul 
153896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
153996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
154096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
154196f2e892SBill Paul 
154291cc2adbSBill Paul 	/*
154391cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
154491cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
154591cc2adbSBill Paul 	 * into a state where it will never come out of reset
154691cc2adbSBill Paul 	 * until we reset the whole chip again.
154791cc2adbSBill Paul 	 */
15485c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
154991cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1550d314ebf5SPyun YongHyeon 		CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFFFFFF);
15515c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15525c1cfac4SBill Paul 	}
155396f2e892SBill Paul }
155496f2e892SBill Paul 
1555ebc284ccSMarius Strobl static const struct dc_type *
dc_devtype(device_t dev)15560934f18aSMaxime Henrion dc_devtype(device_t dev)
155796f2e892SBill Paul {
1558ebc284ccSMarius Strobl 	const struct dc_type *t;
1559ee320f98SPyun YongHyeon 	uint32_t devid;
1560ee320f98SPyun YongHyeon 	uint8_t rev;
156196f2e892SBill Paul 
156296f2e892SBill Paul 	t = dc_devs;
15631e2e70b1SJohn Baldwin 	devid = pci_get_devid(dev);
15641e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
156596f2e892SBill Paul 
156696f2e892SBill Paul 	while (t->dc_name != NULL) {
15671e2e70b1SJohn Baldwin 		if (devid == t->dc_devid && rev >= t->dc_minrev)
156896f2e892SBill Paul 			return (t);
156996f2e892SBill Paul 		t++;
157096f2e892SBill Paul 	}
157196f2e892SBill Paul 
157296f2e892SBill Paul 	return (NULL);
157396f2e892SBill Paul }
157496f2e892SBill Paul 
157596f2e892SBill Paul /*
157696f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
157796f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
157896f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
157996f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
158096f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
158196f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
158296f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
158396f2e892SBill Paul  */
1584e3d2833aSAlfred Perlstein static int
dc_probe(device_t dev)15850934f18aSMaxime Henrion dc_probe(device_t dev)
158696f2e892SBill Paul {
1587ebc284ccSMarius Strobl 	const struct dc_type *t;
158896f2e892SBill Paul 
158996f2e892SBill Paul 	t = dc_devtype(dev);
159096f2e892SBill Paul 
159196f2e892SBill Paul 	if (t != NULL) {
159296f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1593d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
159496f2e892SBill Paul 	}
159596f2e892SBill Paul 
159696f2e892SBill Paul 	return (ENXIO);
159796f2e892SBill Paul }
159896f2e892SBill Paul 
1599e3d2833aSAlfred Perlstein static void
dc_apply_fixup(struct dc_softc * sc,int media)16000934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
16015c1cfac4SBill Paul {
16025c1cfac4SBill Paul 	struct dc_mediainfo *m;
1603ee320f98SPyun YongHyeon 	uint8_t *p;
16045c1cfac4SBill Paul 	int i;
1605ee320f98SPyun YongHyeon 	uint32_t reg;
16065c1cfac4SBill Paul 
16075c1cfac4SBill Paul 	m = sc->dc_mi;
16085c1cfac4SBill Paul 
16095c1cfac4SBill Paul 	while (m != NULL) {
16105c1cfac4SBill Paul 		if (m->dc_media == media)
16115c1cfac4SBill Paul 			break;
16125c1cfac4SBill Paul 		m = m->dc_next;
16135c1cfac4SBill Paul 	}
16145c1cfac4SBill Paul 
16155c1cfac4SBill Paul 	if (m == NULL)
16165c1cfac4SBill Paul 		return;
16175c1cfac4SBill Paul 
16185c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16195c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16205c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16215c1cfac4SBill Paul 	}
16225c1cfac4SBill Paul 
16235c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16245c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16255c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16265c1cfac4SBill Paul 	}
16275c1cfac4SBill Paul }
16285c1cfac4SBill Paul 
1629abe4e865SPyun YongHyeon static int
dc_decode_leaf_sia(struct dc_softc * sc,struct dc_eblock_sia * l)16300934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16315c1cfac4SBill Paul {
16325c1cfac4SBill Paul 	struct dc_mediainfo *m;
16335c1cfac4SBill Paul 
16340934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1635abe4e865SPyun YongHyeon 	if (m == NULL) {
1636abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1637abe4e865SPyun YongHyeon 		return (ENOMEM);
1638abe4e865SPyun YongHyeon 	}
163987f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
164087f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16415c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
164287f4fa15SMartin Blapp 		break;
164387f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16445c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
164587f4fa15SMartin Blapp 		break;
164687f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16475c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
164887f4fa15SMartin Blapp 		break;
164987f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16505c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
165187f4fa15SMartin Blapp 		break;
165287f4fa15SMartin Blapp 	default:
165387f4fa15SMartin Blapp 		break;
165487f4fa15SMartin Blapp 	}
16555c1cfac4SBill Paul 
165687f4fa15SMartin Blapp 	/*
165787f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
165887f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
165987f4fa15SMartin Blapp 	 * supply Media Specific Data.
166087f4fa15SMartin Blapp 	 */
166187f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16625c1cfac4SBill Paul 		m->dc_gp_len = 2;
166387f4fa15SMartin Blapp 		m->dc_gp_ptr =
1664ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
166587f4fa15SMartin Blapp 	} else {
166687f4fa15SMartin Blapp 		m->dc_gp_len = 2;
166787f4fa15SMartin Blapp 		m->dc_gp_ptr =
1668ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
166987f4fa15SMartin Blapp 	}
16705c1cfac4SBill Paul 
16715c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16725c1cfac4SBill Paul 	sc->dc_mi = m;
16735c1cfac4SBill Paul 
16745c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
1675abe4e865SPyun YongHyeon 	return (0);
16765c1cfac4SBill Paul }
16775c1cfac4SBill Paul 
1678abe4e865SPyun YongHyeon static int
dc_decode_leaf_sym(struct dc_softc * sc,struct dc_eblock_sym * l)16790934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16805c1cfac4SBill Paul {
16815c1cfac4SBill Paul 	struct dc_mediainfo *m;
16825c1cfac4SBill Paul 
16830934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1684abe4e865SPyun YongHyeon 	if (m == NULL) {
1685abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1686abe4e865SPyun YongHyeon 		return (ENOMEM);
1687abe4e865SPyun YongHyeon 	}
16885c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16895c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16905c1cfac4SBill Paul 
16915c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16925c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
16935c1cfac4SBill Paul 
16945c1cfac4SBill Paul 	m->dc_gp_len = 2;
1695ee320f98SPyun YongHyeon 	m->dc_gp_ptr = (uint8_t *)&l->dc_sym_gpio_ctl;
16965c1cfac4SBill Paul 
16975c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16985c1cfac4SBill Paul 	sc->dc_mi = m;
16995c1cfac4SBill Paul 
17005c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
1701abe4e865SPyun YongHyeon 	return (0);
17025c1cfac4SBill Paul }
17035c1cfac4SBill Paul 
1704abe4e865SPyun YongHyeon static int
dc_decode_leaf_mii(struct dc_softc * sc,struct dc_eblock_mii * l)17050934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17065c1cfac4SBill Paul {
17075c1cfac4SBill Paul 	struct dc_mediainfo *m;
1708ee320f98SPyun YongHyeon 	uint8_t *p;
17095c1cfac4SBill Paul 
17100934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1711abe4e865SPyun YongHyeon 	if (m == NULL) {
1712abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1713abe4e865SPyun YongHyeon 		return (ENOMEM);
1714abe4e865SPyun YongHyeon 	}
17155c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17165c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17175c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17185c1cfac4SBill Paul 
1719ee320f98SPyun YongHyeon 	p = (uint8_t *)l;
17205c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17215c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17225c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17235c1cfac4SBill Paul 	m->dc_reset_len = *p;
17245c1cfac4SBill Paul 	p++;
17255c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17265c1cfac4SBill Paul 
17275c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17285c1cfac4SBill Paul 	sc->dc_mi = m;
1729abe4e865SPyun YongHyeon 	return (0);
17305c1cfac4SBill Paul }
17315c1cfac4SBill Paul 
1732abe4e865SPyun YongHyeon static int
dc_read_srom(struct dc_softc * sc,int bits)17330934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17343097aa70SWarner Losh {
17353097aa70SWarner Losh 	int size;
17363097aa70SWarner Losh 
1737abe4e865SPyun YongHyeon 	size = DC_ROM_SIZE(bits);
173852ca7ee2SPyun YongHyeon 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
1739abe4e865SPyun YongHyeon 	if (sc->dc_srom == NULL) {
1740abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate SROM buffer\n");
1741abe4e865SPyun YongHyeon 		return (ENOMEM);
1742abe4e865SPyun YongHyeon 	}
17433097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1744abe4e865SPyun YongHyeon 	return (0);
17453097aa70SWarner Losh }
17463097aa70SWarner Losh 
1747abe4e865SPyun YongHyeon static int
dc_parse_21143_srom(struct dc_softc * sc)17480934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17495c1cfac4SBill Paul {
17505c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17515c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
1752abe4e865SPyun YongHyeon 	int error, have_mii, i, loff;
17535c1cfac4SBill Paul 	char *ptr;
17545c1cfac4SBill Paul 
1755f956e0b3SMartin Blapp 	have_mii = 0;
17565c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17575c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17585c1cfac4SBill Paul 
17595c1cfac4SBill Paul 	ptr = (char *)lhdr;
17605c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1761f956e0b3SMartin Blapp 	/*
1762f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1763f956e0b3SMartin Blapp 	 */
1764f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1765f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1766f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1767f956e0b3SMartin Blapp 		    have_mii++;
1768f956e0b3SMartin Blapp 
1769f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1770f956e0b3SMartin Blapp 		ptr++;
1771f956e0b3SMartin Blapp 	}
1772f956e0b3SMartin Blapp 
1773f956e0b3SMartin Blapp 	/*
1774f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1775f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1776f956e0b3SMartin Blapp 	 */
1777f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1778f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1779abe4e865SPyun YongHyeon 	error = 0;
17805c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17815c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17825c1cfac4SBill Paul 		switch (hdr->dc_type) {
17835c1cfac4SBill Paul 		case DC_EBLOCK_MII:
1784abe4e865SPyun YongHyeon 			error = dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17855c1cfac4SBill Paul 			break;
17865c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1787f956e0b3SMartin Blapp 			if (! have_mii)
1788abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sia(sc,
1789f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
17905c1cfac4SBill Paul 			break;
17915c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1792f956e0b3SMartin Blapp 			if (! have_mii)
1793abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sym(sc,
1794f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
17955c1cfac4SBill Paul 			break;
17965c1cfac4SBill Paul 		default:
17975c1cfac4SBill Paul 			/* Don't care. Yet. */
17985c1cfac4SBill Paul 			break;
17995c1cfac4SBill Paul 		}
18005c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
18015c1cfac4SBill Paul 		ptr++;
18025c1cfac4SBill Paul 	}
1803abe4e865SPyun YongHyeon 	return (error);
18045c1cfac4SBill Paul }
18055c1cfac4SBill Paul 
180656e5e7aeSMaxime Henrion static void
dc_dma_map_addr(void * arg,bus_dma_segment_t * segs,int nseg,int error)180756e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
180856e5e7aeSMaxime Henrion {
18095f14ee23SPyun YongHyeon 	bus_addr_t *paddr;
181056e5e7aeSMaxime Henrion 
1811ebc284ccSMarius Strobl 	KASSERT(nseg == 1,
1812ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
181356e5e7aeSMaxime Henrion 	paddr = arg;
181456e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
181556e5e7aeSMaxime Henrion }
181656e5e7aeSMaxime Henrion 
18175f14ee23SPyun YongHyeon static int
dc_dma_alloc(struct dc_softc * sc)18185f14ee23SPyun YongHyeon dc_dma_alloc(struct dc_softc *sc)
18195f14ee23SPyun YongHyeon {
18205f14ee23SPyun YongHyeon 	int error, i;
18215f14ee23SPyun YongHyeon 
18225f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dc_dev), 1, 0,
18235f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
18245f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
18255f14ee23SPyun YongHyeon 	    NULL, NULL, &sc->dc_ptag);
18265f14ee23SPyun YongHyeon 	if (error) {
18275f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18285f14ee23SPyun YongHyeon 		    "failed to allocate parent DMA tag\n");
18295f14ee23SPyun YongHyeon 		goto fail;
18305f14ee23SPyun YongHyeon 	}
18315f14ee23SPyun YongHyeon 
18325f14ee23SPyun YongHyeon 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
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_RX_LIST_SZ, 1,
18355f14ee23SPyun YongHyeon 	    DC_RX_LIST_SZ, 0, NULL, NULL, &sc->dc_rx_ltag);
18365f14ee23SPyun YongHyeon 	if (error) {
18375f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX list DMA tag\n");
18385f14ee23SPyun YongHyeon 		goto fail;
18395f14ee23SPyun YongHyeon 	}
18405f14ee23SPyun YongHyeon 
18415f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18425f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_TX_LIST_SZ, 1,
18435f14ee23SPyun YongHyeon 	    DC_TX_LIST_SZ, 0, NULL, NULL, &sc->dc_tx_ltag);
18445f14ee23SPyun YongHyeon 	if (error) {
18455f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX list DMA tag\n");
18465f14ee23SPyun YongHyeon 		goto fail;
18475f14ee23SPyun YongHyeon 	}
18485f14ee23SPyun YongHyeon 
18495f14ee23SPyun YongHyeon 	/* RX descriptor list. */
18505f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_rx_ltag,
18515f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_rx_list, BUS_DMA_NOWAIT |
18525f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_rx_lmap);
18535f14ee23SPyun YongHyeon 	if (error) {
18545f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18555f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for RX list\n");
18565f14ee23SPyun YongHyeon 		goto fail;
18575f14ee23SPyun YongHyeon 	}
18585f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_rx_ltag, sc->dc_rx_lmap,
18595f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_rx_list, DC_RX_LIST_SZ, dc_dma_map_addr,
18605f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_rx_list_paddr, BUS_DMA_NOWAIT);
18615f14ee23SPyun YongHyeon 	if (error) {
18625f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18635f14ee23SPyun YongHyeon 		    "failed to load DMA'able memory for RX list\n");
18645f14ee23SPyun YongHyeon 		goto fail;
18655f14ee23SPyun YongHyeon 	}
18665f14ee23SPyun YongHyeon 	/* TX descriptor list. */
18675f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_tx_ltag,
18685f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_tx_list, BUS_DMA_NOWAIT |
18695f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_tx_lmap);
18705f14ee23SPyun YongHyeon 	if (error) {
18715f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18725f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for TX list\n");
18735f14ee23SPyun YongHyeon 		goto fail;
18745f14ee23SPyun YongHyeon 	}
18755f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_tx_ltag, sc->dc_tx_lmap,
18765f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_tx_list, DC_TX_LIST_SZ, dc_dma_map_addr,
18775f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_tx_list_paddr, BUS_DMA_NOWAIT);
18785f14ee23SPyun YongHyeon 	if (error) {
18795f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18805f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for TX list\n");
18815f14ee23SPyun YongHyeon 		goto fail;
18825f14ee23SPyun YongHyeon 	}
18835f14ee23SPyun YongHyeon 
18845f14ee23SPyun YongHyeon 	/*
18855f14ee23SPyun YongHyeon 	 * Allocate a busdma tag and DMA safe memory for the multicast
18865f14ee23SPyun YongHyeon 	 * setup frame.
18875f14ee23SPyun YongHyeon 	 */
18885f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18895f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
18905f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
18915f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_stag);
18925f14ee23SPyun YongHyeon 	if (error) {
18935f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18945f14ee23SPyun YongHyeon 		    "failed to create DMA tag for setup frame\n");
18955f14ee23SPyun YongHyeon 		goto fail;
18965f14ee23SPyun YongHyeon 	}
18975f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
18985f14ee23SPyun YongHyeon 	    BUS_DMA_NOWAIT, &sc->dc_smap);
18995f14ee23SPyun YongHyeon 	if (error) {
19005f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19015f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for setup frame\n");
19025f14ee23SPyun YongHyeon 		goto fail;
19035f14ee23SPyun YongHyeon 	}
19045f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
19055f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
19065f14ee23SPyun YongHyeon 	if (error) {
19075f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19085f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for setup frame\n");
19095f14ee23SPyun YongHyeon 		goto fail;
19105f14ee23SPyun YongHyeon 	}
19115f14ee23SPyun YongHyeon 
19125f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for RX mbufs. */
19135f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_RXBUF_ALIGN, 0,
19145f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19155f14ee23SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->dc_rx_mtag);
19165f14ee23SPyun YongHyeon 	if (error) {
19175f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX mbuf tag\n");
19185f14ee23SPyun YongHyeon 		goto fail;
19195f14ee23SPyun YongHyeon 	}
19205f14ee23SPyun YongHyeon 
19215f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for TX mbufs. */
19225f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, 1, 0,
19235f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19245f14ee23SPyun YongHyeon 	    MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
19255f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_tx_mtag);
19265f14ee23SPyun YongHyeon 	if (error) {
19275f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX mbuf tag\n");
19285f14ee23SPyun YongHyeon 		goto fail;
19295f14ee23SPyun YongHyeon 	}
19305f14ee23SPyun YongHyeon 
19315f14ee23SPyun YongHyeon 	/* Create the TX/RX busdma maps. */
19325f14ee23SPyun YongHyeon 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
19335f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_tx_mtag, 0,
19345f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_tx_map[i]);
19355f14ee23SPyun YongHyeon 		if (error) {
19365f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19375f14ee23SPyun YongHyeon 			    "failed to create TX mbuf dmamap\n");
19385f14ee23SPyun YongHyeon 			goto fail;
19395f14ee23SPyun YongHyeon 		}
19405f14ee23SPyun YongHyeon 	}
19415f14ee23SPyun YongHyeon 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
19425f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_rx_mtag, 0,
19435f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_rx_map[i]);
19445f14ee23SPyun YongHyeon 		if (error) {
19455f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19465f14ee23SPyun YongHyeon 			    "failed to create RX mbuf dmamap\n");
19475f14ee23SPyun YongHyeon 			goto fail;
19485f14ee23SPyun YongHyeon 		}
19495f14ee23SPyun YongHyeon 	}
19505f14ee23SPyun YongHyeon 	error = bus_dmamap_create(sc->dc_rx_mtag, 0, &sc->dc_sparemap);
19515f14ee23SPyun YongHyeon 	if (error) {
19525f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19535f14ee23SPyun YongHyeon 		    "failed to create spare RX mbuf dmamap\n");
19545f14ee23SPyun YongHyeon 		goto fail;
19555f14ee23SPyun YongHyeon 	}
19565f14ee23SPyun YongHyeon 
19575f14ee23SPyun YongHyeon fail:
19585f14ee23SPyun YongHyeon 	return (error);
19595f14ee23SPyun YongHyeon }
19605f14ee23SPyun YongHyeon 
19615f14ee23SPyun YongHyeon static void
dc_dma_free(struct dc_softc * sc)19625f14ee23SPyun YongHyeon dc_dma_free(struct dc_softc *sc)
19635f14ee23SPyun YongHyeon {
19645f14ee23SPyun YongHyeon 	int i;
19655f14ee23SPyun YongHyeon 
19665f14ee23SPyun YongHyeon 	/* RX buffers. */
19675f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19685f14ee23SPyun YongHyeon 		for (i = 0; i < DC_RX_LIST_CNT; i++) {
19695f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
19705f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_rx_mtag,
19715f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_rx_map[i]);
19725f14ee23SPyun YongHyeon 		}
19735f14ee23SPyun YongHyeon 		if (sc->dc_sparemap != NULL)
19745f14ee23SPyun YongHyeon 			bus_dmamap_destroy(sc->dc_rx_mtag, sc->dc_sparemap);
19755f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_mtag);
19765f14ee23SPyun YongHyeon 	}
19775f14ee23SPyun YongHyeon 
19785f14ee23SPyun YongHyeon 	/* TX buffers. */
19795f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19805f14ee23SPyun YongHyeon 		for (i = 0; i < DC_TX_LIST_CNT; i++) {
19815f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
19825f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_tx_mtag,
19835f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_tx_map[i]);
19845f14ee23SPyun YongHyeon 		}
19855f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_mtag);
19865f14ee23SPyun YongHyeon 	}
19875f14ee23SPyun YongHyeon 
19885f14ee23SPyun YongHyeon 	/* RX descriptor list. */
19895f14ee23SPyun YongHyeon 	if (sc->dc_rx_ltag) {
1990068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_rx_list_paddr != 0)
19915f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_ltag, sc->dc_rx_lmap);
1992068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_rx_list != NULL)
19935f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_rx_ltag, sc->dc_ldata.dc_rx_list,
19945f14ee23SPyun YongHyeon 			    sc->dc_rx_lmap);
19955f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_ltag);
19965f14ee23SPyun YongHyeon 	}
19975f14ee23SPyun YongHyeon 
19985f14ee23SPyun YongHyeon 	/* TX descriptor list. */
19995f14ee23SPyun YongHyeon 	if (sc->dc_tx_ltag) {
2000068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_tx_list_paddr != 0)
20015f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_ltag, sc->dc_tx_lmap);
2002068d8643SJohn Baldwin 		if (sc->dc_ldata.dc_tx_list != NULL)
20035f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_tx_ltag, sc->dc_ldata.dc_tx_list,
20045f14ee23SPyun YongHyeon 			    sc->dc_tx_lmap);
20055f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_ltag);
20065f14ee23SPyun YongHyeon 	}
20075f14ee23SPyun YongHyeon 
20085f14ee23SPyun YongHyeon 	/* multicast setup frame. */
20095f14ee23SPyun YongHyeon 	if (sc->dc_stag) {
2010068d8643SJohn Baldwin 		if (sc->dc_saddr != 0)
20115f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_stag, sc->dc_smap);
2012068d8643SJohn Baldwin 		if (sc->dc_cdata.dc_sbuf != NULL)
20135f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf,
20145f14ee23SPyun YongHyeon 			    sc->dc_smap);
20155f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_stag);
20165f14ee23SPyun YongHyeon 	}
20175f14ee23SPyun YongHyeon }
20185f14ee23SPyun YongHyeon 
201996f2e892SBill Paul /*
202096f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
202196f2e892SBill Paul  * setup and ethernet/BPF attach.
202296f2e892SBill Paul  */
2023e3d2833aSAlfred Perlstein static int
dc_attach(device_t dev)20240934f18aSMaxime Henrion dc_attach(device_t dev)
202596f2e892SBill Paul {
20268df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
2027ee320f98SPyun YongHyeon 	uint32_t command;
202896f2e892SBill Paul 	struct dc_softc *sc;
2029*91f3f3fcSJustin Hibbits 	if_t ifp;
2030b289c607SPyun YongHyeon 	struct dc_mediainfo *m;
2031ee320f98SPyun YongHyeon 	uint32_t reg, revision;
203252ca7ee2SPyun YongHyeon 	uint16_t *srom;
203352ca7ee2SPyun YongHyeon 	int error, mac_offset, n, phy, rid, tmp;
2034ee320f98SPyun YongHyeon 	uint8_t *mac;
203596f2e892SBill Paul 
203696f2e892SBill Paul 	sc = device_get_softc(dev);
20376b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
203896f2e892SBill Paul 
20396008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2040c8b27acaSJohn Baldwin 	    MTX_DEF);
2041c3e7434fSWarner Losh 
204296f2e892SBill Paul 	/*
204396f2e892SBill Paul 	 * Map control/status registers.
204496f2e892SBill Paul 	 */
204507f65363SBill Paul 	pci_enable_busmaster(dev);
204696f2e892SBill Paul 
204796f2e892SBill Paul 	rid = DC_RID;
20485f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
204996f2e892SBill Paul 
205096f2e892SBill Paul 	if (sc->dc_res == NULL) {
205122f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
205296f2e892SBill Paul 		error = ENXIO;
2053608654d4SNate Lawson 		goto fail;
205496f2e892SBill Paul 	}
205596f2e892SBill Paul 
205696f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
205796f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
205896f2e892SBill Paul 
20590934f18aSMaxime Henrion 	/* Allocate interrupt. */
206054f1f1d1SNate Lawson 	rid = 0;
20615f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
206254f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
206354f1f1d1SNate Lawson 
206454f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
206522f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
206654f1f1d1SNate Lawson 		error = ENXIO;
206754f1f1d1SNate Lawson 		goto fail;
206854f1f1d1SNate Lawson 	}
206954f1f1d1SNate Lawson 
207096f2e892SBill Paul 	/* Need this info to decide on a chip type. */
207196f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
20721e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
207396f2e892SBill Paul 
2074abe4e865SPyun YongHyeon 	error = 0;
20756d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
20761e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
20771e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
20781e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
20791e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
2080eecb3844SMartin Blapp 		dc_eeprom_width(sc);
2081eecb3844SMartin Blapp 
20821e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
20831e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
208496f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
208596f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2086042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
20875c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
2088abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2089abe4e865SPyun YongHyeon 		if (error != 0)
2090abe4e865SPyun YongHyeon 			goto fail;
209196f2e892SBill Paul 		break;
20921e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
20931e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
20941e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
209596f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
2096318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
2097318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
20987dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
20994a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
21001e2e70b1SJohn Baldwin 
21010a46b1dcSBill Paul 		/* Increase the latency timer value. */
21021e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
210396f2e892SBill Paul 		break;
21041e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
210596f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
210696f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
210796f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
210896f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2109abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2110abe4e865SPyun YongHyeon 		if (error != 0)
2111abe4e865SPyun YongHyeon 			goto fail;
211296f2e892SBill Paul 		break;
2113593a1aeaSMartin Blapp 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983):
21141e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
21151e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
21161e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
21171e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
21181e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
21191e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
21201e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
21211e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
21221e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
21231e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
21241e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
21251e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
212617762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
212717762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
2128593a1aeaSMartin Blapp 		sc->dc_type = DC_TYPE_AN983;
2129acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
213096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
213196f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
213296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2133129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
213496f2e892SBill Paul 		break;
21351e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
21361e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
213796f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
213896f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
213996f2e892SBill Paul 		}
2140318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
214196f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
2142318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
2143318b02fdSBill Paul 		}
2144318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
214596f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
214696f2e892SBill Paul 		break;
21471e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
21481e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
214979d11e09SBill Paul 		/*
215079d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
215179d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
215279d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
215379d11e09SBill Paul 		 * get the right number of bits out of the
215479d11e09SBill Paul 		 * CRC routine.
215579d11e09SBill Paul 		 */
215679d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
215779d11e09SBill Paul 		    revision < DC_REVISION_98725)
215879d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
215996f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
216096f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2161318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
216296f2e892SBill Paul 		break;
21631e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
2164ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
2165ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2166ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2167ead7cde9SBill Paul 		break;
21681e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
216996f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
217079d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
2171318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
217296f2e892SBill Paul 		break;
21731e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
217496f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
217591cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
217696f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
217796f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
2178abe4e865SPyun YongHyeon 		if (sc->dc_pnic_rx_buf == NULL) {
2179abe4e865SPyun YongHyeon 			device_printf(sc->dc_dev,
2180abe4e865SPyun YongHyeon 			    "Could not allocate PNIC RX buffer\n");
2181abe4e865SPyun YongHyeon 			error = ENOMEM;
2182abe4e865SPyun YongHyeon 			goto fail;
2183abe4e865SPyun YongHyeon 		}
218496f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
218596f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
218696f2e892SBill Paul 		break;
21871e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
218896f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
218996f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
219096f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
219196f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
219296f2e892SBill Paul 		break;
21931e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
2194feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
21952dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
21962dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2197feb78939SJonathan Chen 		/*
2198feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2199feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
22002dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2201feb78939SJonathan Chen 		 */
22023097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2203feb78939SJonathan Chen 		break;
22041e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
22051af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
22061af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
22071af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
22081af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2209abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2210abe4e865SPyun YongHyeon 		if (error != 0)
2211abe4e865SPyun YongHyeon 			goto fail;
22121af8bec7SBill Paul 		break;
221352ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261):
221452ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263):
221552ca7ee2SPyun YongHyeon 		if (sc->dc_info->dc_devid ==
221652ca7ee2SPyun YongHyeon 		    DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261))
221752ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5261;
221852ca7ee2SPyun YongHyeon 		else
221952ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5263;
222052ca7ee2SPyun YongHyeon 		/* TX buffers should be aligned on 4 byte boundary. */
222152ca7ee2SPyun YongHyeon 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
222252ca7ee2SPyun YongHyeon 		    DC_TX_ALIGN;
222352ca7ee2SPyun YongHyeon 		sc->dc_pmode = DC_PMODE_MII;
222452ca7ee2SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
222552ca7ee2SPyun YongHyeon 		if (error != 0)
222652ca7ee2SPyun YongHyeon 			goto fail;
222752ca7ee2SPyun YongHyeon 		break;
222896f2e892SBill Paul 	default:
22291e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
22301e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
223196f2e892SBill Paul 		break;
223296f2e892SBill Paul 	}
223396f2e892SBill Paul 
223496f2e892SBill Paul 	/* Save the cache line size. */
223588d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
223688d739dcSBill Paul 		sc->dc_cachesize = 0;
223788d739dcSBill Paul 	else
22381e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
223996f2e892SBill Paul 
224096f2e892SBill Paul 	/* Reset the adapter. */
224196f2e892SBill Paul 	dc_reset(sc);
224296f2e892SBill Paul 
224396f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2244feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
224596f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
224696f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
224796f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
224896f2e892SBill Paul 	}
224996f2e892SBill Paul 
225096f2e892SBill Paul 	/*
225196f2e892SBill Paul 	 * Try to learn something about the supported media.
225296f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
225396f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
225496f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
225596f2e892SBill Paul 	 * Intel 21143.
225696f2e892SBill Paul 	 */
2257abe4e865SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
2258abe4e865SPyun YongHyeon 		error = dc_parse_21143_srom(sc);
2259abe4e865SPyun YongHyeon 		if (error != 0)
2260abe4e865SPyun YongHyeon 			goto fail;
2261abe4e865SPyun YongHyeon 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
226296f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
226396f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
226496f2e892SBill Paul 		else
226596f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
226696f2e892SBill Paul 	} else if (!sc->dc_pmode)
226796f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
226896f2e892SBill Paul 
226996f2e892SBill Paul 	/*
227096f2e892SBill Paul 	 * Get station address from the EEPROM.
227196f2e892SBill Paul 	 */
227296f2e892SBill Paul 	switch(sc->dc_type) {
227396f2e892SBill Paul 	case DC_TYPE_98713:
227496f2e892SBill Paul 	case DC_TYPE_98713A:
227596f2e892SBill Paul 	case DC_TYPE_987x5:
227696f2e892SBill Paul 	case DC_TYPE_PNICII:
227796f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
227896f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
227996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
228096f2e892SBill Paul 		break;
228196f2e892SBill Paul 	case DC_TYPE_PNIC:
228296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
228396f2e892SBill Paul 		break;
228496f2e892SBill Paul 	case DC_TYPE_DM9102:
2285ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
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);
2383*91f3f3fcSJustin Hibbits 	if_setsoftc(ifp, sc);
23849bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2385*91f3f3fcSJustin Hibbits 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
2386*91f3f3fcSJustin Hibbits 	if_setioctlfn(ifp, dc_ioctl);
2387*91f3f3fcSJustin Hibbits 	if_setstartfn(ifp, dc_start);
2388*91f3f3fcSJustin Hibbits 	if_setinitfn(ifp, dc_init);
2389*91f3f3fcSJustin Hibbits 	if_setsendqlen(ifp, DC_TX_LIST_CNT - 1);
2390*91f3f3fcSJustin Hibbits 	if_setsendqready(ifp);
239196f2e892SBill Paul 
239296f2e892SBill Paul 	/*
23935c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
23945c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
23955c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
23965c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
23975c1cfac4SBill Paul 	 * driver instead.
239896f2e892SBill Paul 	 */
23998e5d93dbSMarius Strobl 	tmp = 0;
24005c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
24015c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
24025c1cfac4SBill Paul 		tmp = sc->dc_pmode;
24035c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
24045c1cfac4SBill Paul 	}
24055c1cfac4SBill Paul 
24066d431b17SWarner Losh 	/*
24076d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
24088e5d93dbSMarius Strobl 	 * to the MII.  This needs to be done before mii_attach so that
24096d431b17SWarner Losh 	 * we can actually see them.
24106d431b17SWarner Losh 	 */
24116d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
24126d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
24136d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24146d431b17SWarner Losh 		DELAY(10);
24156d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
24166d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24176d431b17SWarner Losh 		DELAY(10);
24186d431b17SWarner Losh 	}
24196d431b17SWarner Losh 
24208e5d93dbSMarius Strobl 	phy = MII_PHY_ANY;
24218e5d93dbSMarius Strobl 	/*
24228e5d93dbSMarius Strobl 	 * Note: both the AL981 and AN983 have internal PHYs, however the
24238e5d93dbSMarius Strobl 	 * AL981 provides direct access to the PHY registers while the AN983
24248e5d93dbSMarius Strobl 	 * uses a serial MII interface. The AN983's MII interface is also
24258e5d93dbSMarius Strobl 	 * buggy in that you can read from any MII address (0 to 31), but
24268e5d93dbSMarius Strobl 	 * only address 1 behaves normally. To deal with both cases, we
24278e5d93dbSMarius Strobl 	 * pretend that the PHY is at MII address 1.
24288e5d93dbSMarius Strobl 	 */
24298e5d93dbSMarius Strobl 	if (DC_IS_ADMTEK(sc))
24308e5d93dbSMarius Strobl 		phy = DC_ADMTEK_PHYADDR;
24318e5d93dbSMarius Strobl 
24328e5d93dbSMarius Strobl 	/*
24338e5d93dbSMarius Strobl 	 * Note: the ukphy probes of the RS7112 report a PHY at MII address
24348e5d93dbSMarius Strobl 	 * 0 (possibly HomePNA?) and 1 (ethernet) so we only respond to the
24358e5d93dbSMarius Strobl 	 * correct one.
24368e5d93dbSMarius Strobl 	 */
24378e5d93dbSMarius Strobl 	if (DC_IS_CONEXANT(sc))
24388e5d93dbSMarius Strobl 		phy = DC_CONEXANT_PHYADDR;
24398e5d93dbSMarius Strobl 
24408e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24418e5d93dbSMarius Strobl 	    dc_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
244296f2e892SBill Paul 
244396f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
24445c1cfac4SBill Paul 		sc->dc_pmode = tmp;
24455c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
244696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2447042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
244878999dd1SBill Paul 		/*
244978999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
245078999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
245178999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
245278999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
245378999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
245478999dd1SBill Paul 		 */
24551e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
24561e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
245778999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
2458166e31d9SMarius Strobl 		error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
2459166e31d9SMarius Strobl 		    dc_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
2460166e31d9SMarius Strobl 		    MII_OFFSET_ANY, 0);
246196f2e892SBill Paul 	}
246296f2e892SBill Paul 
246396f2e892SBill Paul 	if (error) {
24648e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
246596f2e892SBill Paul 		goto fail;
246696f2e892SBill Paul 	}
246796f2e892SBill Paul 
2468028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2469028a8491SMartin Blapp 		/*
2470028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2471028a8491SMartin Blapp 		 */
2472028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2473028a8491SMartin Blapp 	}
2474028a8491SMartin Blapp 
247596f2e892SBill Paul 	/*
2476db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2477db40c1aeSDoug Ambrisko 	 */
2478*91f3f3fcSJustin Hibbits 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
2479*91f3f3fcSJustin Hibbits 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
2480*91f3f3fcSJustin Hibbits 	if_setcapenable(ifp, if_getcapabilities(ifp));
2481e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2482*91f3f3fcSJustin Hibbits 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
2483e695984eSRuslan Ermilov #endif
2484db40c1aeSDoug Ambrisko 
2485c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2486b1d16143SMarius Strobl 	callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
248796f2e892SBill Paul 
2488608654d4SNate Lawson 	/*
2489608654d4SNate Lawson 	 * Call MI attach routine.
2490608654d4SNate Lawson 	 */
24918df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2492608654d4SNate Lawson 
249354f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2494c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2495ef544f63SPaolo Pisati 	    NULL, dc_intr, sc, &sc->dc_intrhand);
2496608654d4SNate Lawson 
2497608654d4SNate Lawson 	if (error) {
249822f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2499693f4477SNate Lawson 		ether_ifdetach(ifp);
250054f1f1d1SNate Lawson 		goto fail;
2501608654d4SNate Lawson 	}
2502510a809eSMike Smith 
250396f2e892SBill Paul fail:
250454f1f1d1SNate Lawson 	if (error)
250554f1f1d1SNate Lawson 		dc_detach(dev);
250696f2e892SBill Paul 	return (error);
250796f2e892SBill Paul }
250896f2e892SBill Paul 
2509693f4477SNate Lawson /*
2510693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2511693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2512693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2513693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2514693f4477SNate Lawson  * allocated.
2515693f4477SNate Lawson  */
2516e3d2833aSAlfred Perlstein static int
dc_detach(device_t dev)25170934f18aSMaxime Henrion dc_detach(device_t dev)
251896f2e892SBill Paul {
251996f2e892SBill Paul 	struct dc_softc *sc;
2520*91f3f3fcSJustin Hibbits 	if_t ifp;
25215c1cfac4SBill Paul 	struct dc_mediainfo *m;
252296f2e892SBill Paul 
252396f2e892SBill Paul 	sc = device_get_softc(dev);
252459f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2525d1ce9105SBill Paul 
2526fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
252796f2e892SBill Paul 
252840929967SGleb Smirnoff #ifdef DEVICE_POLLING
2529*91f3f3fcSJustin Hibbits 	if (ifp != NULL && if_getcapenable(ifp) & IFCAP_POLLING)
253040929967SGleb Smirnoff 		ether_poll_deregister(ifp);
253140929967SGleb Smirnoff #endif
253240929967SGleb Smirnoff 
2533693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2534214073e5SWarner Losh 	if (device_is_attached(dev)) {
2535c8b27acaSJohn Baldwin 		DC_LOCK(sc);
253696f2e892SBill Paul 		dc_stop(sc);
2537c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2538c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
2539b1d16143SMarius Strobl 		callout_drain(&sc->dc_wdog_ch);
25409ef8b520SSam Leffler 		ether_ifdetach(ifp);
2541693f4477SNate Lawson 	}
254254f1f1d1SNate Lawson 	bus_generic_detach(dev);
254396f2e892SBill Paul 
254454f1f1d1SNate Lawson 	if (sc->dc_intrhand)
254596f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
254654f1f1d1SNate Lawson 	if (sc->dc_irq)
254796f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
254854f1f1d1SNate Lawson 	if (sc->dc_res)
254996f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
255096f2e892SBill Paul 
2551166e31d9SMarius Strobl 	if (ifp != NULL)
25526a3033a8SWarner Losh 		if_free(ifp);
25536a3033a8SWarner Losh 
25545f14ee23SPyun YongHyeon 	dc_dma_free(sc);
255556e5e7aeSMaxime Henrion 
255696f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
255796f2e892SBill Paul 
25585c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
25595c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
25605c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
25615c1cfac4SBill Paul 		sc->dc_mi = m;
25625c1cfac4SBill Paul 	}
25637efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
25645c1cfac4SBill Paul 
2565d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
256696f2e892SBill Paul 
256796f2e892SBill Paul 	return (0);
256896f2e892SBill Paul }
256996f2e892SBill Paul 
257096f2e892SBill Paul /*
257196f2e892SBill Paul  * Initialize the transmit descriptors.
257296f2e892SBill Paul  */
2573e3d2833aSAlfred Perlstein static int
dc_list_tx_init(struct dc_softc * sc)25740934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
257596f2e892SBill Paul {
257696f2e892SBill Paul 	struct dc_chain_data *cd;
257796f2e892SBill Paul 	struct dc_list_data *ld;
257801faf54bSLuigi Rizzo 	int i, nexti;
257996f2e892SBill Paul 
258096f2e892SBill Paul 	cd = &sc->dc_cdata;
25815f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
258296f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2583b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2584b3811c95SMaxime Henrion 			nexti = 0;
2585b3811c95SMaxime Henrion 		else
2586b3811c95SMaxime Henrion 			nexti = i + 1;
258752c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_status = 0;
258852c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_ctl = 0;
258952c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_data = 0;
2590af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
259196f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
259296f2e892SBill Paul 	}
259396f2e892SBill Paul 
259496f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
259506d23883SPyun YongHyeon 	cd->dc_tx_pkts = 0;
25965f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
259756e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
259896f2e892SBill Paul 	return (0);
259996f2e892SBill Paul }
260096f2e892SBill Paul 
260196f2e892SBill Paul /*
260296f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
260396f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
260496f2e892SBill Paul  * points back to the first.
260596f2e892SBill Paul  */
2606e3d2833aSAlfred Perlstein static int
dc_list_rx_init(struct dc_softc * sc)26070934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
260896f2e892SBill Paul {
260996f2e892SBill Paul 	struct dc_chain_data *cd;
261096f2e892SBill Paul 	struct dc_list_data *ld;
261101faf54bSLuigi Rizzo 	int i, nexti;
261296f2e892SBill Paul 
261396f2e892SBill Paul 	cd = &sc->dc_cdata;
26145f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
261596f2e892SBill Paul 
261696f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
26175f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0)
261896f2e892SBill Paul 			return (ENOBUFS);
2619b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2620b3811c95SMaxime Henrion 			nexti = 0;
2621b3811c95SMaxime Henrion 		else
2622b3811c95SMaxime Henrion 			nexti = i + 1;
2623af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
262496f2e892SBill Paul 	}
262596f2e892SBill Paul 
262696f2e892SBill Paul 	cd->dc_rx_prod = 0;
26275f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
262856e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
262996f2e892SBill Paul 	return (0);
263096f2e892SBill Paul }
263196f2e892SBill Paul 
263296f2e892SBill Paul /*
263396f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
263496f2e892SBill Paul  */
2635e3d2833aSAlfred Perlstein static int
dc_newbuf(struct dc_softc * sc,int i)26365f14ee23SPyun YongHyeon dc_newbuf(struct dc_softc *sc, int i)
263796f2e892SBill Paul {
26385f14ee23SPyun YongHyeon 	struct mbuf *m;
26395f14ee23SPyun YongHyeon 	bus_dmamap_t map;
264082a67a70SMarius Strobl 	bus_dma_segment_t segs[1];
264182a67a70SMarius Strobl 	int error, nseg;
264296f2e892SBill Paul 
2643c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
26445f14ee23SPyun YongHyeon 	if (m == NULL)
264596f2e892SBill Paul 		return (ENOBUFS);
26465f14ee23SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
26475f14ee23SPyun YongHyeon 	m_adj(m, sizeof(u_int64_t));
264896f2e892SBill Paul 
264996f2e892SBill Paul 	/*
265096f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
265196f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
265296f2e892SBill Paul 	 * 82c169 chips.
265396f2e892SBill Paul 	 */
265496f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
26555f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
265696f2e892SBill Paul 
26575f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_rx_mtag, sc->dc_sparemap,
26585f14ee23SPyun YongHyeon 	    m, segs, &nseg, 0);
265956e5e7aeSMaxime Henrion 	if (error) {
26605f14ee23SPyun YongHyeon 		m_freem(m);
266156e5e7aeSMaxime Henrion 		return (error);
266256e5e7aeSMaxime Henrion 	}
26635f14ee23SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: wrong number of segments (%d)", __func__,
26645f14ee23SPyun YongHyeon 	    nseg));
26655f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_rx_chain[i] != NULL)
26665f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i]);
266796f2e892SBill Paul 
26685f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_rx_map[i];
26695f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
26705f14ee23SPyun YongHyeon 	sc->dc_sparemap = map;
26715f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_chain[i] = m;
26725f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
267356e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
26745f14ee23SPyun YongHyeon 
26755f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
26765f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_data =
26775f14ee23SPyun YongHyeon 	    htole32(DC_ADDR_LO(segs[0].ds_addr));
26785f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
26795f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
268056e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
268196f2e892SBill Paul 	return (0);
268296f2e892SBill Paul }
268396f2e892SBill Paul 
268496f2e892SBill Paul /*
268596f2e892SBill Paul  * Grrrrr.
268696f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
268796f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
268896f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
268996f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
269096f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
269196f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
269296f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
269396f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
269496f2e892SBill Paul  *
269596f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
269696f2e892SBill Paul  * Here's what we know:
269796f2e892SBill Paul  *
269896f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
269996f2e892SBill Paul  *   descriptors uploaded.
270096f2e892SBill Paul  *
270196f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
270296f2e892SBill Paul  *   total data upload.
270396f2e892SBill Paul  *
270496f2e892SBill Paul  * - We know the size of the desired received frame because it will be
270596f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
270696f2e892SBill Paul  *
270796f2e892SBill Paul  * Here's what we do:
270896f2e892SBill Paul  *
270996f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
271096f2e892SBill Paul  *   This means that we know that the buffer contents should be all
271196f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
271296f2e892SBill Paul  *
271396f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
271496f2e892SBill Paul  *   ethernet CRC at the end.
271596f2e892SBill Paul  *
271696f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
271796f2e892SBill Paul  *
271896f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
271996f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
272096f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
272196f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
272296f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
272396f2e892SBill Paul  *   we won't be fooled.
272496f2e892SBill Paul  *
272596f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
272696f2e892SBill Paul  *   that value from the current pointer location. This brings us
272796f2e892SBill Paul  *   to the start of the actual received packet.
272896f2e892SBill Paul  *
272996f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
273096f2e892SBill Paul  *   frame length.
273196f2e892SBill Paul  *
273296f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
273396f2e892SBill Paul  * the time.
273496f2e892SBill Paul  */
273596f2e892SBill Paul 
273696f2e892SBill Paul #define	DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2737e3d2833aSAlfred Perlstein static void
dc_pnic_rx_bug_war(struct dc_softc * sc,int idx)27380934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
273996f2e892SBill Paul {
274096f2e892SBill Paul 	struct dc_desc *cur_rx;
274196f2e892SBill Paul 	struct dc_desc *c = NULL;
274296f2e892SBill Paul 	struct mbuf *m = NULL;
274396f2e892SBill Paul 	unsigned char *ptr;
274496f2e892SBill Paul 	int i, total_len;
2745ee320f98SPyun YongHyeon 	uint32_t rxstat = 0;
274696f2e892SBill Paul 
274796f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
27485f14ee23SPyun YongHyeon 	cur_rx = &sc->dc_ldata.dc_rx_list[idx];
274996f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
27501edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
275196f2e892SBill Paul 
275296f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
275396f2e892SBill Paul 	while (1) {
27545f14ee23SPyun YongHyeon 		c = &sc->dc_ldata.dc_rx_list[i];
2755af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
275696f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
275796f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
275896f2e892SBill Paul 		ptr += DC_RXLEN;
275996f2e892SBill Paul 		/* If this is the last buffer, break out. */
276096f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
276196f2e892SBill Paul 			break;
27625f14ee23SPyun YongHyeon 		dc_discard_rxbuf(sc, i);
276396f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
276496f2e892SBill Paul 	}
276596f2e892SBill Paul 
276696f2e892SBill Paul 	/* Find the length of the actual receive frame. */
276796f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
276896f2e892SBill Paul 
276996f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
277096f2e892SBill Paul 	while (*ptr == 0x00)
277196f2e892SBill Paul 		ptr--;
277296f2e892SBill Paul 
277396f2e892SBill Paul 	/* Round off. */
277496f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
277596f2e892SBill Paul 		ptr -= 1;
277696f2e892SBill Paul 
277796f2e892SBill Paul 	/* Now find the start of the frame. */
277896f2e892SBill Paul 	ptr -= total_len;
277996f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
278096f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
278196f2e892SBill Paul 
278296f2e892SBill Paul 	/*
278396f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
278496f2e892SBill Paul 	 * the status word to make it look like a successful
278596f2e892SBill Paul 	 * frame reception.
278696f2e892SBill Paul 	 */
278796f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2788af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
278996f2e892SBill Paul }
279096f2e892SBill Paul 
279196f2e892SBill Paul /*
279273bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
279373bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
279473bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
279573bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
279673bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
279773bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
279873bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
279973bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
280073bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
280173bf949cSBill Paul  */
2802e3d2833aSAlfred Perlstein static int
dc_rx_resync(struct dc_softc * sc)28030934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
280473bf949cSBill Paul {
280573bf949cSBill Paul 	struct dc_desc *cur_rx;
28060934f18aSMaxime Henrion 	int i, pos;
280773bf949cSBill Paul 
280873bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
280973bf949cSBill Paul 
281073bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
28115f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[pos];
2812af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
281373bf949cSBill Paul 			break;
281473bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
281573bf949cSBill Paul 	}
281673bf949cSBill Paul 
281773bf949cSBill Paul 	/* If the ring really is empty, then just return. */
281873bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
281973bf949cSBill Paul 		return (0);
282073bf949cSBill Paul 
282173bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
282273bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
282373bf949cSBill Paul 
282473bf949cSBill Paul 	return (EAGAIN);
282573bf949cSBill Paul }
282673bf949cSBill Paul 
28275f14ee23SPyun YongHyeon static void
dc_discard_rxbuf(struct dc_softc * sc,int i)28285f14ee23SPyun YongHyeon dc_discard_rxbuf(struct dc_softc *sc, int i)
28295f14ee23SPyun YongHyeon {
28305f14ee23SPyun YongHyeon 	struct mbuf *m;
28315f14ee23SPyun YongHyeon 
28325f14ee23SPyun YongHyeon 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
28335f14ee23SPyun YongHyeon 		m = sc->dc_cdata.dc_rx_chain[i];
28345f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
28355f14ee23SPyun YongHyeon 	}
28365f14ee23SPyun YongHyeon 
28375f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
28385f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
28395f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_PREREAD |
28405f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
28415f14ee23SPyun YongHyeon }
28425f14ee23SPyun YongHyeon 
284373bf949cSBill Paul /*
284496f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
284596f2e892SBill Paul  * the higher level protocols.
284696f2e892SBill Paul  */
28471abcdbd1SAttilio Rao static int
dc_rxeof(struct dc_softc * sc)28480934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
284996f2e892SBill Paul {
28505f14ee23SPyun YongHyeon 	struct mbuf *m;
2851*91f3f3fcSJustin Hibbits 	if_t ifp;
285296f2e892SBill Paul 	struct dc_desc *cur_rx;
28531abcdbd1SAttilio Rao 	int i, total_len, rx_npkts;
2854ee320f98SPyun YongHyeon 	uint32_t rxstat;
285596f2e892SBill Paul 
28565120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
28575120abbfSSam Leffler 
2858fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
28591abcdbd1SAttilio Rao 	rx_npkts = 0;
286096f2e892SBill Paul 
28615f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_POSTREAD |
28625f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
28635f14ee23SPyun YongHyeon 	for (i = sc->dc_cdata.dc_rx_prod;
2864*91f3f3fcSJustin Hibbits 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0;
28655f14ee23SPyun YongHyeon 	    DC_INC(i, DC_RX_LIST_CNT)) {
2866e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2867*91f3f3fcSJustin Hibbits 		if (if_getcapenable(ifp) & IFCAP_POLLING) {
2868e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2869e4fc250cSLuigi Rizzo 				break;
2870e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2871e4fc250cSLuigi Rizzo 		}
28720934f18aSMaxime Henrion #endif
28735f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[i];
2874af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
28755f14ee23SPyun YongHyeon 		if ((rxstat & DC_RXSTAT_OWN) != 0)
28765f14ee23SPyun YongHyeon 			break;
287796f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
28785f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
287956e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
288096f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
2881848a02fcSPyun YongHyeon 		rx_npkts++;
288296f2e892SBill Paul 
288396f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
288496f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
288596f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
288696f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
28875f14ee23SPyun YongHyeon 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0)
288896f2e892SBill Paul 					continue;
288996f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2890af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
289196f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
289296f2e892SBill Paul 			}
289396f2e892SBill Paul 		}
289496f2e892SBill Paul 
289596f2e892SBill Paul 		/*
289696f2e892SBill Paul 		 * If an error occurs, update stats, clear the
289796f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
289896f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2899db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
29000934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
290196f2e892SBill Paul 		 */
2902db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2903db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2904db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2905db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2906db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
2907c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
290896f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
2909c8dfaf38SGleb Smirnoff 					if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
29105f14ee23SPyun YongHyeon 				dc_discard_rxbuf(sc, i);
29115f14ee23SPyun YongHyeon 				if (rxstat & DC_RXSTAT_CRCERR)
291296f2e892SBill Paul 					continue;
29135f14ee23SPyun YongHyeon 				else {
2914*91f3f3fcSJustin Hibbits 					if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2915c8b27acaSJohn Baldwin 					dc_init_locked(sc);
29161abcdbd1SAttilio Rao 					return (rx_npkts);
291796f2e892SBill Paul 				}
291896f2e892SBill Paul 			}
2919db40c1aeSDoug Ambrisko 		}
292096f2e892SBill Paul 
292196f2e892SBill Paul 		/* No errors; receive the packet. */
292296f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2923432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
292401faf54bSLuigi Rizzo 		/*
2925432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
292601faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
292701faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
292801faf54bSLuigi Rizzo 		 * copy done in m_devget().
292901faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
293001faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
293101faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
293201faf54bSLuigi Rizzo 		 */
29335f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0) {
29345f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
2935c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
29365f14ee23SPyun YongHyeon 			continue;
29375f14ee23SPyun YongHyeon 		}
293801faf54bSLuigi Rizzo 		m->m_pkthdr.rcvif = ifp;
293901faf54bSLuigi Rizzo 		m->m_pkthdr.len = m->m_len = total_len;
29405f14ee23SPyun YongHyeon #else
294101faf54bSLuigi Rizzo 		{
29425f14ee23SPyun YongHyeon 			struct mbuf *m0;
29435f14ee23SPyun YongHyeon 
294401faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
294501faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
29465f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
294796f2e892SBill Paul 			if (m0 == NULL) {
2948c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
294996f2e892SBill Paul 				continue;
295096f2e892SBill Paul 			}
295196f2e892SBill Paul 			m = m0;
295201faf54bSLuigi Rizzo 		}
29535f14ee23SPyun YongHyeon #endif
295496f2e892SBill Paul 
2955c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
29565120abbfSSam Leffler 		DC_UNLOCK(sc);
2957*91f3f3fcSJustin Hibbits 		if_input(ifp, m);
29585120abbfSSam Leffler 		DC_LOCK(sc);
295996f2e892SBill Paul 	}
296096f2e892SBill Paul 
296196f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
29621abcdbd1SAttilio Rao 	return (rx_npkts);
296396f2e892SBill Paul }
296496f2e892SBill Paul 
296596f2e892SBill Paul /*
296696f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
296796f2e892SBill Paul  * the list buffers.
296896f2e892SBill Paul  */
2969e3d2833aSAlfred Perlstein static void
dc_txeof(struct dc_softc * sc)29700934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
297196f2e892SBill Paul {
29725f14ee23SPyun YongHyeon 	struct dc_desc *cur_tx;
2973*91f3f3fcSJustin Hibbits 	if_t ifp;
29745f14ee23SPyun YongHyeon 	int idx, setup;
2975ee320f98SPyun YongHyeon 	uint32_t ctl, txstat;
297696f2e892SBill Paul 
297706d23883SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt == 0)
297806d23883SPyun YongHyeon 		return;
297906d23883SPyun YongHyeon 
2980fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
298196f2e892SBill Paul 
298296f2e892SBill Paul 	/*
298396f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
298496f2e892SBill Paul 	 * frames that have been transmitted.
298596f2e892SBill Paul 	 */
2986cb94db27SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_POSTREAD |
29875f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
29885f14ee23SPyun YongHyeon 	setup = 0;
29895f14ee23SPyun YongHyeon 	for (idx = sc->dc_cdata.dc_tx_cons; idx != sc->dc_cdata.dc_tx_prod;
29905f14ee23SPyun YongHyeon 	    DC_INC(idx, DC_TX_LIST_CNT), sc->dc_cdata.dc_tx_cnt--) {
29915f14ee23SPyun YongHyeon 		cur_tx = &sc->dc_ldata.dc_tx_list[idx];
2992af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
2993af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
299496f2e892SBill Paul 
299596f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
299696f2e892SBill Paul 			break;
299796f2e892SBill Paul 
29985f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_chain[idx] == NULL)
29995f14ee23SPyun YongHyeon 			continue;
30005f14ee23SPyun YongHyeon 
3001af4358c7SMaxime Henrion 		if (ctl & DC_TXCTL_SETUP) {
30025f14ee23SPyun YongHyeon 			cur_tx->dc_ctl = htole32(ctl & ~DC_TXCTL_SETUP);
30035f14ee23SPyun YongHyeon 			setup++;
30045f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
30055f14ee23SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
300696f2e892SBill Paul 			/*
300796f2e892SBill Paul 			 * Yes, the PNIC is so brain damaged
300896f2e892SBill Paul 			 * that it will sometimes generate a TX
300996f2e892SBill Paul 			 * underrun error while DMAing the RX
301096f2e892SBill Paul 			 * filter setup frame. If we detect this,
301196f2e892SBill Paul 			 * we have to send the setup frame again,
301296f2e892SBill Paul 			 * or else the filter won't be programmed
301396f2e892SBill Paul 			 * correctly.
301496f2e892SBill Paul 			 */
301596f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
301696f2e892SBill Paul 				if (txstat & DC_TXSTAT_ERRSUM)
301796f2e892SBill Paul 					dc_setfilt(sc);
301896f2e892SBill Paul 			}
301996f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
302096f2e892SBill Paul 			continue;
302196f2e892SBill Paul 		}
302296f2e892SBill Paul 
302329a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
3024feb78939SJonathan Chen 			/*
3025feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
3026feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
302729a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
302829a2220aSBill Paul 			 * Who knows, but Conexant chips have the
302929a2220aSBill Paul 			 * same problem. Maybe they took lessons
303029a2220aSBill Paul 			 * from Xircom.
303129a2220aSBill Paul 			 */
3032feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
3033feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
3034feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
3035feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
3036feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
3037feb78939SJonathan Chen 		} else {
303896f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
303996f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
304096f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
304196f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
304296f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
3043feb78939SJonathan Chen 		}
304496f2e892SBill Paul 
304596f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
3046c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
304796f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
3048c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
304996f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
3050c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
305196f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
3052*91f3f3fcSJustin Hibbits 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3053c8b27acaSJohn Baldwin 				dc_init_locked(sc);
305496f2e892SBill Paul 				return;
305596f2e892SBill Paul 			}
305652c43a47SPyun YongHyeon 		} else
3057c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
3058c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_COLLISIONS, (txstat & DC_TXSTAT_COLLCNT) >> 3);
305996f2e892SBill Paul 
30605f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
306156e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
30625f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
306396f2e892SBill Paul 		m_freem(sc->dc_cdata.dc_tx_chain[idx]);
306496f2e892SBill Paul 		sc->dc_cdata.dc_tx_chain[idx] = NULL;
306596f2e892SBill Paul 	}
306696f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
306782a67a70SMarius Strobl 
30685f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
3069*91f3f3fcSJustin Hibbits 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
30703e0e6726SMarius Strobl 		if (sc->dc_cdata.dc_tx_cnt == 0)
30713e0e6726SMarius Strobl 			sc->dc_wdog_timer = 0;
307296f2e892SBill Paul 	}
30735f14ee23SPyun YongHyeon 	if (setup > 0)
30745f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
30755f14ee23SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
30765f14ee23SPyun YongHyeon }
307796f2e892SBill Paul 
3078e3d2833aSAlfred Perlstein static void
dc_tick(void * xsc)30790934f18aSMaxime Henrion dc_tick(void *xsc)
308096f2e892SBill Paul {
308196f2e892SBill Paul 	struct dc_softc *sc;
308296f2e892SBill Paul 	struct mii_data *mii;
3083*91f3f3fcSJustin Hibbits 	if_t ifp;
3084ee320f98SPyun YongHyeon 	uint32_t r;
308596f2e892SBill Paul 
308696f2e892SBill Paul 	sc = xsc;
3087c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3088fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
308996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
309096f2e892SBill Paul 
309106d23883SPyun YongHyeon 	/*
309206d23883SPyun YongHyeon 	 * Reclaim transmitted frames for controllers that do
309306d23883SPyun YongHyeon 	 * not generate TX completion interrupt for every frame.
309406d23883SPyun YongHyeon 	 */
309506d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR)
309606d23883SPyun YongHyeon 		dc_txeof(sc);
309706d23883SPyun YongHyeon 
309896f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
3099318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
3100318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
3101318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3102318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
310396f2e892SBill Paul 				sc->dc_link = 0;
3104318b02fdSBill Paul 				mii_mediachg(mii);
3105318b02fdSBill Paul 			}
3106318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3107318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
3108318b02fdSBill Paul 				sc->dc_link = 0;
3109318b02fdSBill Paul 				mii_mediachg(mii);
3110318b02fdSBill Paul 			}
3111d675147eSBill Paul 			if (sc->dc_link == 0)
311296f2e892SBill Paul 				mii_tick(mii);
311396f2e892SBill Paul 		} else {
3114d0d67284SMarius Strobl 			/*
3115d0d67284SMarius Strobl 			 * For NICs which never report DC_RXSTATE_WAIT, we
3116d0d67284SMarius Strobl 			 * have to bite the bullet...
3117d0d67284SMarius Strobl 			 */
3118d0d67284SMarius Strobl 			if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
3119d0d67284SMarius Strobl 			    DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
3120d314ebf5SPyun YongHyeon 			    sc->dc_cdata.dc_tx_cnt == 0)
312196f2e892SBill Paul 				mii_tick(mii);
3122259b8d84SMartin Blapp 		}
312396f2e892SBill Paul 	} else
312496f2e892SBill Paul 		mii_tick(mii);
312596f2e892SBill Paul 
312696f2e892SBill Paul 	/*
312796f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
312896f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
312996f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
313096f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
313196f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
313296f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
313396f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
313496f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
313596f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
313696f2e892SBill Paul 	 * a screeching halt for several seconds.
313796f2e892SBill Paul 	 *
313896f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
313996f2e892SBill Paul 	 * any packets until a link has been established. After the
314096f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
314196f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
314296f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
314396f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
314496f2e892SBill Paul 	 */
3145*91f3f3fcSJustin Hibbits 	if (sc->dc_link != 0 && !if_sendq_empty(ifp))
3146c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
314796f2e892SBill Paul 
3148318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
3149b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3150318b02fdSBill Paul 	else
3151b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
315296f2e892SBill Paul }
315396f2e892SBill Paul 
3154d467c136SBill Paul /*
3155d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
3156d467c136SBill Paul  * or switch to store and forward mode if we have to.
3157d467c136SBill Paul  */
3158e3d2833aSAlfred Perlstein static void
dc_tx_underrun(struct dc_softc * sc)31590934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
3160d467c136SBill Paul {
3161d9efae03SPyun YongHyeon 	uint32_t netcfg, isr;
3162d9efae03SPyun YongHyeon 	int i, reinit;
3163d467c136SBill Paul 
3164d9efae03SPyun YongHyeon 	reinit = 0;
3165d9efae03SPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
3166d9efae03SPyun YongHyeon 	device_printf(sc->dc_dev, "TX underrun -- ");
3167d9efae03SPyun YongHyeon 	if ((sc->dc_flags & DC_TX_STORENFWD) == 0) {
3168d9efae03SPyun YongHyeon 		if (sc->dc_txthresh + DC_TXTHRESH_INC > DC_TXTHRESH_MAX) {
3169d9efae03SPyun YongHyeon 			printf("using store and forward mode\n");
3170d9efae03SPyun YongHyeon 			netcfg |= DC_NETCFG_STORENFWD;
3171d9efae03SPyun YongHyeon 		} else {
3172d9efae03SPyun YongHyeon 			printf("increasing TX threshold\n");
3173d9efae03SPyun YongHyeon 			sc->dc_txthresh += DC_TXTHRESH_INC;
3174d9efae03SPyun YongHyeon 			netcfg &= ~DC_NETCFG_TX_THRESH;
3175d9efae03SPyun YongHyeon 			netcfg |= sc->dc_txthresh;
31768f382a1fSPyun YongHyeon 		}
3177d467c136SBill Paul 
3178d467c136SBill Paul 		if (DC_IS_INTEL(sc)) {
3179d467c136SBill Paul 			/*
3180d467c136SBill Paul 			 * The real 21143 requires that the transmitter be idle
3181d467c136SBill Paul 			 * in order to change the transmit threshold or store
3182d467c136SBill Paul 			 * and forward state.
3183d467c136SBill Paul 			 */
3184d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg & ~DC_NETCFG_TX_ON);
3185d467c136SBill Paul 
3186d467c136SBill Paul 			for (i = 0; i < DC_TIMEOUT; i++) {
3187d467c136SBill Paul 				isr = CSR_READ_4(sc, DC_ISR);
3188d467c136SBill Paul 				if (isr & DC_ISR_TX_IDLE)
3189d467c136SBill Paul 					break;
3190d467c136SBill Paul 				DELAY(10);
3191d467c136SBill Paul 			}
3192d467c136SBill Paul 			if (i == DC_TIMEOUT) {
31936b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
3194432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
3195432120f2SMarius Strobl 				    __func__);
3196d9efae03SPyun YongHyeon 				reinit++;
3197d9efae03SPyun YongHyeon 			}
3198d9efae03SPyun YongHyeon 		}
3199d9efae03SPyun YongHyeon 	} else {
3200d9efae03SPyun YongHyeon 		printf("resetting\n");
3201d9efae03SPyun YongHyeon 		reinit++;
3202d9efae03SPyun YongHyeon 	}
3203d9efae03SPyun YongHyeon 
3204d9efae03SPyun YongHyeon 	if (reinit == 0) {
3205d9efae03SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, netcfg);
3206d9efae03SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3207d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg | DC_NETCFG_TX_ON);
3208d9efae03SPyun YongHyeon 	} else {
3209*91f3f3fcSJustin Hibbits 		if_setdrvflagbits(sc->dc_ifp, 0, IFF_DRV_RUNNING);
3210c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3211d467c136SBill Paul 	}
3212d467c136SBill Paul }
3213d467c136SBill Paul 
3214e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3215e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3216e4fc250cSLuigi Rizzo 
32171abcdbd1SAttilio Rao static int
dc_poll(if_t ifp,enum poll_cmd cmd,int count)3218*91f3f3fcSJustin Hibbits dc_poll(if_t ifp, enum poll_cmd cmd, int count)
3219e4fc250cSLuigi Rizzo {
3220*91f3f3fcSJustin Hibbits 	struct dc_softc *sc = if_getsoftc(ifp);
32211abcdbd1SAttilio Rao 	int rx_npkts = 0;
3222e4fc250cSLuigi Rizzo 
322340929967SGleb Smirnoff 	DC_LOCK(sc);
322440929967SGleb Smirnoff 
3225*91f3f3fcSJustin Hibbits 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
322640929967SGleb Smirnoff 		DC_UNLOCK(sc);
32271abcdbd1SAttilio Rao 		return (rx_npkts);
3228e4fc250cSLuigi Rizzo 	}
322940929967SGleb Smirnoff 
3230e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
32311abcdbd1SAttilio Rao 	rx_npkts = dc_rxeof(sc);
3232e4fc250cSLuigi Rizzo 	dc_txeof(sc);
3233*91f3f3fcSJustin Hibbits 	if (!if_sendq_empty(ifp) &&
3234*91f3f3fcSJustin Hibbits 	    !(if_getdrvflags(ifp) & IFF_DRV_OACTIVE))
3235c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3236e4fc250cSLuigi Rizzo 
3237e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3238ee320f98SPyun YongHyeon 		uint32_t	status;
3239e4fc250cSLuigi Rizzo 
3240e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3241e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3242e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3243e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
32445120abbfSSam Leffler 		if (!status) {
32455120abbfSSam Leffler 			DC_UNLOCK(sc);
32461abcdbd1SAttilio Rao 			return (rx_npkts);
32475120abbfSSam Leffler 		}
3248e4fc250cSLuigi Rizzo 		/* ack what we have */
3249e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3250e4fc250cSLuigi Rizzo 
3251e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3252ee320f98SPyun YongHyeon 			uint32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3253c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, (r & 0xffff) + ((r >> 17) & 0x7ff));
3254e4fc250cSLuigi Rizzo 
3255e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3256e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3257e4fc250cSLuigi Rizzo 		}
3258e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3259e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3260e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3261e4fc250cSLuigi Rizzo 
3262e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3263e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3264e4fc250cSLuigi Rizzo 
3265e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
32666b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
3267*91f3f3fcSJustin Hibbits 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3268c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3269e4fc250cSLuigi Rizzo 		}
3270e4fc250cSLuigi Rizzo 	}
32715120abbfSSam Leffler 	DC_UNLOCK(sc);
32721abcdbd1SAttilio Rao 	return (rx_npkts);
3273e4fc250cSLuigi Rizzo }
3274e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3275e4fc250cSLuigi Rizzo 
3276e3d2833aSAlfred Perlstein static void
dc_intr(void * arg)32770934f18aSMaxime Henrion dc_intr(void *arg)
327896f2e892SBill Paul {
327996f2e892SBill Paul 	struct dc_softc *sc;
3280*91f3f3fcSJustin Hibbits 	if_t ifp;
3281ee320f98SPyun YongHyeon 	uint32_t r, status;
3282848a02fcSPyun YongHyeon 	int n;
328396f2e892SBill Paul 
328496f2e892SBill Paul 	sc = arg;
3285d2a1864bSWarner Losh 
32860934f18aSMaxime Henrion 	if (sc->suspended)
3287e8388e14SMitsuru IWASAKI 		return;
3288e8388e14SMitsuru IWASAKI 
3289d1ce9105SBill Paul 	DC_LOCK(sc);
3290a84b4e80SPyun YongHyeon 	status = CSR_READ_4(sc, DC_ISR);
3291a84b4e80SPyun YongHyeon 	if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0) {
3292a84b4e80SPyun YongHyeon 		DC_UNLOCK(sc);
3293a84b4e80SPyun YongHyeon 		return;
3294a84b4e80SPyun YongHyeon 	}
3295fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3296e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3297*91f3f3fcSJustin Hibbits 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
329840929967SGleb Smirnoff 		DC_UNLOCK(sc);
329940929967SGleb Smirnoff 		return;
3300e4fc250cSLuigi Rizzo 	}
33010934f18aSMaxime Henrion #endif
330296f2e892SBill Paul 	/* Disable interrupts. */
330396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
330496f2e892SBill Paul 
3305a84b4e80SPyun YongHyeon 	for (n = 16; n > 0; n--) {
3306*91f3f3fcSJustin Hibbits 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
3307a84b4e80SPyun YongHyeon 			break;
3308a84b4e80SPyun YongHyeon 		/* Ack interrupts. */
330996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
331096f2e892SBill Paul 
331173bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
3312848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
331373bf949cSBill Paul 				while (dc_rx_resync(sc))
331473bf949cSBill Paul 					dc_rxeof(sc);
331573bf949cSBill Paul 			}
331673bf949cSBill Paul 		}
331796f2e892SBill Paul 
331896f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
331996f2e892SBill Paul 			dc_txeof(sc);
332096f2e892SBill Paul 
332196f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
332296f2e892SBill Paul 			dc_txeof(sc);
332396f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
332496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
332596f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
332696f2e892SBill Paul 			}
332796f2e892SBill Paul 		}
332896f2e892SBill Paul 
3329d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3330d467c136SBill Paul 			dc_tx_underrun(sc);
333196f2e892SBill Paul 
333296f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
333373bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
333426b40a65SPyun YongHyeon 			r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3335c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, (r & 0xffff) + ((r >> 17) & 0x7ff));
3336848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
333773bf949cSBill Paul 				while (dc_rx_resync(sc))
333873bf949cSBill Paul 					dc_rxeof(sc);
333973bf949cSBill Paul 			}
334073bf949cSBill Paul 		}
334196f2e892SBill Paul 
3342*91f3f3fcSJustin Hibbits 		if (!if_sendq_empty(ifp))
3343a84b4e80SPyun YongHyeon 			dc_start_locked(ifp);
3344a84b4e80SPyun YongHyeon 
334596f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
3346*91f3f3fcSJustin Hibbits 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3347c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3348a84b4e80SPyun YongHyeon 			DC_UNLOCK(sc);
3349a84b4e80SPyun YongHyeon 			return;
335096f2e892SBill Paul 		}
3351a84b4e80SPyun YongHyeon 		status = CSR_READ_4(sc, DC_ISR);
3352a84b4e80SPyun YongHyeon 		if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0)
3353a84b4e80SPyun YongHyeon 			break;
335496f2e892SBill Paul 	}
335596f2e892SBill Paul 
335696f2e892SBill Paul 	/* Re-enable interrupts. */
3357*91f3f3fcSJustin Hibbits 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
335896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
335996f2e892SBill Paul 
3360d1ce9105SBill Paul 	DC_UNLOCK(sc);
336196f2e892SBill Paul }
336296f2e892SBill Paul 
336396f2e892SBill Paul /*
336496f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
336596f2e892SBill Paul  * pointers to the fragment pointers.
336696f2e892SBill Paul  */
3367e3d2833aSAlfred Perlstein static int
dc_encap(struct dc_softc * sc,struct mbuf ** m_head)3368a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
336996f2e892SBill Paul {
3370ebc284ccSMarius Strobl 	bus_dma_segment_t segs[DC_MAXFRAGS];
33715f14ee23SPyun YongHyeon 	bus_dmamap_t map;
3372ebc284ccSMarius Strobl 	struct dc_desc *f;
337396f2e892SBill Paul 	struct mbuf *m;
3374993a741aSMarius Strobl 	int cur, defragged, error, first, frag, i, idx, nseg;
3375cda97c50SMike Silbersack 
3376993a741aSMarius Strobl 	m = NULL;
3377993a741aSMarius Strobl 	defragged = 0;
3378993a741aSMarius Strobl 	if (sc->dc_flags & DC_TX_COALESCE &&
3379993a741aSMarius Strobl 	    ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3380c6499eccSGleb Smirnoff 		m = m_defrag(*m_head, M_NOWAIT);
3381993a741aSMarius Strobl 		defragged = 1;
3382993a741aSMarius Strobl 	} else {
3383cda97c50SMike Silbersack 		/*
3384993a741aSMarius Strobl 		 * Count the number of frags in this chain to see if we
3385993a741aSMarius Strobl 		 * need to m_collapse.  Since the descriptor list is shared
3386993a741aSMarius Strobl 		 * by all packets, we'll m_collapse long chains so that they
3387cda97c50SMike Silbersack 		 * do not use up the entire list, even if they would fit.
3388cda97c50SMike Silbersack 		 */
3389993a741aSMarius Strobl 		i = 0;
3390a10c0e45SMike Silbersack 		for (m = *m_head; m != NULL; m = m->m_next)
3391993a741aSMarius Strobl 			i++;
3392993a741aSMarius Strobl 		if (i > DC_TX_LIST_CNT / 4 ||
3393993a741aSMarius Strobl 		    DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3394993a741aSMarius Strobl 		    DC_TX_LIST_RSVD) {
3395c6499eccSGleb Smirnoff 			m = m_collapse(*m_head, M_NOWAIT, DC_MAXFRAGS);
3396993a741aSMarius Strobl 			defragged = 1;
3397993a741aSMarius Strobl 		}
3398993a741aSMarius Strobl 	}
3399993a741aSMarius Strobl 	if (defragged != 0) {
340082a67a70SMarius Strobl 		if (m == NULL) {
340182a67a70SMarius Strobl 			m_freem(*m_head);
340282a67a70SMarius Strobl 			*m_head = NULL;
3403cda97c50SMike Silbersack 			return (ENOBUFS);
340482a67a70SMarius Strobl 		}
3405a10c0e45SMike Silbersack 		*m_head = m;
3406cda97c50SMike Silbersack 	}
3407993a741aSMarius Strobl 
340856e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
34095f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3410ebc284ccSMarius Strobl 	    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3411ebc284ccSMarius Strobl 	if (error == EFBIG) {
3412c6499eccSGleb Smirnoff 		if (defragged != 0 || (m = m_collapse(*m_head, M_NOWAIT,
3413993a741aSMarius Strobl 		    DC_MAXFRAGS)) == NULL) {
3414ebc284ccSMarius Strobl 			m_freem(*m_head);
341582a67a70SMarius Strobl 			*m_head = NULL;
3416993a741aSMarius Strobl 			return (defragged != 0 ? error : ENOBUFS);
341782a67a70SMarius Strobl 		}
3418ebc284ccSMarius Strobl 		*m_head = m;
34195f14ee23SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3420ebc284ccSMarius Strobl 		    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3421ebc284ccSMarius Strobl 		if (error != 0) {
3422ebc284ccSMarius Strobl 			m_freem(*m_head);
3423ebc284ccSMarius Strobl 			*m_head = NULL;
3424ebc284ccSMarius Strobl 			return (error);
342582a67a70SMarius Strobl 		}
3426ebc284ccSMarius Strobl 	} else if (error != 0)
3427ebc284ccSMarius Strobl 		return (error);
3428ebc284ccSMarius Strobl 	KASSERT(nseg <= DC_MAXFRAGS,
3429ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
3430ebc284ccSMarius Strobl 	if (nseg == 0) {
3431ebc284ccSMarius Strobl 		m_freem(*m_head);
3432ebc284ccSMarius Strobl 		*m_head = NULL;
3433ebc284ccSMarius Strobl 		return (EIO);
3434ebc284ccSMarius Strobl 	}
3435ebc284ccSMarius Strobl 
34365f14ee23SPyun YongHyeon 	/* Check descriptor overruns. */
34375f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt + nseg > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
34385f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
34395f14ee23SPyun YongHyeon 		return (ENOBUFS);
34405f14ee23SPyun YongHyeon 	}
34415f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
34425f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
34435f14ee23SPyun YongHyeon 
3444ebc284ccSMarius Strobl 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3445ebc284ccSMarius Strobl 	for (i = 0; i < nseg; i++) {
3446ebc284ccSMarius Strobl 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3447ebc284ccSMarius Strobl 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3448ebc284ccSMarius Strobl 		    (first != sc->dc_cdata.dc_tx_first)) {
34495f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_mtag,
3450ebc284ccSMarius Strobl 			    sc->dc_cdata.dc_tx_map[first]);
3451ebc284ccSMarius Strobl 			m_freem(*m_head);
3452ebc284ccSMarius Strobl 			*m_head = NULL;
3453ebc284ccSMarius Strobl 			return (ENOBUFS);
3454ebc284ccSMarius Strobl 		}
3455ebc284ccSMarius Strobl 
34565f14ee23SPyun YongHyeon 		f = &sc->dc_ldata.dc_tx_list[frag];
3457ebc284ccSMarius Strobl 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3458ebc284ccSMarius Strobl 		if (i == 0) {
3459ebc284ccSMarius Strobl 			f->dc_status = 0;
3460ebc284ccSMarius Strobl 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3461ebc284ccSMarius Strobl 		} else
3462ebc284ccSMarius Strobl 			f->dc_status = htole32(DC_TXSTAT_OWN);
34635f14ee23SPyun YongHyeon 		f->dc_data = htole32(DC_ADDR_LO(segs[i].ds_addr));
3464ebc284ccSMarius Strobl 		cur = frag;
3465ebc284ccSMarius Strobl 		DC_INC(frag, DC_TX_LIST_CNT);
3466ebc284ccSMarius Strobl 	}
3467ebc284ccSMarius Strobl 
3468ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_prod = frag;
3469ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_cnt += nseg;
3470ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_chain[cur] = *m_head;
34715f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3472ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
34735f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[first].dc_ctl |=
3474ebc284ccSMarius Strobl 		    htole32(DC_TXCTL_FINT);
3475ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
34765f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
347706d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR &&
347806d23883SPyun YongHyeon 	    ++sc->dc_cdata.dc_tx_pkts >= 8) {
347906d23883SPyun YongHyeon 		sc->dc_cdata.dc_tx_pkts = 0;
34805f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
348106d23883SPyun YongHyeon 	}
34825f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3483ebc284ccSMarius Strobl 
34845f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
34855f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
34865f14ee23SPyun YongHyeon 
34875f14ee23SPyun YongHyeon 	/*
34885f14ee23SPyun YongHyeon 	 * Swap the last and the first dmamaps to ensure the map for
34895f14ee23SPyun YongHyeon 	 * this transmission is placed at the last descriptor.
34905f14ee23SPyun YongHyeon 	 */
34915f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_tx_map[cur];
34925f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[cur] = sc->dc_cdata.dc_tx_map[first];
34935f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[first] = map;
34945f14ee23SPyun YongHyeon 
349596f2e892SBill Paul 	return (0);
349696f2e892SBill Paul }
349796f2e892SBill Paul 
3498e3d2833aSAlfred Perlstein static void
dc_start(if_t ifp)3499*91f3f3fcSJustin Hibbits dc_start(if_t ifp)
350096f2e892SBill Paul {
350196f2e892SBill Paul 	struct dc_softc *sc;
3502c8b27acaSJohn Baldwin 
3503*91f3f3fcSJustin Hibbits 	sc = if_getsoftc(ifp);
3504c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3505c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3506c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3507c8b27acaSJohn Baldwin }
3508c8b27acaSJohn Baldwin 
3509ebc284ccSMarius Strobl /*
3510ebc284ccSMarius Strobl  * Main transmit routine
3511ebc284ccSMarius Strobl  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3512ebc284ccSMarius Strobl  * regions directly in the transmit lists.  We also save a copy of the
3513ebc284ccSMarius Strobl  * pointers since the transmit list fragment pointers are physical
3514ebc284ccSMarius Strobl  * addresses.
3515ebc284ccSMarius Strobl  */
3516c8b27acaSJohn Baldwin static void
dc_start_locked(if_t ifp)3517*91f3f3fcSJustin Hibbits dc_start_locked(if_t ifp)
3518c8b27acaSJohn Baldwin {
3519c8b27acaSJohn Baldwin 	struct dc_softc *sc;
35205f14ee23SPyun YongHyeon 	struct mbuf *m_head;
35215f14ee23SPyun YongHyeon 	int queued;
352296f2e892SBill Paul 
3523*91f3f3fcSJustin Hibbits 	sc = if_getsoftc(ifp);
352496f2e892SBill Paul 
3525c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
352696f2e892SBill Paul 
3527*91f3f3fcSJustin Hibbits 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
352876d40c85SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->dc_link == 0)
3529d1ce9105SBill Paul 		return;
353096f2e892SBill Paul 
35315f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
353296f2e892SBill Paul 
3533*91f3f3fcSJustin Hibbits 	for (queued = 0; !if_sendq_empty(ifp); ) {
35345f14ee23SPyun YongHyeon 		/*
35355f14ee23SPyun YongHyeon 		 * If there's no way we can send any packets, return now.
35365f14ee23SPyun YongHyeon 		 */
35375f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
3538*91f3f3fcSJustin Hibbits 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
35395f14ee23SPyun YongHyeon 			break;
35405f14ee23SPyun YongHyeon 		}
3541*91f3f3fcSJustin Hibbits 		m_head = if_dequeue(ifp);
354296f2e892SBill Paul 		if (m_head == NULL)
354396f2e892SBill Paul 			break;
354496f2e892SBill Paul 
3545a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
354682a67a70SMarius Strobl 			if (m_head == NULL)
354782a67a70SMarius Strobl 				break;
3548*91f3f3fcSJustin Hibbits 			if_sendq_prepend(ifp, m_head);
3549*91f3f3fcSJustin Hibbits 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
355096f2e892SBill Paul 			break;
355196f2e892SBill Paul 		}
355296f2e892SBill Paul 
3553cbaf877fSBrian Feldman 		queued++;
355496f2e892SBill Paul 		/*
355596f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
355696f2e892SBill Paul 		 * to him.
355796f2e892SBill Paul 		 */
35589ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
355996f2e892SBill Paul 	}
356096f2e892SBill Paul 
3561cbaf877fSBrian Feldman 	if (queued > 0) {
356296f2e892SBill Paul 		/* Transmit */
356396f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
356496f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
356596f2e892SBill Paul 
356696f2e892SBill Paul 		/*
356796f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
356896f2e892SBill Paul 		 */
3569b1d16143SMarius Strobl 		sc->dc_wdog_timer = 5;
3570cbaf877fSBrian Feldman 	}
357196f2e892SBill Paul }
357296f2e892SBill Paul 
3573e3d2833aSAlfred Perlstein static void
dc_init(void * xsc)35740934f18aSMaxime Henrion dc_init(void *xsc)
357596f2e892SBill Paul {
357696f2e892SBill Paul 	struct dc_softc *sc = xsc;
3577c8b27acaSJohn Baldwin 
3578c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3579c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3580c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3581c8b27acaSJohn Baldwin }
3582c8b27acaSJohn Baldwin 
3583c8b27acaSJohn Baldwin static void
dc_init_locked(struct dc_softc * sc)3584c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3585c8b27acaSJohn Baldwin {
3586*91f3f3fcSJustin Hibbits 	if_t ifp = sc->dc_ifp;
358796f2e892SBill Paul 	struct mii_data *mii;
3588d314ebf5SPyun YongHyeon 	struct ifmedia *ifm;
358996f2e892SBill Paul 
3590c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
359196f2e892SBill Paul 
3592*91f3f3fcSJustin Hibbits 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
35938f382a1fSPyun YongHyeon 		return;
35948f382a1fSPyun YongHyeon 
359596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
359696f2e892SBill Paul 
359796f2e892SBill Paul 	/*
359896f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
359996f2e892SBill Paul 	 */
360096f2e892SBill Paul 	dc_stop(sc);
360196f2e892SBill Paul 	dc_reset(sc);
3602d314ebf5SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
3603d314ebf5SPyun YongHyeon 		ifm = &mii->mii_media;
3604d314ebf5SPyun YongHyeon 		dc_apply_fixup(sc, ifm->ifm_media);
3605d314ebf5SPyun YongHyeon 	}
360696f2e892SBill Paul 
360796f2e892SBill Paul 	/*
360896f2e892SBill Paul 	 * Set cache alignment and burst length.
360996f2e892SBill Paul 	 */
361052ca7ee2SPyun YongHyeon 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc) || DC_IS_ULI(sc))
361196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
361296f2e892SBill Paul 	else
361396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3614935fe010SLuigi Rizzo 	/*
3615935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3616935fe010SLuigi Rizzo 	 */
3617935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3618935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
361996f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
362096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
362196f2e892SBill Paul 	} else {
362296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
362396f2e892SBill Paul 	}
362496f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
362596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
362696f2e892SBill Paul 	switch(sc->dc_cachesize) {
362796f2e892SBill Paul 	case 32:
362896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
362996f2e892SBill Paul 		break;
363096f2e892SBill Paul 	case 16:
363196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
363296f2e892SBill Paul 		break;
363396f2e892SBill Paul 	case 8:
363496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
363596f2e892SBill Paul 		break;
363696f2e892SBill Paul 	case 0:
363796f2e892SBill Paul 	default:
363896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
363996f2e892SBill Paul 		break;
364096f2e892SBill Paul 	}
364196f2e892SBill Paul 
364296f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
364396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
364496f2e892SBill Paul 	else {
3645d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
364696f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
364796f2e892SBill Paul 		} else {
364896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
364996f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
365096f2e892SBill Paul 		}
365196f2e892SBill Paul 	}
365296f2e892SBill Paul 
365396f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
365496f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
365596f2e892SBill Paul 
365696f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
365796f2e892SBill Paul 		/*
365896f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
365996f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
366096f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
366196f2e892SBill Paul 		 * document the meaning of these bits so there's no way
366296f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
366396f2e892SBill Paul 		 * number all its own; the rest all use a different one.
366496f2e892SBill Paul 		 */
366596f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
366696f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
366796f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
366896f2e892SBill Paul 		else
366996f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
367096f2e892SBill Paul 	}
367196f2e892SBill Paul 
3672feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3673feb78939SJonathan Chen 		/*
3674feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3675feb78939SJonathan Chen 		 * can talk to the MII.
3676feb78939SJonathan Chen 		 */
3677feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3678feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3679feb78939SJonathan Chen 		DELAY(10);
3680feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3681feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3682feb78939SJonathan Chen 		DELAY(10);
3683feb78939SJonathan Chen 	}
3684feb78939SJonathan Chen 
368596f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3686d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
368796f2e892SBill Paul 
368896f2e892SBill Paul 	/* Init circular RX list. */
368996f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
36906b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
369122f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
369296f2e892SBill Paul 		dc_stop(sc);
369396f2e892SBill Paul 		return;
369496f2e892SBill Paul 	}
369596f2e892SBill Paul 
369696f2e892SBill Paul 	/*
369756e5e7aeSMaxime Henrion 	 * Init TX descriptors.
369896f2e892SBill Paul 	 */
369996f2e892SBill Paul 	dc_list_tx_init(sc);
370096f2e892SBill Paul 
370196f2e892SBill Paul 	/*
370296f2e892SBill Paul 	 * Load the address of the RX list.
370396f2e892SBill Paul 	 */
370456e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
370556e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
370696f2e892SBill Paul 
370796f2e892SBill Paul 	/*
370896f2e892SBill Paul 	 * Enable interrupts.
370996f2e892SBill Paul 	 */
3710e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3711e4fc250cSLuigi Rizzo 	/*
3712e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3713e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3714e4fc250cSLuigi Rizzo 	 * after a reset.
3715e4fc250cSLuigi Rizzo 	 */
3716*91f3f3fcSJustin Hibbits 	if (if_getcapenable(ifp) & IFCAP_POLLING)
3717e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3718e4fc250cSLuigi Rizzo 	else
3719e4fc250cSLuigi Rizzo #endif
372096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
372196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
372296f2e892SBill Paul 
372352ca7ee2SPyun YongHyeon 	/* Initialize TX jabber and RX watchdog timer. */
372452ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
372552ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_WATCHDOG, DC_WDOG_JABBERCLK |
372652ca7ee2SPyun YongHyeon 		    DC_WDOG_HOSTUNJAB);
372752ca7ee2SPyun YongHyeon 
372896f2e892SBill Paul 	/* Enable transmitter. */
372996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
373096f2e892SBill Paul 
373196f2e892SBill Paul 	/*
3732918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3733918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3734918434c8SBill Paul 	 * link and activity indications.
3735918434c8SBill Paul 	 */
373678999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3737918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3738918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
373978999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3740918434c8SBill Paul 	}
3741918434c8SBill Paul 
3742918434c8SBill Paul 	/*
374396f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
374496f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
374596f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
374696f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
374796f2e892SBill Paul 	 */
374896f2e892SBill Paul 	dc_setfilt(sc);
374996f2e892SBill Paul 
375096f2e892SBill Paul 	/* Enable receiver. */
375196f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
375296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
375396f2e892SBill Paul 
3754*91f3f3fcSJustin Hibbits 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
3755*91f3f3fcSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
375696f2e892SBill Paul 
3757d7e9ac75SPyun YongHyeon 	dc_ifmedia_upd_locked(sc);
3758d314ebf5SPyun YongHyeon 
375926b40a65SPyun YongHyeon 	/* Clear missed frames and overflow counter. */
376026b40a65SPyun YongHyeon 	CSR_READ_4(sc, DC_FRAMESDISCARDED);
376126b40a65SPyun YongHyeon 
3762857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
376345521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3764857fd445SBill Paul 		sc->dc_link = 1;
3765857fd445SBill Paul 	else {
3766318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3767b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3768318b02fdSBill Paul 		else
3769b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3770857fd445SBill Paul 	}
3771b1d16143SMarius Strobl 
3772b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
3773b1d16143SMarius Strobl 	callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
377496f2e892SBill Paul }
377596f2e892SBill Paul 
377696f2e892SBill Paul /*
377796f2e892SBill Paul  * Set media options.
377896f2e892SBill Paul  */
3779e3d2833aSAlfred Perlstein static int
dc_ifmedia_upd(if_t ifp)3780*91f3f3fcSJustin Hibbits dc_ifmedia_upd(if_t ifp)
378196f2e892SBill Paul {
378296f2e892SBill Paul 	struct dc_softc *sc;
3783d7e9ac75SPyun YongHyeon 	int error;
378496f2e892SBill Paul 
3785*91f3f3fcSJustin Hibbits 	sc = if_getsoftc(ifp);
3786c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3787d7e9ac75SPyun YongHyeon 	error = dc_ifmedia_upd_locked(sc);
3788d7e9ac75SPyun YongHyeon 	DC_UNLOCK(sc);
3789d7e9ac75SPyun YongHyeon 	return (error);
3790d7e9ac75SPyun YongHyeon }
3791f43d9309SBill Paul 
3792d7e9ac75SPyun YongHyeon static int
dc_ifmedia_upd_locked(struct dc_softc * sc)3793d7e9ac75SPyun YongHyeon dc_ifmedia_upd_locked(struct dc_softc *sc)
3794d7e9ac75SPyun YongHyeon {
3795d7e9ac75SPyun YongHyeon 	struct mii_data *mii;
3796d7e9ac75SPyun YongHyeon 	struct ifmedia *ifm;
3797d7e9ac75SPyun YongHyeon 	int error;
3798d7e9ac75SPyun YongHyeon 
3799d7e9ac75SPyun YongHyeon 	DC_LOCK_ASSERT(sc);
3800d7e9ac75SPyun YongHyeon 
3801d7e9ac75SPyun YongHyeon 	sc->dc_link = 0;
3802d7e9ac75SPyun YongHyeon 	mii = device_get_softc(sc->dc_miibus);
3803d7e9ac75SPyun YongHyeon 	error = mii_mediachg(mii);
3804d7e9ac75SPyun YongHyeon 	if (error == 0) {
3805d7e9ac75SPyun YongHyeon 		ifm = &mii->mii_media;
3806d314ebf5SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3807d314ebf5SPyun YongHyeon 			dc_setcfg(sc, ifm->ifm_media);
3808d314ebf5SPyun YongHyeon 		else if (DC_IS_DAVICOM(sc) &&
380945521525SPoul-Henning Kamp 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3810f43d9309SBill Paul 			dc_setcfg(sc, ifm->ifm_media);
3811d7e9ac75SPyun YongHyeon 	}
381296f2e892SBill Paul 
3813d7e9ac75SPyun YongHyeon 	return (error);
381496f2e892SBill Paul }
381596f2e892SBill Paul 
381696f2e892SBill Paul /*
381796f2e892SBill Paul  * Report current media status.
381896f2e892SBill Paul  */
3819e3d2833aSAlfred Perlstein static void
dc_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)3820*91f3f3fcSJustin Hibbits dc_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
382196f2e892SBill Paul {
382296f2e892SBill Paul 	struct dc_softc *sc;
382396f2e892SBill Paul 	struct mii_data *mii;
3824f43d9309SBill Paul 	struct ifmedia *ifm;
382596f2e892SBill Paul 
3826*91f3f3fcSJustin Hibbits 	sc = if_getsoftc(ifp);
382796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3828c8b27acaSJohn Baldwin 	DC_LOCK(sc);
382996f2e892SBill Paul 	mii_pollstat(mii);
3830f43d9309SBill Paul 	ifm = &mii->mii_media;
3831f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
383245521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3833f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3834f43d9309SBill Paul 			ifmr->ifm_status = 0;
3835432120f2SMarius Strobl 			DC_UNLOCK(sc);
3836f43d9309SBill Paul 			return;
3837f43d9309SBill Paul 		}
3838f43d9309SBill Paul 	}
383996f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
384096f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3841c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
384296f2e892SBill Paul }
384396f2e892SBill Paul 
3844e3d2833aSAlfred Perlstein static int
dc_ioctl(if_t ifp,u_long command,caddr_t data)3845*91f3f3fcSJustin Hibbits dc_ioctl(if_t ifp, u_long command, caddr_t data)
384696f2e892SBill Paul {
3847*91f3f3fcSJustin Hibbits 	struct dc_softc *sc = if_getsoftc(ifp);
384896f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
384996f2e892SBill Paul 	struct mii_data *mii;
3850d1ce9105SBill Paul 	int error = 0;
385196f2e892SBill Paul 
385296f2e892SBill Paul 	switch (command) {
385396f2e892SBill Paul 	case SIOCSIFFLAGS:
3854c8b27acaSJohn Baldwin 		DC_LOCK(sc);
3855*91f3f3fcSJustin Hibbits 		if (if_getflags(ifp) & IFF_UP) {
3856*91f3f3fcSJustin Hibbits 			int need_setfilt = (if_getflags(ifp) ^ sc->dc_if_flags) &
38575d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
38585d6dfbbbSLuigi Rizzo 
3859*91f3f3fcSJustin Hibbits 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
38605d6dfbbbSLuigi Rizzo 				if (need_setfilt)
386196f2e892SBill Paul 					dc_setfilt(sc);
38625d6dfbbbSLuigi Rizzo 			} else {
3863*91f3f3fcSJustin Hibbits 				if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3864c8b27acaSJohn Baldwin 				dc_init_locked(sc);
386596f2e892SBill Paul 			}
386696f2e892SBill Paul 		} else {
3867*91f3f3fcSJustin Hibbits 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
386896f2e892SBill Paul 				dc_stop(sc);
386996f2e892SBill Paul 		}
3870*91f3f3fcSJustin Hibbits 		sc->dc_if_flags = if_getflags(ifp);
3871c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
387296f2e892SBill Paul 		break;
387396f2e892SBill Paul 	case SIOCADDMULTI:
387496f2e892SBill Paul 	case SIOCDELMULTI:
3875c8b27acaSJohn Baldwin 		DC_LOCK(sc);
3876*91f3f3fcSJustin Hibbits 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
387796f2e892SBill Paul 			dc_setfilt(sc);
3878c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
387996f2e892SBill Paul 		break;
388096f2e892SBill Paul 	case SIOCGIFMEDIA:
388196f2e892SBill Paul 	case SIOCSIFMEDIA:
388296f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
388396f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
388496f2e892SBill Paul 		break;
3885e695984eSRuslan Ermilov 	case SIOCSIFCAP:
388640929967SGleb Smirnoff #ifdef DEVICE_POLLING
388740929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
3888*91f3f3fcSJustin Hibbits 		    !(if_getcapenable(ifp) & IFCAP_POLLING)) {
388940929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
389040929967SGleb Smirnoff 			if (error)
389140929967SGleb Smirnoff 				return(error);
3892c8b27acaSJohn Baldwin 			DC_LOCK(sc);
389340929967SGleb Smirnoff 			/* Disable interrupts */
389440929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3895*91f3f3fcSJustin Hibbits 			if_setcapenablebit(ifp, IFCAP_POLLING, 0);
3896c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
389740929967SGleb Smirnoff 			return (error);
389840929967SGleb Smirnoff 		}
389940929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
3900*91f3f3fcSJustin Hibbits 		    if_getcapenable(ifp) & IFCAP_POLLING) {
390140929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
390240929967SGleb Smirnoff 			/* Enable interrupts. */
390340929967SGleb Smirnoff 			DC_LOCK(sc);
390440929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
3905*91f3f3fcSJustin Hibbits 			if_setcapenablebit(ifp, 0, IFCAP_POLLING);
390640929967SGleb Smirnoff 			DC_UNLOCK(sc);
390740929967SGleb Smirnoff 			return (error);
390840929967SGleb Smirnoff 		}
390940929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3910e695984eSRuslan Ermilov 		break;
391196f2e892SBill Paul 	default:
39129ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
391396f2e892SBill Paul 		break;
391496f2e892SBill Paul 	}
391596f2e892SBill Paul 
391696f2e892SBill Paul 	return (error);
391796f2e892SBill Paul }
391896f2e892SBill Paul 
3919e3d2833aSAlfred Perlstein static void
dc_watchdog(void * xsc)3920b1d16143SMarius Strobl dc_watchdog(void *xsc)
392196f2e892SBill Paul {
3922b1d16143SMarius Strobl 	struct dc_softc *sc = xsc;
3923*91f3f3fcSJustin Hibbits 	if_t ifp;
392496f2e892SBill Paul 
3925b1d16143SMarius Strobl 	DC_LOCK_ASSERT(sc);
392696f2e892SBill Paul 
3927b1d16143SMarius Strobl 	if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3928b1d16143SMarius Strobl 		callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3929b1d16143SMarius Strobl 		return;
3930b1d16143SMarius Strobl 	}
3931d1ce9105SBill Paul 
3932b1d16143SMarius Strobl 	ifp = sc->dc_ifp;
3933c8dfaf38SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3934b1d16143SMarius Strobl 	device_printf(sc->dc_dev, "watchdog timeout\n");
393596f2e892SBill Paul 
3936*91f3f3fcSJustin Hibbits 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
3937c8b27acaSJohn Baldwin 	dc_init_locked(sc);
393896f2e892SBill Paul 
3939*91f3f3fcSJustin Hibbits 	if (!if_sendq_empty(ifp))
3940c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
394196f2e892SBill Paul }
394296f2e892SBill Paul 
394396f2e892SBill Paul /*
394496f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
394596f2e892SBill Paul  * RX and TX lists.
394696f2e892SBill Paul  */
3947e3d2833aSAlfred Perlstein static void
dc_stop(struct dc_softc * sc)39480934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
394996f2e892SBill Paul {
3950*91f3f3fcSJustin Hibbits 	if_t ifp;
3951b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3952b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3953b3811c95SMaxime Henrion 	int i;
39541da7683aSPyun YongHyeon 	uint32_t ctl, netcfg;
395596f2e892SBill Paul 
3956c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3957d1ce9105SBill Paul 
3958fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
39595f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
3960b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
396196f2e892SBill Paul 
3962b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
3963b1d16143SMarius Strobl 	callout_stop(&sc->dc_wdog_ch);
3964b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
39651da7683aSPyun YongHyeon 	sc->dc_link = 0;
396696f2e892SBill Paul 
3967*91f3f3fcSJustin Hibbits 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
39683b3ec200SPeter Wemm 
39691da7683aSPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
39701da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39711da7683aSPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
39721da7683aSPyun YongHyeon 		   netcfg & ~(DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
397396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
39741da7683aSPyun YongHyeon 	/* Wait the completion of TX/RX SM. */
39751da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39761da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
39771da7683aSPyun YongHyeon 
397896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
397996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
398096f2e892SBill Paul 
398196f2e892SBill Paul 	/*
398296f2e892SBill Paul 	 * Free data in the RX lists.
398396f2e892SBill Paul 	 */
398496f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3985b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
39865f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_rx_mtag,
39875f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i], BUS_DMASYNC_POSTREAD);
39885f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_mtag,
39895f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i]);
399056e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
399156e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
399296f2e892SBill Paul 		}
399396f2e892SBill Paul 	}
39945f14ee23SPyun YongHyeon 	bzero(ld->dc_rx_list, DC_RX_LIST_SZ);
39955f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
39965f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
399796f2e892SBill Paul 
399896f2e892SBill Paul 	/*
399996f2e892SBill Paul 	 * Free the TX list buffers.
400096f2e892SBill Paul 	 */
400196f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
4002b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
4003af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
40045f14ee23SPyun YongHyeon 			if (ctl & DC_TXCTL_SETUP) {
40055f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
40065f14ee23SPyun YongHyeon 				    BUS_DMASYNC_POSTWRITE);
40075f14ee23SPyun YongHyeon 			} else {
40085f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_tx_mtag,
40095f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i], BUS_DMASYNC_POSTWRITE);
40105f14ee23SPyun YongHyeon 				bus_dmamap_unload(sc->dc_tx_mtag,
40115f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i]);
401256e5e7aeSMaxime Henrion 				m_freem(cd->dc_tx_chain[i]);
40135f14ee23SPyun YongHyeon 			}
4014b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
401596f2e892SBill Paul 		}
401696f2e892SBill Paul 	}
40175f14ee23SPyun YongHyeon 	bzero(ld->dc_tx_list, DC_TX_LIST_SZ);
40185f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
40195f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
402096f2e892SBill Paul }
402196f2e892SBill Paul 
402296f2e892SBill Paul /*
4023e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
4024e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
4025e8388e14SMitsuru IWASAKI  * resume.
4026e8388e14SMitsuru IWASAKI  */
4027e3d2833aSAlfred Perlstein static int
dc_suspend(device_t dev)40280934f18aSMaxime Henrion dc_suspend(device_t dev)
4029e8388e14SMitsuru IWASAKI {
4030e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4031e8388e14SMitsuru IWASAKI 
4032e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4033c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4034e8388e14SMitsuru IWASAKI 	dc_stop(sc);
4035e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
4036c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4037e8388e14SMitsuru IWASAKI 
4038e8388e14SMitsuru IWASAKI 	return (0);
4039e8388e14SMitsuru IWASAKI }
4040e8388e14SMitsuru IWASAKI 
4041e8388e14SMitsuru IWASAKI /*
4042e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
4043e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
4044e8388e14SMitsuru IWASAKI  * appropriate.
4045e8388e14SMitsuru IWASAKI  */
4046e3d2833aSAlfred Perlstein static int
dc_resume(device_t dev)40470934f18aSMaxime Henrion dc_resume(device_t dev)
4048e8388e14SMitsuru IWASAKI {
4049e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4050*91f3f3fcSJustin Hibbits 	if_t ifp;
4051e8388e14SMitsuru IWASAKI 
4052e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4053fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
4054e8388e14SMitsuru IWASAKI 
4055e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
4056c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4057*91f3f3fcSJustin Hibbits 	if (if_getflags(ifp) & IFF_UP)
4058c8b27acaSJohn Baldwin 		dc_init_locked(sc);
4059e8388e14SMitsuru IWASAKI 
4060e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
4061c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4062e8388e14SMitsuru IWASAKI 
4063e8388e14SMitsuru IWASAKI 	return (0);
4064e8388e14SMitsuru IWASAKI }
4065e8388e14SMitsuru IWASAKI 
4066e8388e14SMitsuru IWASAKI /*
406796f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
406896f2e892SBill Paul  * get confused by errant DMAs when rebooting.
406996f2e892SBill Paul  */
40706a087a87SPyun YongHyeon static int
dc_shutdown(device_t dev)40710934f18aSMaxime Henrion dc_shutdown(device_t dev)
407296f2e892SBill Paul {
407396f2e892SBill Paul 	struct dc_softc *sc;
407496f2e892SBill Paul 
407596f2e892SBill Paul 	sc = device_get_softc(dev);
407696f2e892SBill Paul 
4077c8b27acaSJohn Baldwin 	DC_LOCK(sc);
407896f2e892SBill Paul 	dc_stop(sc);
4079c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
40806a087a87SPyun YongHyeon 
40816a087a87SPyun YongHyeon 	return (0);
408296f2e892SBill Paul }
408339d76ed6SPyun YongHyeon 
408439d76ed6SPyun YongHyeon static int
dc_check_multiport(struct dc_softc * sc)408539d76ed6SPyun YongHyeon dc_check_multiport(struct dc_softc *sc)
408639d76ed6SPyun YongHyeon {
408739d76ed6SPyun YongHyeon 	struct dc_softc *dsc;
408839d76ed6SPyun YongHyeon 	devclass_t dc;
408939d76ed6SPyun YongHyeon 	device_t child;
409039d76ed6SPyun YongHyeon 	uint8_t *eaddr;
409139d76ed6SPyun YongHyeon 	int unit;
409239d76ed6SPyun YongHyeon 
409339d76ed6SPyun YongHyeon 	dc = devclass_find("dc");
409439d76ed6SPyun YongHyeon 	for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
409539d76ed6SPyun YongHyeon 		child = devclass_get_device(dc, unit);
409639d76ed6SPyun YongHyeon 		if (child == NULL)
409739d76ed6SPyun YongHyeon 			continue;
409839d76ed6SPyun YongHyeon 		if (child == sc->dc_dev)
409939d76ed6SPyun YongHyeon 			continue;
410039d76ed6SPyun YongHyeon 		if (device_get_parent(child) != device_get_parent(sc->dc_dev))
410139d76ed6SPyun YongHyeon 			continue;
410239d76ed6SPyun YongHyeon 		if (unit > device_get_unit(sc->dc_dev))
410339d76ed6SPyun YongHyeon 			continue;
4104b289c607SPyun YongHyeon 		if (device_is_attached(child) == 0)
4105b289c607SPyun YongHyeon 			continue;
410639d76ed6SPyun YongHyeon 		dsc = device_get_softc(child);
4107b289c607SPyun YongHyeon 		device_printf(sc->dc_dev,
4108b289c607SPyun YongHyeon 		    "Using station address of %s as base\n",
410939d76ed6SPyun YongHyeon 		    device_get_nameunit(child));
411039d76ed6SPyun YongHyeon 		bcopy(dsc->dc_eaddr, sc->dc_eaddr, ETHER_ADDR_LEN);
411139d76ed6SPyun YongHyeon 		eaddr = (uint8_t *)sc->dc_eaddr;
411239d76ed6SPyun YongHyeon 		eaddr[5]++;
4113b289c607SPyun YongHyeon 		/* Prepare SROM to parse again. */
4114b289c607SPyun YongHyeon 		if (DC_IS_INTEL(sc) && dsc->dc_srom != NULL &&
4115b289c607SPyun YongHyeon 		    sc->dc_romwidth != 0) {
4116b289c607SPyun YongHyeon 			free(sc->dc_srom, M_DEVBUF);
4117b289c607SPyun YongHyeon 			sc->dc_romwidth = dsc->dc_romwidth;
4118b289c607SPyun YongHyeon 			sc->dc_srom = malloc(DC_ROM_SIZE(sc->dc_romwidth),
4119b289c607SPyun YongHyeon 			    M_DEVBUF, M_NOWAIT);
4120b289c607SPyun YongHyeon 			if (sc->dc_srom == NULL) {
4121b289c607SPyun YongHyeon 				device_printf(sc->dc_dev,
4122b289c607SPyun YongHyeon 				    "Could not allocate SROM buffer\n");
4123b289c607SPyun YongHyeon 				return (ENOMEM);
4124b289c607SPyun YongHyeon 			}
4125b289c607SPyun YongHyeon 			bcopy(dsc->dc_srom, sc->dc_srom,
4126b289c607SPyun YongHyeon 			    DC_ROM_SIZE(sc->dc_romwidth));
4127b289c607SPyun YongHyeon 		}
412839d76ed6SPyun YongHyeon 		return (0);
412939d76ed6SPyun YongHyeon 	}
413039d76ed6SPyun YongHyeon 	return (ENOENT);
413139d76ed6SPyun YongHyeon }
4132