xref: /freebsd/sys/dev/dc/if_dc.c (revision 89b2411bc3b8d8efe9e4337974dcd029d639cf38)
160727d8bSWarner Losh /*-
296f2e892SBill Paul  * Copyright (c) 1997, 1998, 1999
396f2e892SBill Paul  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
496f2e892SBill Paul  *
596f2e892SBill Paul  * Redistribution and use in source and binary forms, with or without
696f2e892SBill Paul  * modification, are permitted provided that the following conditions
796f2e892SBill Paul  * are met:
896f2e892SBill Paul  * 1. Redistributions of source code must retain the above copyright
996f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer.
1096f2e892SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1196f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1296f2e892SBill Paul  *    documentation and/or other materials provided with the distribution.
1396f2e892SBill Paul  * 3. All advertising materials mentioning features or use of this software
1496f2e892SBill Paul  *    must display the following acknowledgement:
1596f2e892SBill Paul  *	This product includes software developed by Bill Paul.
1696f2e892SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1796f2e892SBill Paul  *    may be used to endorse or promote products derived from this software
1896f2e892SBill Paul  *    without specific prior written permission.
1996f2e892SBill Paul  *
2096f2e892SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2196f2e892SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2296f2e892SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2396f2e892SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2496f2e892SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2596f2e892SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2696f2e892SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2796f2e892SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2896f2e892SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2996f2e892SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3096f2e892SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3196f2e892SBill Paul  */
3296f2e892SBill Paul 
334dc52c32SDavid E. O'Brien #include <sys/cdefs.h>
344dc52c32SDavid E. O'Brien __FBSDID("$FreeBSD$");
354dc52c32SDavid E. O'Brien 
3696f2e892SBill Paul /*
3796f2e892SBill Paul  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
3896f2e892SBill Paul  * series chips and several workalikes including the following:
3996f2e892SBill Paul  *
40ead7cde9SBill Paul  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
4196f2e892SBill Paul  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
4296f2e892SBill Paul  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
4396f2e892SBill Paul  * ASIX Electronics AX88140A (www.asix.com.tw)
4496f2e892SBill Paul  * ASIX Electronics AX88141 (www.asix.com.tw)
4596f2e892SBill Paul  * ADMtek AL981 (www.admtek.com.tw)
46593a1aeaSMartin Blapp  * ADMtek AN983 (www.admtek.com.tw)
47a2d61e43SWarner Losh  * ADMtek CardBus AN985 (www.admtek.com.tw)
48a2d61e43SWarner Losh  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek CardBus AN985
4988d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
509ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
51feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
521d5e5310SBill Paul  * Abocom FE2500
531af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
547eac366bSMartin Blapp  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
5596f2e892SBill Paul  *
5696f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5796f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5896f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5996f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
6096f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
6196f2e892SBill Paul  * instead of 512.
6296f2e892SBill Paul  *
6396f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
6496f2e892SBill Paul  * Electrical Engineering Department
6596f2e892SBill Paul  * Columbia University, New York City
6696f2e892SBill Paul  */
6796f2e892SBill Paul /*
6896f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6996f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
7096f2e892SBill Paul  * three kinds of media attachments:
7196f2e892SBill Paul  *
7296f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
7396f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7496f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7596f2e892SBill Paul  * o 10baseT port.
7696f2e892SBill Paul  * o AUI/BNC port.
7796f2e892SBill Paul  *
7896f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7996f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
8096f2e892SBill Paul  * autosensing configuration.
8196f2e892SBill Paul  *
8296f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
8396f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8496f2e892SBill Paul  * handled separately due to its different register offsets and the
8596f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8696f2e892SBill Paul  * here, but I'm not thrilled about it.
8796f2e892SBill Paul  *
8896f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8996f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
9096f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
9196f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
9296f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
9396f2e892SBill Paul  */
9496f2e892SBill Paul 
95f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
96f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
97f0796cd2SGleb Smirnoff #endif
98f0796cd2SGleb Smirnoff 
9996f2e892SBill Paul #include <sys/param.h>
100af4358c7SMaxime Henrion #include <sys/endian.h>
10196f2e892SBill Paul #include <sys/systm.h>
10296f2e892SBill Paul #include <sys/sockio.h>
10396f2e892SBill Paul #include <sys/mbuf.h>
10496f2e892SBill Paul #include <sys/malloc.h>
10596f2e892SBill Paul #include <sys/kernel.h>
106f11d01c3SPoul-Henning Kamp #include <sys/module.h>
10796f2e892SBill Paul #include <sys/socket.h>
10896f2e892SBill Paul 
10996f2e892SBill Paul #include <net/if.h>
11096f2e892SBill Paul #include <net/if_arp.h>
11196f2e892SBill Paul #include <net/ethernet.h>
11296f2e892SBill Paul #include <net/if_dl.h>
11396f2e892SBill Paul #include <net/if_media.h>
114db40c1aeSDoug Ambrisko #include <net/if_types.h>
115db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
11696f2e892SBill Paul 
11796f2e892SBill Paul #include <net/bpf.h>
11896f2e892SBill Paul 
11996f2e892SBill Paul #include <machine/bus.h>
12096f2e892SBill Paul #include <machine/resource.h>
12196f2e892SBill Paul #include <sys/bus.h>
12296f2e892SBill Paul #include <sys/rman.h>
12396f2e892SBill Paul 
12496f2e892SBill Paul #include <dev/mii/mii.h>
1258c1093fcSMarius Strobl #include <dev/mii/mii_bitbang.h>
12696f2e892SBill Paul #include <dev/mii/miivar.h>
12796f2e892SBill Paul 
12819b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
12919b7ffd1SWarner Losh #include <dev/pci/pcivar.h>
13096f2e892SBill Paul 
13196f2e892SBill Paul #define	DC_USEIOSPACE
13296f2e892SBill Paul 
1336a3033a8SWarner Losh #include <dev/dc/if_dcreg.h>
13496f2e892SBill Paul 
135ec6a7299SMaxime Henrion #ifdef __sparc64__
136ec6a7299SMaxime Henrion #include <dev/ofw/openfirm.h>
137ec6a7299SMaxime Henrion #include <machine/ofw_machdep.h>
138ec6a7299SMaxime Henrion #endif
139ec6a7299SMaxime Henrion 
140f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, pci, 1, 1, 1);
141f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, ether, 1, 1, 1);
14295a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
14395a16455SPeter Wemm 
144919ccba7SWarner Losh /*
145919ccba7SWarner Losh  * "device miibus" is required in kernel config.  See GENERIC if you get
146919ccba7SWarner Losh  * errors here.
147919ccba7SWarner Losh  */
14896f2e892SBill Paul #include "miibus_if.h"
14996f2e892SBill Paul 
15096f2e892SBill Paul /*
15196f2e892SBill Paul  * Various supported device vendors/types and their names.
15296f2e892SBill Paul  */
1538c1093fcSMarius Strobl static const struct dc_type const dc_devs[] = {
1541e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143), 0,
15596f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
1561e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009), 0,
15738deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
1581e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100), 0,
15996f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
1601e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), DC_REVISION_DM9102A,
16188d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
1621e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), 0,
1631e2e70b1SJohn Baldwin 		"Davicom DM9102 10/100BaseTX" },
1641e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981), 0,
16596f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
166593a1aeaSMartin Blapp 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983), 0,
167593a1aeaSMartin Blapp 		"ADMtek AN983 10/100BaseTX" },
1681e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
169a2d61e43SWarner Losh 		"ADMtek AN985 CardBus 10/100BaseTX or clone" },
1701e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
171e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
1721e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
173e351d778SMartin Blapp 		"ADMtek ADM9513 10/100BaseTX" },
1741e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), DC_REVISION_88141,
17596f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
1761e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), 0,
1771e2e70b1SJohn Baldwin 		"ASIX AX88140A 10/100BaseTX" },
1781e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), DC_REVISION_98713A,
17996f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
1801e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), 0,
1811e2e70b1SJohn Baldwin 		"Macronix 98713 10/100BaseTX" },
1821e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), DC_REVISION_98713A,
18396f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1841e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), 0,
18596f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1861e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98725,
18796f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
1881e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98715AEC_C,
1891e2e70b1SJohn Baldwin 		"Macronix 98715AEC-C 10/100BaseTX" },
1901e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), 0,
1911e2e70b1SJohn Baldwin 		"Macronix 98715/98715A 10/100BaseTX" },
1921e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727), 0,
193ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
1941e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115), 0,
19596f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
1961e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), DC_REVISION_82C169,
19796f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1981e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), 0,
1991e2e70b1SJohn Baldwin 		"82c168 PNIC 10/100BaseTX" },
2001e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217), 0,
2019ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
2021e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242), 0,
203fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
2041e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201), 0,
205feb78939SJonathan Chen 		"Xircom X3201 10/100BaseTX" },
2061e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD), 0,
2079be0993cSJohn Baldwin 		"Neteasy DRP-32TXD Cardbus 10/100" },
2081e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500), 0,
2091d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
2101e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX), 0,
211773c505fSMIHIRA Sanpei Yoshiro 		"Abocom FE2500MX 10/100BaseTX" },
2121e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112), 0,
2131af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
2141e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX), 0,
215948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
2161e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T), 0,
21797f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2181e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB), 0,
2197eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
2201e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120), 0,
221e7b9ab3aSBill Paul 		"Microsoft MN-120 CardBus 10/100" },
2221e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130), 0,
223e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
22417762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08), 0,
22517762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22617762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09), 0,
22717762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22852ca7ee2SPyun YongHyeon 	{ DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261), 0,
22952ca7ee2SPyun YongHyeon 		"ULi M5261 FastEthernet" },
23052ca7ee2SPyun YongHyeon 	{ DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263), 0,
23152ca7ee2SPyun YongHyeon 		"ULi M5263 FastEthernet" },
23296f2e892SBill Paul 	{ 0, 0, NULL }
23396f2e892SBill Paul };
23496f2e892SBill Paul 
235e51a25f8SAlfred Perlstein static int dc_probe(device_t);
236e51a25f8SAlfred Perlstein static int dc_attach(device_t);
237e51a25f8SAlfred Perlstein static int dc_detach(device_t);
238e8388e14SMitsuru IWASAKI static int dc_suspend(device_t);
239e8388e14SMitsuru IWASAKI static int dc_resume(device_t);
240ebc284ccSMarius Strobl static const struct dc_type *dc_devtype(device_t);
2415f14ee23SPyun YongHyeon static void dc_discard_rxbuf(struct dc_softc *, int);
2425f14ee23SPyun YongHyeon static int dc_newbuf(struct dc_softc *, int);
243a10c0e45SMike Silbersack static int dc_encap(struct dc_softc *, struct mbuf **);
244e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war(struct dc_softc *, int);
245e51a25f8SAlfred Perlstein static int dc_rx_resync(struct dc_softc *);
2461abcdbd1SAttilio Rao static int dc_rxeof(struct dc_softc *);
247e51a25f8SAlfred Perlstein static void dc_txeof(struct dc_softc *);
248e51a25f8SAlfred Perlstein static void dc_tick(void *);
249e51a25f8SAlfred Perlstein static void dc_tx_underrun(struct dc_softc *);
250e51a25f8SAlfred Perlstein static void dc_intr(void *);
251e51a25f8SAlfred Perlstein static void dc_start(struct ifnet *);
252c8b27acaSJohn Baldwin static void dc_start_locked(struct ifnet *);
253e51a25f8SAlfred Perlstein static int dc_ioctl(struct ifnet *, u_long, caddr_t);
254e51a25f8SAlfred Perlstein static void dc_init(void *);
255c8b27acaSJohn Baldwin static void dc_init_locked(struct dc_softc *);
256e51a25f8SAlfred Perlstein static void dc_stop(struct dc_softc *);
257b1d16143SMarius Strobl static void dc_watchdog(void *);
2586a087a87SPyun YongHyeon static int dc_shutdown(device_t);
259e51a25f8SAlfred Perlstein static int dc_ifmedia_upd(struct ifnet *);
260d7e9ac75SPyun YongHyeon static int dc_ifmedia_upd_locked(struct dc_softc *);
261e51a25f8SAlfred Perlstein static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
26296f2e892SBill Paul 
2635f14ee23SPyun YongHyeon static int dc_dma_alloc(struct dc_softc *);
2645f14ee23SPyun YongHyeon static void dc_dma_free(struct dc_softc *);
2655f14ee23SPyun YongHyeon static void dc_dma_map_addr(void *, bus_dma_segment_t *, int, int);
2665f14ee23SPyun YongHyeon 
267e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
268e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
269e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
270ee320f98SPyun YongHyeon static void dc_eeprom_getword(struct dc_softc *, int, uint16_t *);
271ee320f98SPyun YongHyeon static void dc_eeprom_getword_pnic(struct dc_softc *, int, uint16_t *);
272ee320f98SPyun YongHyeon static void dc_eeprom_getword_xircom(struct dc_softc *, int, uint16_t *);
2733097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
274e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
27596f2e892SBill Paul 
276e51a25f8SAlfred Perlstein static int dc_miibus_readreg(device_t, int, int);
277e51a25f8SAlfred Perlstein static int dc_miibus_writereg(device_t, int, int, int);
278e51a25f8SAlfred Perlstein static void dc_miibus_statchg(device_t);
279e51a25f8SAlfred Perlstein static void dc_miibus_mediainit(device_t);
28096f2e892SBill Paul 
281e51a25f8SAlfred Perlstein static void dc_setcfg(struct dc_softc *, int);
2821da7683aSPyun YongHyeon static void dc_netcfg_wait(struct dc_softc *);
2833373489bSWarner Losh static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
2843373489bSWarner Losh static uint32_t dc_mchash_be(const uint8_t *);
285e51a25f8SAlfred Perlstein static void dc_setfilt_21143(struct dc_softc *);
286e51a25f8SAlfred Perlstein static void dc_setfilt_asix(struct dc_softc *);
287e51a25f8SAlfred Perlstein static void dc_setfilt_admtek(struct dc_softc *);
28852ca7ee2SPyun YongHyeon static void dc_setfilt_uli(struct dc_softc *);
289e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
29096f2e892SBill Paul 
291e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
29296f2e892SBill Paul 
293e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
294e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
295e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
29696f2e892SBill Paul 
297abe4e865SPyun YongHyeon static int dc_read_srom(struct dc_softc *, int);
298abe4e865SPyun YongHyeon static int dc_parse_21143_srom(struct dc_softc *);
299abe4e865SPyun YongHyeon static int dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
300abe4e865SPyun YongHyeon static int dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
301abe4e865SPyun YongHyeon static int dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
302e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
30339d76ed6SPyun YongHyeon static int dc_check_multiport(struct dc_softc *);
3045c1cfac4SBill Paul 
3058c1093fcSMarius Strobl /*
3068c1093fcSMarius Strobl  * MII bit-bang glue
3078c1093fcSMarius Strobl  */
3088c1093fcSMarius Strobl static uint32_t dc_mii_bitbang_read(device_t);
3098c1093fcSMarius Strobl static void dc_mii_bitbang_write(device_t, uint32_t);
3108c1093fcSMarius Strobl 
3118c1093fcSMarius Strobl static const struct mii_bitbang_ops dc_mii_bitbang_ops = {
3128c1093fcSMarius Strobl 	dc_mii_bitbang_read,
3138c1093fcSMarius Strobl 	dc_mii_bitbang_write,
3148c1093fcSMarius Strobl 	{
3158c1093fcSMarius Strobl 		DC_SIO_MII_DATAOUT,	/* MII_BIT_MDO */
3168c1093fcSMarius Strobl 		DC_SIO_MII_DATAIN,	/* MII_BIT_MDI */
3178c1093fcSMarius Strobl 		DC_SIO_MII_CLK,		/* MII_BIT_MDC */
3188c1093fcSMarius Strobl 		0,			/* MII_BIT_DIR_HOST_PHY */
3198c1093fcSMarius Strobl 		DC_SIO_MII_DIR,		/* MII_BIT_DIR_PHY_HOST */
3208c1093fcSMarius Strobl 	}
3218c1093fcSMarius Strobl };
3228c1093fcSMarius Strobl 
32396f2e892SBill Paul #ifdef DC_USEIOSPACE
32496f2e892SBill Paul #define	DC_RES			SYS_RES_IOPORT
32596f2e892SBill Paul #define	DC_RID			DC_PCI_CFBIO
32696f2e892SBill Paul #else
32796f2e892SBill Paul #define	DC_RES			SYS_RES_MEMORY
32896f2e892SBill Paul #define	DC_RID			DC_PCI_CFBMA
32996f2e892SBill Paul #endif
33096f2e892SBill Paul 
33196f2e892SBill Paul static device_method_t dc_methods[] = {
33296f2e892SBill Paul 	/* Device interface */
33396f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
33496f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
33596f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
336e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
337e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
33896f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
33996f2e892SBill Paul 
34096f2e892SBill Paul 	/* bus interface */
34196f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
34296f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
34396f2e892SBill Paul 
34496f2e892SBill Paul 	/* MII interface */
34596f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
34696f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
34796f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
348f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
34996f2e892SBill Paul 
35096f2e892SBill Paul 	{ 0, 0 }
35196f2e892SBill Paul };
35296f2e892SBill Paul 
35396f2e892SBill Paul static driver_t dc_driver = {
35496f2e892SBill Paul 	"dc",
35596f2e892SBill Paul 	dc_methods,
35696f2e892SBill Paul 	sizeof(struct dc_softc)
35796f2e892SBill Paul };
35896f2e892SBill Paul 
35996f2e892SBill Paul static devclass_t dc_devclass;
36096f2e892SBill Paul 
361f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
36296f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
36396f2e892SBill Paul 
36496f2e892SBill Paul #define	DC_SETBIT(sc, reg, x)				\
36596f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
36696f2e892SBill Paul 
36796f2e892SBill Paul #define	DC_CLRBIT(sc, reg, x)				\
36896f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
36996f2e892SBill Paul 
37096f2e892SBill Paul #define	SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
37196f2e892SBill Paul #define	SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
37296f2e892SBill Paul 
373e3d2833aSAlfred Perlstein static void
3740934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
37596f2e892SBill Paul {
37696f2e892SBill Paul 	int idx;
37796f2e892SBill Paul 
37896f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
37996f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
38096f2e892SBill Paul }
38196f2e892SBill Paul 
3822c876e15SPoul-Henning Kamp static void
3830934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3843097aa70SWarner Losh {
3853097aa70SWarner Losh 	int i;
3863097aa70SWarner Losh 
3873097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3883097aa70SWarner Losh 	dc_eeprom_idle(sc);
3893097aa70SWarner Losh 
3903097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3913097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3923097aa70SWarner Losh 	dc_delay(sc);
3933097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3943097aa70SWarner Losh 	dc_delay(sc);
3953097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3963097aa70SWarner Losh 	dc_delay(sc);
3973097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3983097aa70SWarner Losh 	dc_delay(sc);
3993097aa70SWarner Losh 
4003097aa70SWarner Losh 	for (i = 3; i--;) {
4013097aa70SWarner Losh 		if (6 & (1 << i))
4023097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4033097aa70SWarner Losh 		else
4043097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4053097aa70SWarner Losh 		dc_delay(sc);
4063097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4073097aa70SWarner Losh 		dc_delay(sc);
4083097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4093097aa70SWarner Losh 		dc_delay(sc);
4103097aa70SWarner Losh 	}
4113097aa70SWarner Losh 
4123097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
4133097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4143097aa70SWarner Losh 		dc_delay(sc);
4153097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
4163097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4173097aa70SWarner Losh 			dc_delay(sc);
4183097aa70SWarner Losh 			break;
4193097aa70SWarner Losh 		}
4203097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4213097aa70SWarner Losh 		dc_delay(sc);
4223097aa70SWarner Losh 	}
4233097aa70SWarner Losh 
4243097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4253097aa70SWarner Losh 	dc_eeprom_idle(sc);
4263097aa70SWarner Losh 
4273097aa70SWarner Losh 	if (i < 4 || i > 12)
4283097aa70SWarner Losh 		sc->dc_romwidth = 6;
4293097aa70SWarner Losh 	else
4303097aa70SWarner Losh 		sc->dc_romwidth = i;
4313097aa70SWarner Losh 
4323097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4333097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4343097aa70SWarner Losh 	dc_delay(sc);
4353097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4363097aa70SWarner Losh 	dc_delay(sc);
4373097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4383097aa70SWarner Losh 	dc_delay(sc);
4393097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4403097aa70SWarner Losh 	dc_delay(sc);
4413097aa70SWarner Losh 
4423097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4433097aa70SWarner Losh 	dc_eeprom_idle(sc);
4443097aa70SWarner Losh }
4453097aa70SWarner Losh 
446e3d2833aSAlfred Perlstein static void
4470934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
44896f2e892SBill Paul {
4490934f18aSMaxime Henrion 	int i;
45096f2e892SBill Paul 
45196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
45296f2e892SBill Paul 	dc_delay(sc);
45396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
45496f2e892SBill Paul 	dc_delay(sc);
45596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45696f2e892SBill Paul 	dc_delay(sc);
45796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
45896f2e892SBill Paul 	dc_delay(sc);
45996f2e892SBill Paul 
46096f2e892SBill Paul 	for (i = 0; i < 25; i++) {
46196f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46296f2e892SBill Paul 		dc_delay(sc);
46396f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46496f2e892SBill Paul 		dc_delay(sc);
46596f2e892SBill Paul 	}
46696f2e892SBill Paul 
46796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46896f2e892SBill Paul 	dc_delay(sc);
46996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
47096f2e892SBill Paul 	dc_delay(sc);
47196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
47296f2e892SBill Paul }
47396f2e892SBill Paul 
47496f2e892SBill Paul /*
47596f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
47696f2e892SBill Paul  */
477e3d2833aSAlfred Perlstein static void
4780934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
47996f2e892SBill Paul {
4800934f18aSMaxime Henrion 	int d, i;
48196f2e892SBill Paul 
4823097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4833097aa70SWarner Losh 	for (i = 3; i--; ) {
4843097aa70SWarner Losh 		if (d & (1 << i))
4853097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
48696f2e892SBill Paul 		else
4873097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4883097aa70SWarner Losh 		dc_delay(sc);
4893097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4903097aa70SWarner Losh 		dc_delay(sc);
4913097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4923097aa70SWarner Losh 		dc_delay(sc);
4933097aa70SWarner Losh 	}
49496f2e892SBill Paul 
49596f2e892SBill Paul 	/*
49696f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
49796f2e892SBill Paul 	 */
4983097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4993097aa70SWarner Losh 		if (addr & (1 << i)) {
50096f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
50196f2e892SBill Paul 		} else {
50296f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
50396f2e892SBill Paul 		}
50496f2e892SBill Paul 		dc_delay(sc);
50596f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
50696f2e892SBill Paul 		dc_delay(sc);
50796f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
50896f2e892SBill Paul 		dc_delay(sc);
50996f2e892SBill Paul 	}
51096f2e892SBill Paul }
51196f2e892SBill Paul 
51296f2e892SBill Paul /*
51396f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
51496f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
51596f2e892SBill Paul  * the EEPROM.
51696f2e892SBill Paul  */
517e3d2833aSAlfred Perlstein static void
518ee320f98SPyun YongHyeon dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, uint16_t *dest)
51996f2e892SBill Paul {
5200934f18aSMaxime Henrion 	int i;
521ee320f98SPyun YongHyeon 	uint32_t r;
52296f2e892SBill Paul 
52396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
52496f2e892SBill Paul 
52596f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
52696f2e892SBill Paul 		DELAY(1);
52796f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
52896f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
529ee320f98SPyun YongHyeon 			*dest = (uint16_t)(r & 0xFFFF);
53096f2e892SBill Paul 			return;
53196f2e892SBill Paul 		}
53296f2e892SBill Paul 	}
53396f2e892SBill Paul }
53496f2e892SBill Paul 
53596f2e892SBill Paul /*
53696f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
537feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
538feb78939SJonathan Chen  * the EEPROM, too.
539feb78939SJonathan Chen  */
540e3d2833aSAlfred Perlstein static void
541ee320f98SPyun YongHyeon dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, uint16_t *dest)
542feb78939SJonathan Chen {
5430934f18aSMaxime Henrion 
544feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
545feb78939SJonathan Chen 
546feb78939SJonathan Chen 	addr *= 2;
547feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
548ee320f98SPyun YongHyeon 	*dest = (uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
549feb78939SJonathan Chen 	addr += 1;
550feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
551ee320f98SPyun YongHyeon 	*dest |= ((uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
552feb78939SJonathan Chen 
553feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
554feb78939SJonathan Chen }
555feb78939SJonathan Chen 
556feb78939SJonathan Chen /*
557feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
55896f2e892SBill Paul  */
559e3d2833aSAlfred Perlstein static void
560ee320f98SPyun YongHyeon dc_eeprom_getword(struct dc_softc *sc, int addr, uint16_t *dest)
56196f2e892SBill Paul {
5620934f18aSMaxime Henrion 	int i;
563ee320f98SPyun YongHyeon 	uint16_t word = 0;
56496f2e892SBill Paul 
56596f2e892SBill Paul 	/* Force EEPROM to idle state. */
56696f2e892SBill Paul 	dc_eeprom_idle(sc);
56796f2e892SBill Paul 
56896f2e892SBill Paul 	/* Enter EEPROM access mode. */
56996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
57096f2e892SBill Paul 	dc_delay(sc);
57196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
57296f2e892SBill Paul 	dc_delay(sc);
57396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
57496f2e892SBill Paul 	dc_delay(sc);
57596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
57696f2e892SBill Paul 	dc_delay(sc);
57796f2e892SBill Paul 
57896f2e892SBill Paul 	/*
57996f2e892SBill Paul 	 * Send address of word we want to read.
58096f2e892SBill Paul 	 */
58196f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
58296f2e892SBill Paul 
58396f2e892SBill Paul 	/*
58496f2e892SBill Paul 	 * Start reading bits from EEPROM.
58596f2e892SBill Paul 	 */
58696f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
58796f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
58896f2e892SBill Paul 		dc_delay(sc);
58996f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
59096f2e892SBill Paul 			word |= i;
59196f2e892SBill Paul 		dc_delay(sc);
59296f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
59396f2e892SBill Paul 		dc_delay(sc);
59496f2e892SBill Paul 	}
59596f2e892SBill Paul 
59696f2e892SBill Paul 	/* Turn off EEPROM access mode. */
59796f2e892SBill Paul 	dc_eeprom_idle(sc);
59896f2e892SBill Paul 
59996f2e892SBill Paul 	*dest = word;
60096f2e892SBill Paul }
60196f2e892SBill Paul 
60296f2e892SBill Paul /*
60396f2e892SBill Paul  * Read a sequence of words from the EEPROM.
60496f2e892SBill Paul  */
605e3d2833aSAlfred Perlstein static void
6068c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
60796f2e892SBill Paul {
60896f2e892SBill Paul 	int i;
609ee320f98SPyun YongHyeon 	uint16_t word = 0, *ptr;
61096f2e892SBill Paul 
61196f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
61296f2e892SBill Paul 		if (DC_IS_PNIC(sc))
61396f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
614feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
615feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
61696f2e892SBill Paul 		else
61796f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
618ee320f98SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
6198c7ff1f3SMaxime Henrion 		if (be)
6208c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
62196f2e892SBill Paul 		else
6228c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
62396f2e892SBill Paul 	}
62496f2e892SBill Paul }
62596f2e892SBill Paul 
62696f2e892SBill Paul /*
6278c1093fcSMarius Strobl  * Write the MII serial port for the MII bit-bang module.
62896f2e892SBill Paul  */
629e3d2833aSAlfred Perlstein static void
6308c1093fcSMarius Strobl dc_mii_bitbang_write(device_t dev, uint32_t val)
63196f2e892SBill Paul {
6328c1093fcSMarius Strobl 	struct dc_softc *sc;
6330934f18aSMaxime Henrion 
6348c1093fcSMarius Strobl 	sc = device_get_softc(dev);
63596f2e892SBill Paul 
6368c1093fcSMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, val);
63715578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
63815578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
63996f2e892SBill Paul }
64096f2e892SBill Paul 
64196f2e892SBill Paul /*
6428c1093fcSMarius Strobl  * Read the MII serial port for the MII bit-bang module.
64396f2e892SBill Paul  */
6448c1093fcSMarius Strobl static uint32_t
6458c1093fcSMarius Strobl dc_mii_bitbang_read(device_t dev)
64696f2e892SBill Paul {
6478c1093fcSMarius Strobl 	struct dc_softc *sc;
6488c1093fcSMarius Strobl 	uint32_t val;
6490934f18aSMaxime Henrion 
6508c1093fcSMarius Strobl 	sc = device_get_softc(dev);
6518c1093fcSMarius Strobl 
6528c1093fcSMarius Strobl 	val = CSR_READ_4(sc, DC_SIO);
65315578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
65415578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
65596f2e892SBill Paul 
6568c1093fcSMarius Strobl 	return (val);
65796f2e892SBill Paul }
65896f2e892SBill Paul 
659e3d2833aSAlfred Perlstein static int
6600934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
66196f2e892SBill Paul {
66296f2e892SBill Paul 	struct dc_softc *sc;
663c85c4667SBill Paul 	int i, rval, phy_reg = 0;
66496f2e892SBill Paul 
66596f2e892SBill Paul 	sc = device_get_softc(dev);
66696f2e892SBill Paul 
6675c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
66896f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
66996f2e892SBill Paul 			switch (reg) {
67096f2e892SBill Paul 			case MII_BMSR:
67196f2e892SBill Paul 			/*
67296f2e892SBill Paul 			 * Fake something to make the probe
67396f2e892SBill Paul 			 * code think there's a PHY here.
67496f2e892SBill Paul 			 */
67596f2e892SBill Paul 				return (BMSR_MEDIAMASK);
67696f2e892SBill Paul 				break;
67796f2e892SBill Paul 			case MII_PHYIDR1:
67896f2e892SBill Paul 				if (DC_IS_PNIC(sc))
67996f2e892SBill Paul 					return (DC_VENDORID_LO);
68096f2e892SBill Paul 				return (DC_VENDORID_DEC);
68196f2e892SBill Paul 				break;
68296f2e892SBill Paul 			case MII_PHYIDR2:
68396f2e892SBill Paul 				if (DC_IS_PNIC(sc))
68496f2e892SBill Paul 					return (DC_DEVICEID_82C168);
68596f2e892SBill Paul 				return (DC_DEVICEID_21143);
68696f2e892SBill Paul 				break;
68796f2e892SBill Paul 			default:
68896f2e892SBill Paul 				return (0);
68996f2e892SBill Paul 				break;
69096f2e892SBill Paul 			}
69196f2e892SBill Paul 		} else
69296f2e892SBill Paul 			return (0);
69396f2e892SBill Paul 	}
69496f2e892SBill Paul 
69596f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
69696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
69796f2e892SBill Paul 		    (phy << 23) | (reg << 18));
69896f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
69996f2e892SBill Paul 			DELAY(1);
70096f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
70196f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
70296f2e892SBill Paul 				rval &= 0xFFFF;
70396f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
70496f2e892SBill Paul 			}
70596f2e892SBill Paul 		}
70696f2e892SBill Paul 		return (0);
70796f2e892SBill Paul 	}
70896f2e892SBill Paul 
70952ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
71052ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
71152ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
71252ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
71352ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_READ);
71452ca7ee2SPyun YongHyeon 		for (i = 0; i < DC_TIMEOUT; i++) {
71552ca7ee2SPyun YongHyeon 			DELAY(1);
71652ca7ee2SPyun YongHyeon 			rval = CSR_READ_4(sc, DC_ROM);
71752ca7ee2SPyun YongHyeon 			if ((rval & DC_ULI_PHY_OP_DONE) != 0) {
71852ca7ee2SPyun YongHyeon 				return (rval & DC_ULI_PHY_DATA_MASK);
71952ca7ee2SPyun YongHyeon 			}
72052ca7ee2SPyun YongHyeon 		}
72152ca7ee2SPyun YongHyeon 		if (i == DC_TIMEOUT)
72252ca7ee2SPyun YongHyeon 			device_printf(dev, "phy read timed out\n");
72352ca7ee2SPyun YongHyeon 		return (0);
72452ca7ee2SPyun YongHyeon 	}
72552ca7ee2SPyun YongHyeon 
72696f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
72796f2e892SBill Paul 		switch (reg) {
72896f2e892SBill Paul 		case MII_BMCR:
72996f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
73096f2e892SBill Paul 			break;
73196f2e892SBill Paul 		case MII_BMSR:
73296f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
73396f2e892SBill Paul 			break;
73496f2e892SBill Paul 		case MII_PHYIDR1:
73596f2e892SBill Paul 			phy_reg = DC_AL_VENID;
73696f2e892SBill Paul 			break;
73796f2e892SBill Paul 		case MII_PHYIDR2:
73896f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
73996f2e892SBill Paul 			break;
74096f2e892SBill Paul 		case MII_ANAR:
74196f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
74296f2e892SBill Paul 			break;
74396f2e892SBill Paul 		case MII_ANLPAR:
74496f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
74596f2e892SBill Paul 			break;
74696f2e892SBill Paul 		case MII_ANER:
74796f2e892SBill Paul 			phy_reg = DC_AL_ANER;
74896f2e892SBill Paul 			break;
74996f2e892SBill Paul 		default:
75022f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
75122f6205dSJohn Baldwin 			    reg);
75296f2e892SBill Paul 			return (0);
75396f2e892SBill Paul 			break;
75496f2e892SBill Paul 		}
75596f2e892SBill Paul 
75696f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
75796f2e892SBill Paul 		if (rval == 0xFFFF)
75896f2e892SBill Paul 			return (0);
75996f2e892SBill Paul 		return (rval);
76096f2e892SBill Paul 	}
76196f2e892SBill Paul 
762419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
763f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
764f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
765419146d9SBill Paul 	}
7668c1093fcSMarius Strobl 	rval = mii_bitbang_readreg(dev, &dc_mii_bitbang_ops, phy, reg);
767419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
768f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
76996f2e892SBill Paul 
7708c1093fcSMarius Strobl 	return (rval);
77196f2e892SBill Paul }
77296f2e892SBill Paul 
773e3d2833aSAlfred Perlstein static int
7740934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
77596f2e892SBill Paul {
77696f2e892SBill Paul 	struct dc_softc *sc;
777c85c4667SBill Paul 	int i, phy_reg = 0;
77896f2e892SBill Paul 
77996f2e892SBill Paul 	sc = device_get_softc(dev);
78096f2e892SBill Paul 
78196f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
78296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
78396f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
78496f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
78596f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
78696f2e892SBill Paul 				break;
78796f2e892SBill Paul 		}
78896f2e892SBill Paul 		return (0);
78996f2e892SBill Paul 	}
79096f2e892SBill Paul 
79152ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
79252ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
79352ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
79452ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
79552ca7ee2SPyun YongHyeon 		    ((data << DC_ULI_PHY_DATA_SHIFT) & DC_ULI_PHY_DATA_MASK) |
79652ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_WRITE);
79752ca7ee2SPyun YongHyeon 		DELAY(1);
79852ca7ee2SPyun YongHyeon 		return (0);
79952ca7ee2SPyun YongHyeon 	}
80052ca7ee2SPyun YongHyeon 
80196f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
80296f2e892SBill Paul 		switch (reg) {
80396f2e892SBill Paul 		case MII_BMCR:
80496f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
80596f2e892SBill Paul 			break;
80696f2e892SBill Paul 		case MII_BMSR:
80796f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
80896f2e892SBill Paul 			break;
80996f2e892SBill Paul 		case MII_PHYIDR1:
81096f2e892SBill Paul 			phy_reg = DC_AL_VENID;
81196f2e892SBill Paul 			break;
81296f2e892SBill Paul 		case MII_PHYIDR2:
81396f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
81496f2e892SBill Paul 			break;
81596f2e892SBill Paul 		case MII_ANAR:
81696f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
81796f2e892SBill Paul 			break;
81896f2e892SBill Paul 		case MII_ANLPAR:
81996f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
82096f2e892SBill Paul 			break;
82196f2e892SBill Paul 		case MII_ANER:
82296f2e892SBill Paul 			phy_reg = DC_AL_ANER;
82396f2e892SBill Paul 			break;
82496f2e892SBill Paul 		default:
82522f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
82622f6205dSJohn Baldwin 			    reg);
82796f2e892SBill Paul 			return (0);
82896f2e892SBill Paul 			break;
82996f2e892SBill Paul 		}
83096f2e892SBill Paul 
83196f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
83296f2e892SBill Paul 		return (0);
83396f2e892SBill Paul 	}
83496f2e892SBill Paul 
835419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
836f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
837f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
838419146d9SBill Paul 	}
8398c1093fcSMarius Strobl 	mii_bitbang_writereg(dev, &dc_mii_bitbang_ops, phy, reg, data);
840419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
841f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
84296f2e892SBill Paul 
84396f2e892SBill Paul 	return (0);
84496f2e892SBill Paul }
84596f2e892SBill Paul 
846e3d2833aSAlfred Perlstein static void
8470934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
84896f2e892SBill Paul {
84996f2e892SBill Paul 	struct dc_softc *sc;
850d314ebf5SPyun YongHyeon 	struct ifnet *ifp;
85196f2e892SBill Paul 	struct mii_data *mii;
852f43d9309SBill Paul 	struct ifmedia *ifm;
85396f2e892SBill Paul 
85496f2e892SBill Paul 	sc = device_get_softc(dev);
8555c1cfac4SBill Paul 
85696f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
857d314ebf5SPyun YongHyeon 	ifp = sc->dc_ifp;
858d314ebf5SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
859d314ebf5SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
860d314ebf5SPyun YongHyeon 		return;
861d314ebf5SPyun YongHyeon 
862f43d9309SBill Paul 	ifm = &mii->mii_media;
863*89b2411bSPyun YongHyeon 	if (DC_IS_DAVICOM(sc) && IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
864f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
865d314ebf5SPyun YongHyeon 		return;
866*89b2411bSPyun YongHyeon 	} else if (!DC_IS_ADMTEK(sc))
867*89b2411bSPyun YongHyeon 		dc_setcfg(sc, mii->mii_media_active);
868d314ebf5SPyun YongHyeon 
869d314ebf5SPyun YongHyeon 	sc->dc_link = 0;
870d314ebf5SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
871d314ebf5SPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
872d314ebf5SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
873d314ebf5SPyun YongHyeon 		case IFM_10_T:
874d314ebf5SPyun YongHyeon 		case IFM_100_TX:
875d314ebf5SPyun YongHyeon 			sc->dc_link = 1;
876d314ebf5SPyun YongHyeon 			break;
877d314ebf5SPyun YongHyeon 		}
878d314ebf5SPyun YongHyeon 	}
879f43d9309SBill Paul }
880f43d9309SBill Paul 
881f43d9309SBill Paul /*
882f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
883f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
884f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
885f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
886f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
887f43d9309SBill Paul  * with it itself. *sigh*
888f43d9309SBill Paul  */
889e3d2833aSAlfred Perlstein static void
8900934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
891f43d9309SBill Paul {
892f43d9309SBill Paul 	struct dc_softc *sc;
893f43d9309SBill Paul 	struct mii_data *mii;
894f43d9309SBill Paul 	struct ifmedia *ifm;
895f43d9309SBill Paul 	int rev;
896f43d9309SBill Paul 
8971e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
898f43d9309SBill Paul 
899f43d9309SBill Paul 	sc = device_get_softc(dev);
900f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
901f43d9309SBill Paul 	ifm = &mii->mii_media;
902f43d9309SBill Paul 
903f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
90445521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
90596f2e892SBill Paul }
90696f2e892SBill Paul 
90779d11e09SBill Paul #define	DC_BITS_512	9
90879d11e09SBill Paul #define	DC_BITS_128	7
90979d11e09SBill Paul #define	DC_BITS_64	6
91096f2e892SBill Paul 
9113373489bSWarner Losh static uint32_t
9123373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
91396f2e892SBill Paul {
9143373489bSWarner Losh 	uint32_t crc;
91596f2e892SBill Paul 
91696f2e892SBill Paul 	/* Compute CRC for the address value. */
9170e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
91896f2e892SBill Paul 
91979d11e09SBill Paul 	/*
92079d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
92179d11e09SBill Paul 	 * chips is only 128 bits wide.
92279d11e09SBill Paul 	 */
92379d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
92479d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
92596f2e892SBill Paul 
92679d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
92779d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
92879d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
92979d11e09SBill Paul 
930feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
931feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
932feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
933feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
9340934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
935feb78939SJonathan Chen 		else
9360934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
9370934f18aSMaxime Henrion 			    (12 << 4));
938feb78939SJonathan Chen 	}
939feb78939SJonathan Chen 
94079d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
94196f2e892SBill Paul }
94296f2e892SBill Paul 
94396f2e892SBill Paul /*
94496f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
94596f2e892SBill Paul  */
9463373489bSWarner Losh static uint32_t
9473373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
94896f2e892SBill Paul {
9490e939c0cSChristian Weisgerber 	uint32_t crc;
95096f2e892SBill Paul 
95196f2e892SBill Paul 	/* Compute CRC for the address value. */
9520e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
95396f2e892SBill Paul 
9540934f18aSMaxime Henrion 	/* Return the filter bit position. */
95596f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
95696f2e892SBill Paul }
95796f2e892SBill Paul 
95896f2e892SBill Paul /*
95996f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
96096f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
96196f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
96296f2e892SBill Paul  *
96396f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
96496f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
96596f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
96696f2e892SBill Paul  * we need that too.
96796f2e892SBill Paul  */
9682c876e15SPoul-Henning Kamp static void
9690934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
97096f2e892SBill Paul {
9718df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
97296f2e892SBill Paul 	struct dc_desc *sframe;
973ee320f98SPyun YongHyeon 	uint32_t h, *sp;
97496f2e892SBill Paul 	struct ifmultiaddr *ifma;
97596f2e892SBill Paul 	struct ifnet *ifp;
97696f2e892SBill Paul 	int i;
97796f2e892SBill Paul 
978fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
97996f2e892SBill Paul 
98096f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
98196f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
98296f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
9835f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
98456e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
9850934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
98696f2e892SBill Paul 
9875f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
988af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
989af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
99096f2e892SBill Paul 
99156e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
99296f2e892SBill Paul 
99396f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
99496f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
99596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99696f2e892SBill Paul 	else
99796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99896f2e892SBill Paul 
99996f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
100096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100196f2e892SBill Paul 	else
100296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100396f2e892SBill Paul 
1004eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
10056817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
100696f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100796f2e892SBill Paul 			continue;
1008aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
100996f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1010af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
101196f2e892SBill Paul 	}
1012eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
101396f2e892SBill Paul 
101496f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1015aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1016af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
101796f2e892SBill Paul 	}
101896f2e892SBill Paul 
10198df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
10208df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10218df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
10228df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
10238df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
102496f2e892SBill Paul 
1025af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
10268c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
10278c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
10285f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
102996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
103096f2e892SBill Paul 
103196f2e892SBill Paul 	/*
103296f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
103396f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
103496f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
103596f2e892SBill Paul 	 * medicine.
103696f2e892SBill Paul 	 */
103796f2e892SBill Paul 	DELAY(10000);
103896f2e892SBill Paul 
1039b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
104096f2e892SBill Paul }
104196f2e892SBill Paul 
10422c876e15SPoul-Henning Kamp static void
10430934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
104496f2e892SBill Paul {
10452e3d4b79SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
104696f2e892SBill Paul 	struct ifnet *ifp;
10470934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
104896f2e892SBill Paul 	int h = 0;
1049ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
105096f2e892SBill Paul 
1051fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
105296f2e892SBill Paul 
10530934f18aSMaxime Henrion 	/* Init our MAC address. */
10548df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10552e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
10562e3d4b79SPyun YongHyeon 	    eaddr[1] << 8 | eaddr[0]);
10572e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
105896f2e892SBill Paul 
105996f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
106096f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
106196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
106296f2e892SBill Paul 	else
106396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
106496f2e892SBill Paul 
106596f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
106696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106796f2e892SBill Paul 	else
106896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106996f2e892SBill Paul 
10700934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
107196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
107296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
107396f2e892SBill Paul 
107496f2e892SBill Paul 	/*
107596f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
107696f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
107796f2e892SBill Paul 	 */
107896f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
107996f2e892SBill Paul 		return;
108096f2e892SBill Paul 
10810934f18aSMaxime Henrion 	/* Now program new ones. */
1082eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
10836817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
108496f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
108596f2e892SBill Paul 			continue;
1086acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1087aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1088aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1089acc1bcccSMartin Blapp 		else
1090aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1091aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
109296f2e892SBill Paul 		if (h < 32)
109396f2e892SBill Paul 			hashes[0] |= (1 << h);
109496f2e892SBill Paul 		else
109596f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
109696f2e892SBill Paul 	}
1097eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
109896f2e892SBill Paul 
109996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
110096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
110196f2e892SBill Paul }
110296f2e892SBill Paul 
11032c876e15SPoul-Henning Kamp static void
11040934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
110596f2e892SBill Paul {
11068df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
110796f2e892SBill Paul 	struct ifnet *ifp;
11080934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
110996f2e892SBill Paul 	int h = 0;
1110ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
111196f2e892SBill Paul 
1112fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
111396f2e892SBill Paul 
11148df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
11158df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
111696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
11178df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
111896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
11198df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
112096f2e892SBill Paul 
112196f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
112296f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
112396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112496f2e892SBill Paul 	else
112596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112696f2e892SBill Paul 
112796f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
112896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112996f2e892SBill Paul 	else
113096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
113196f2e892SBill Paul 
113296f2e892SBill Paul 	/*
113396f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
113496f2e892SBill Paul 	 * of broadcast frames.
113596f2e892SBill Paul 	 */
113696f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
113796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113896f2e892SBill Paul 	else
113996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
114096f2e892SBill Paul 
114196f2e892SBill Paul 	/* first, zot all the existing hash bits */
114296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
114396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
114596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114696f2e892SBill Paul 
114796f2e892SBill Paul 	/*
114896f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
114996f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
115096f2e892SBill Paul 	 */
115196f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
115296f2e892SBill Paul 		return;
115396f2e892SBill Paul 
115496f2e892SBill Paul 	/* now program new ones */
1155eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
11566817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
115796f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
115896f2e892SBill Paul 			continue;
1159aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
116096f2e892SBill Paul 		if (h < 32)
116196f2e892SBill Paul 			hashes[0] |= (1 << h);
116296f2e892SBill Paul 		else
116396f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
116496f2e892SBill Paul 	}
1165eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
116696f2e892SBill Paul 
116796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
116896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
116996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
117096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
117196f2e892SBill Paul }
117296f2e892SBill Paul 
11732c876e15SPoul-Henning Kamp static void
117452ca7ee2SPyun YongHyeon dc_setfilt_uli(struct dc_softc *sc)
117552ca7ee2SPyun YongHyeon {
117652ca7ee2SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
117752ca7ee2SPyun YongHyeon 	struct ifnet *ifp;
117852ca7ee2SPyun YongHyeon 	struct ifmultiaddr *ifma;
117952ca7ee2SPyun YongHyeon 	struct dc_desc *sframe;
118052ca7ee2SPyun YongHyeon 	uint32_t filter, *sp;
118152ca7ee2SPyun YongHyeon 	uint8_t *ma;
118252ca7ee2SPyun YongHyeon 	int i, mcnt;
118352ca7ee2SPyun YongHyeon 
118452ca7ee2SPyun YongHyeon 	ifp = sc->dc_ifp;
118552ca7ee2SPyun YongHyeon 
118652ca7ee2SPyun YongHyeon 	i = sc->dc_cdata.dc_tx_prod;
118752ca7ee2SPyun YongHyeon 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
118852ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_cnt++;
118952ca7ee2SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
119052ca7ee2SPyun YongHyeon 	sp = sc->dc_cdata.dc_sbuf;
119152ca7ee2SPyun YongHyeon 	bzero(sp, DC_SFRAME_LEN);
119252ca7ee2SPyun YongHyeon 
119352ca7ee2SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
119452ca7ee2SPyun YongHyeon 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
119552ca7ee2SPyun YongHyeon 	    DC_TXCTL_TLINK | DC_FILTER_PERFECT | DC_TXCTL_FINT);
119652ca7ee2SPyun YongHyeon 
119752ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
119852ca7ee2SPyun YongHyeon 
119952ca7ee2SPyun YongHyeon 	/* Set station address. */
120052ca7ee2SPyun YongHyeon 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
120152ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[1] << 8 | eaddr[0]);
120252ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[3] << 8 | eaddr[2]);
120352ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[5] << 8 | eaddr[4]);
120452ca7ee2SPyun YongHyeon 
120552ca7ee2SPyun YongHyeon 	/* Set broadcast address. */
120652ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120752ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120852ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120952ca7ee2SPyun YongHyeon 
121052ca7ee2SPyun YongHyeon 	/* Extract current filter configuration. */
121152ca7ee2SPyun YongHyeon 	filter = CSR_READ_4(sc, DC_NETCFG);
121252ca7ee2SPyun YongHyeon 	filter &= ~(DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI);
121352ca7ee2SPyun YongHyeon 
121452ca7ee2SPyun YongHyeon 	/* Now build perfect filters. */
121552ca7ee2SPyun YongHyeon 	mcnt = 0;
121652ca7ee2SPyun YongHyeon 	if_maddr_rlock(ifp);
121752ca7ee2SPyun YongHyeon 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
121852ca7ee2SPyun YongHyeon 		if (ifma->ifma_addr->sa_family != AF_LINK)
121952ca7ee2SPyun YongHyeon 			continue;
122052ca7ee2SPyun YongHyeon 		if (mcnt >= DC_ULI_FILTER_NPERF) {
122152ca7ee2SPyun YongHyeon 			filter |= DC_NETCFG_RX_ALLMULTI;
122252ca7ee2SPyun YongHyeon 			break;
122352ca7ee2SPyun YongHyeon 		}
122452ca7ee2SPyun YongHyeon 		ma = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
122552ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[1] << 8 | ma[0]);
122652ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[3] << 8 | ma[2]);
122752ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[5] << 8 | ma[4]);
122852ca7ee2SPyun YongHyeon 		mcnt++;
122952ca7ee2SPyun YongHyeon 	}
123052ca7ee2SPyun YongHyeon 	if_maddr_runlock(ifp);
123152ca7ee2SPyun YongHyeon 
123252ca7ee2SPyun YongHyeon 	for (; mcnt < DC_ULI_FILTER_NPERF; mcnt++) {
123352ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123452ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123552ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123652ca7ee2SPyun YongHyeon 	}
123752ca7ee2SPyun YongHyeon 
123852ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
123952ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
124052ca7ee2SPyun YongHyeon 		    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
124152ca7ee2SPyun YongHyeon 	if (ifp->if_flags & IFF_PROMISC)
124252ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI;
124352ca7ee2SPyun YongHyeon 	if (ifp->if_flags & IFF_ALLMULTI)
124452ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_ALLMULTI;
124552ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_NETCFG,
124652ca7ee2SPyun YongHyeon 	    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
124752ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
124852ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, filter);
124952ca7ee2SPyun YongHyeon 
125052ca7ee2SPyun YongHyeon 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
125152ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
125252ca7ee2SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
125352ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
125452ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
125552ca7ee2SPyun YongHyeon 
125652ca7ee2SPyun YongHyeon 	/*
125752ca7ee2SPyun YongHyeon 	 * Wait some time...
125852ca7ee2SPyun YongHyeon 	 */
125952ca7ee2SPyun YongHyeon 	DELAY(1000);
126052ca7ee2SPyun YongHyeon 
126152ca7ee2SPyun YongHyeon 	sc->dc_wdog_timer = 5;
126252ca7ee2SPyun YongHyeon }
126352ca7ee2SPyun YongHyeon 
126452ca7ee2SPyun YongHyeon static void
12650934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1266feb78939SJonathan Chen {
12678df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
12680934f18aSMaxime Henrion 	struct ifnet *ifp;
12690934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1270feb78939SJonathan Chen 	struct dc_desc *sframe;
1271ee320f98SPyun YongHyeon 	uint32_t h, *sp;
1272feb78939SJonathan Chen 	int i;
1273feb78939SJonathan Chen 
1274fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1275feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1276feb78939SJonathan Chen 
1277feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1278feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1279feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
12805f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
128156e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
12820934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1283feb78939SJonathan Chen 
12845f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1285af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1286af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1287feb78939SJonathan Chen 
128856e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1289feb78939SJonathan Chen 
1290feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1291feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1292feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1293feb78939SJonathan Chen 	else
1294feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1295feb78939SJonathan Chen 
1296feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1297feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1298feb78939SJonathan Chen 	else
1299feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1300feb78939SJonathan Chen 
1301eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
13026817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1303feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1304feb78939SJonathan Chen 			continue;
1305aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13061d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1307af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1308feb78939SJonathan Chen 	}
1309eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
1310feb78939SJonathan Chen 
1311feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1312aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1313af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1314feb78939SJonathan Chen 	}
1315feb78939SJonathan Chen 
13168df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
13178df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13188df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13198df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13208df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1321feb78939SJonathan Chen 
1322feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1323feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1324af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
13258c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
13268c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
13275f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1328feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1329feb78939SJonathan Chen 
1330feb78939SJonathan Chen 	/*
13310934f18aSMaxime Henrion 	 * Wait some time...
1332feb78939SJonathan Chen 	 */
1333feb78939SJonathan Chen 	DELAY(1000);
1334feb78939SJonathan Chen 
1335b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
1336feb78939SJonathan Chen }
1337feb78939SJonathan Chen 
1338e3d2833aSAlfred Perlstein static void
13390934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
134096f2e892SBill Paul {
13410934f18aSMaxime Henrion 
134296f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13431af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
134496f2e892SBill Paul 		dc_setfilt_21143(sc);
134596f2e892SBill Paul 
134696f2e892SBill Paul 	if (DC_IS_ASIX(sc))
134796f2e892SBill Paul 		dc_setfilt_asix(sc);
134896f2e892SBill Paul 
134996f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
135096f2e892SBill Paul 		dc_setfilt_admtek(sc);
135196f2e892SBill Paul 
135252ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
135352ca7ee2SPyun YongHyeon 		dc_setfilt_uli(sc);
135452ca7ee2SPyun YongHyeon 
1355feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1356feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
135796f2e892SBill Paul }
135896f2e892SBill Paul 
1359e3d2833aSAlfred Perlstein static void
13601da7683aSPyun YongHyeon dc_netcfg_wait(struct dc_softc *sc)
136196f2e892SBill Paul {
1362ee320f98SPyun YongHyeon 	uint32_t isr;
13631da7683aSPyun YongHyeon 	int i;
136496f2e892SBill Paul 
136596f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
136696f2e892SBill Paul 		isr = CSR_READ_4(sc, DC_ISR);
1367d467c136SBill Paul 		if (isr & DC_ISR_TX_IDLE &&
1368351267c1SMartin Blapp 		    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1369351267c1SMartin Blapp 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
137096f2e892SBill Paul 			break;
1371d467c136SBill Paul 		DELAY(10);
137296f2e892SBill Paul 	}
13737a6fab66SWarner Losh 	if (i == DC_TIMEOUT && bus_child_present(sc->dc_dev)) {
1374432120f2SMarius Strobl 		if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13756b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
13761da7683aSPyun YongHyeon 			    "%s: failed to force tx to idle state\n", __func__);
1377432120f2SMarius Strobl 		if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1378432120f2SMarius Strobl 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1379d0d67284SMarius Strobl 		    !DC_HAS_BROKEN_RXSTATE(sc))
1380432120f2SMarius Strobl 			device_printf(sc->dc_dev,
13811da7683aSPyun YongHyeon 			    "%s: failed to force rx to idle state\n", __func__);
1382432120f2SMarius Strobl 	}
138396f2e892SBill Paul }
138496f2e892SBill Paul 
13851da7683aSPyun YongHyeon /*
13861da7683aSPyun YongHyeon  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13871da7683aSPyun YongHyeon  * the netconfig register, we first have to put the transmit and/or
13881da7683aSPyun YongHyeon  * receive logic in the idle state.
13891da7683aSPyun YongHyeon  */
13901da7683aSPyun YongHyeon static void
13911da7683aSPyun YongHyeon dc_setcfg(struct dc_softc *sc, int media)
13921da7683aSPyun YongHyeon {
13931da7683aSPyun YongHyeon 	int restart = 0, watchdogreg;
13941da7683aSPyun YongHyeon 
13951da7683aSPyun YongHyeon 	if (IFM_SUBTYPE(media) == IFM_NONE)
13961da7683aSPyun YongHyeon 		return;
13971da7683aSPyun YongHyeon 
13981da7683aSPyun YongHyeon 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
13991da7683aSPyun YongHyeon 		restart = 1;
14001da7683aSPyun YongHyeon 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
14011da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
14021da7683aSPyun YongHyeon 	}
14031da7683aSPyun YongHyeon 
140496f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1405042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1406042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
140796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1408bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14090934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14108273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14118273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14128273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14134c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1414bf645417SBill Paul 			} else {
1415bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1416bf645417SBill Paul 			}
141796f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141896f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
141996f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
142096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
142196f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
142288d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
142396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
142496f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
142596f2e892SBill Paul 		} else {
142696f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
142796f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
142896f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
142996f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
143096f2e892SBill Paul 			}
1431318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1432318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1433318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
143496f2e892SBill Paul 		}
143596f2e892SBill Paul 	}
143696f2e892SBill Paul 
143796f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1438042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1439042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
144096f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14410934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14424c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14438273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14448273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14458273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14468273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14474c2efe27SBill Paul 			} else {
14484c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14494c2efe27SBill Paul 			}
145096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
145196f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
145296f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
145396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
145488d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
145596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
145696f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
145796f2e892SBill Paul 		} else {
145896f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
145996f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
146096f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
146196f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
146296f2e892SBill Paul 			}
146396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1464318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14665c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14675c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14685c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14695c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14705c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14715c1cfac4SBill Paul 				else
14725c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14735c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14745c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14755c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14765c1cfac4SBill Paul 				DELAY(20000);
14775c1cfac4SBill Paul 			}
147896f2e892SBill Paul 		}
147996f2e892SBill Paul 	}
148096f2e892SBill Paul 
1481f43d9309SBill Paul 	/*
1482f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1483f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1484f43d9309SBill Paul 	 * on the external MII port.
1485f43d9309SBill Paul 	 */
1486f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
148745521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1488f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1489f43d9309SBill Paul 			sc->dc_link = 1;
1490f43d9309SBill Paul 		} else {
1491f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1492f43d9309SBill Paul 		}
1493f43d9309SBill Paul 	}
1494f43d9309SBill Paul 
149596f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
149696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149896f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149996f2e892SBill Paul 	} else {
150096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
150196f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
150296f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
150396f2e892SBill Paul 	}
150496f2e892SBill Paul 
150596f2e892SBill Paul 	if (restart)
150696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
150796f2e892SBill Paul }
150896f2e892SBill Paul 
1509e3d2833aSAlfred Perlstein static void
15100934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
151196f2e892SBill Paul {
15120934f18aSMaxime Henrion 	int i;
151396f2e892SBill Paul 
151496f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
151596f2e892SBill Paul 
151696f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
151796f2e892SBill Paul 		DELAY(10);
151896f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
151996f2e892SBill Paul 			break;
152096f2e892SBill Paul 	}
152196f2e892SBill Paul 
15221af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
152352ca7ee2SPyun YongHyeon 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc) || DC_IS_ULI(sc)) {
152496f2e892SBill Paul 		DELAY(10000);
152596f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152696f2e892SBill Paul 		i = 0;
152796f2e892SBill Paul 	}
152896f2e892SBill Paul 
152996f2e892SBill Paul 	if (i == DC_TIMEOUT)
15306b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev, "reset never completed!\n");
153196f2e892SBill Paul 
153296f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
153396f2e892SBill Paul 	DELAY(1000);
153496f2e892SBill Paul 
153596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
153696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
153796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
153896f2e892SBill Paul 
153991cc2adbSBill Paul 	/*
154091cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
154191cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
154291cc2adbSBill Paul 	 * into a state where it will never come out of reset
154391cc2adbSBill Paul 	 * until we reset the whole chip again.
154491cc2adbSBill Paul 	 */
15455c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
154691cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1547d314ebf5SPyun YongHyeon 		CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFFFFFF);
15485c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15495c1cfac4SBill Paul 	}
155096f2e892SBill Paul }
155196f2e892SBill Paul 
1552ebc284ccSMarius Strobl static const struct dc_type *
15530934f18aSMaxime Henrion dc_devtype(device_t dev)
155496f2e892SBill Paul {
1555ebc284ccSMarius Strobl 	const struct dc_type *t;
1556ee320f98SPyun YongHyeon 	uint32_t devid;
1557ee320f98SPyun YongHyeon 	uint8_t rev;
155896f2e892SBill Paul 
155996f2e892SBill Paul 	t = dc_devs;
15601e2e70b1SJohn Baldwin 	devid = pci_get_devid(dev);
15611e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
156296f2e892SBill Paul 
156396f2e892SBill Paul 	while (t->dc_name != NULL) {
15641e2e70b1SJohn Baldwin 		if (devid == t->dc_devid && rev >= t->dc_minrev)
156596f2e892SBill Paul 			return (t);
156696f2e892SBill Paul 		t++;
156796f2e892SBill Paul 	}
156896f2e892SBill Paul 
156996f2e892SBill Paul 	return (NULL);
157096f2e892SBill Paul }
157196f2e892SBill Paul 
157296f2e892SBill Paul /*
157396f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
157496f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
157596f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
157696f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
157796f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
157896f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
157996f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
158096f2e892SBill Paul  */
1581e3d2833aSAlfred Perlstein static int
15820934f18aSMaxime Henrion dc_probe(device_t dev)
158396f2e892SBill Paul {
1584ebc284ccSMarius Strobl 	const struct dc_type *t;
158596f2e892SBill Paul 
158696f2e892SBill Paul 	t = dc_devtype(dev);
158796f2e892SBill Paul 
158896f2e892SBill Paul 	if (t != NULL) {
158996f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1590d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
159196f2e892SBill Paul 	}
159296f2e892SBill Paul 
159396f2e892SBill Paul 	return (ENXIO);
159496f2e892SBill Paul }
159596f2e892SBill Paul 
1596e3d2833aSAlfred Perlstein static void
15970934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
15985c1cfac4SBill Paul {
15995c1cfac4SBill Paul 	struct dc_mediainfo *m;
1600ee320f98SPyun YongHyeon 	uint8_t *p;
16015c1cfac4SBill Paul 	int i;
1602ee320f98SPyun YongHyeon 	uint32_t reg;
16035c1cfac4SBill Paul 
16045c1cfac4SBill Paul 	m = sc->dc_mi;
16055c1cfac4SBill Paul 
16065c1cfac4SBill Paul 	while (m != NULL) {
16075c1cfac4SBill Paul 		if (m->dc_media == media)
16085c1cfac4SBill Paul 			break;
16095c1cfac4SBill Paul 		m = m->dc_next;
16105c1cfac4SBill Paul 	}
16115c1cfac4SBill Paul 
16125c1cfac4SBill Paul 	if (m == NULL)
16135c1cfac4SBill Paul 		return;
16145c1cfac4SBill Paul 
16155c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16165c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16175c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16185c1cfac4SBill Paul 	}
16195c1cfac4SBill Paul 
16205c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16215c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16225c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16235c1cfac4SBill Paul 	}
16245c1cfac4SBill Paul }
16255c1cfac4SBill Paul 
1626abe4e865SPyun YongHyeon static int
16270934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16285c1cfac4SBill Paul {
16295c1cfac4SBill Paul 	struct dc_mediainfo *m;
16305c1cfac4SBill Paul 
16310934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1632abe4e865SPyun YongHyeon 	if (m == NULL) {
1633abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1634abe4e865SPyun YongHyeon 		return (ENOMEM);
1635abe4e865SPyun YongHyeon 	}
163687f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
163787f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16385c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
163987f4fa15SMartin Blapp 		break;
164087f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16415c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
164287f4fa15SMartin Blapp 		break;
164387f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16445c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
164587f4fa15SMartin Blapp 		break;
164687f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16475c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
164887f4fa15SMartin Blapp 		break;
164987f4fa15SMartin Blapp 	default:
165087f4fa15SMartin Blapp 		break;
165187f4fa15SMartin Blapp 	}
16525c1cfac4SBill Paul 
165387f4fa15SMartin Blapp 	/*
165487f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
165587f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
165687f4fa15SMartin Blapp 	 * supply Media Specific Data.
165787f4fa15SMartin Blapp 	 */
165887f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16595c1cfac4SBill Paul 		m->dc_gp_len = 2;
166087f4fa15SMartin Blapp 		m->dc_gp_ptr =
1661ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
166287f4fa15SMartin Blapp 	} else {
166387f4fa15SMartin Blapp 		m->dc_gp_len = 2;
166487f4fa15SMartin Blapp 		m->dc_gp_ptr =
1665ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
166687f4fa15SMartin Blapp 	}
16675c1cfac4SBill Paul 
16685c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16695c1cfac4SBill Paul 	sc->dc_mi = m;
16705c1cfac4SBill Paul 
16715c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
1672abe4e865SPyun YongHyeon 	return (0);
16735c1cfac4SBill Paul }
16745c1cfac4SBill Paul 
1675abe4e865SPyun YongHyeon static int
16760934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16775c1cfac4SBill Paul {
16785c1cfac4SBill Paul 	struct dc_mediainfo *m;
16795c1cfac4SBill Paul 
16800934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1681abe4e865SPyun YongHyeon 	if (m == NULL) {
1682abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1683abe4e865SPyun YongHyeon 		return (ENOMEM);
1684abe4e865SPyun YongHyeon 	}
16855c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16865c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16875c1cfac4SBill Paul 
16885c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16895c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
16905c1cfac4SBill Paul 
16915c1cfac4SBill Paul 	m->dc_gp_len = 2;
1692ee320f98SPyun YongHyeon 	m->dc_gp_ptr = (uint8_t *)&l->dc_sym_gpio_ctl;
16935c1cfac4SBill Paul 
16945c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16955c1cfac4SBill Paul 	sc->dc_mi = m;
16965c1cfac4SBill Paul 
16975c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
1698abe4e865SPyun YongHyeon 	return (0);
16995c1cfac4SBill Paul }
17005c1cfac4SBill Paul 
1701abe4e865SPyun YongHyeon static int
17020934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17035c1cfac4SBill Paul {
17045c1cfac4SBill Paul 	struct dc_mediainfo *m;
1705ee320f98SPyun YongHyeon 	uint8_t *p;
17065c1cfac4SBill Paul 
17070934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1708abe4e865SPyun YongHyeon 	if (m == NULL) {
1709abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1710abe4e865SPyun YongHyeon 		return (ENOMEM);
1711abe4e865SPyun YongHyeon 	}
17125c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17135c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17145c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17155c1cfac4SBill Paul 
1716ee320f98SPyun YongHyeon 	p = (uint8_t *)l;
17175c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17185c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17195c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17205c1cfac4SBill Paul 	m->dc_reset_len = *p;
17215c1cfac4SBill Paul 	p++;
17225c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17235c1cfac4SBill Paul 
17245c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17255c1cfac4SBill Paul 	sc->dc_mi = m;
1726abe4e865SPyun YongHyeon 	return (0);
17275c1cfac4SBill Paul }
17285c1cfac4SBill Paul 
1729abe4e865SPyun YongHyeon static int
17300934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17313097aa70SWarner Losh {
17323097aa70SWarner Losh 	int size;
17333097aa70SWarner Losh 
1734abe4e865SPyun YongHyeon 	size = DC_ROM_SIZE(bits);
173552ca7ee2SPyun YongHyeon 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
1736abe4e865SPyun YongHyeon 	if (sc->dc_srom == NULL) {
1737abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate SROM buffer\n");
1738abe4e865SPyun YongHyeon 		return (ENOMEM);
1739abe4e865SPyun YongHyeon 	}
17403097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1741abe4e865SPyun YongHyeon 	return (0);
17423097aa70SWarner Losh }
17433097aa70SWarner Losh 
1744abe4e865SPyun YongHyeon static int
17450934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17465c1cfac4SBill Paul {
17475c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17485c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
1749abe4e865SPyun YongHyeon 	int error, have_mii, i, loff;
17505c1cfac4SBill Paul 	char *ptr;
17515c1cfac4SBill Paul 
1752f956e0b3SMartin Blapp 	have_mii = 0;
17535c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17545c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17555c1cfac4SBill Paul 
17565c1cfac4SBill Paul 	ptr = (char *)lhdr;
17575c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1758f956e0b3SMartin Blapp 	/*
1759f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1760f956e0b3SMartin Blapp 	 */
1761f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1762f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1763f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1764f956e0b3SMartin Blapp 		    have_mii++;
1765f956e0b3SMartin Blapp 
1766f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1767f956e0b3SMartin Blapp 		ptr++;
1768f956e0b3SMartin Blapp 	}
1769f956e0b3SMartin Blapp 
1770f956e0b3SMartin Blapp 	/*
1771f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1772f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1773f956e0b3SMartin Blapp 	 */
1774f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1775f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1776abe4e865SPyun YongHyeon 	error = 0;
17775c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17785c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17795c1cfac4SBill Paul 		switch (hdr->dc_type) {
17805c1cfac4SBill Paul 		case DC_EBLOCK_MII:
1781abe4e865SPyun YongHyeon 			error = dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17825c1cfac4SBill Paul 			break;
17835c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1784f956e0b3SMartin Blapp 			if (! have_mii)
1785abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sia(sc,
1786f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
17875c1cfac4SBill Paul 			break;
17885c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1789f956e0b3SMartin Blapp 			if (! have_mii)
1790abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sym(sc,
1791f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
17925c1cfac4SBill Paul 			break;
17935c1cfac4SBill Paul 		default:
17945c1cfac4SBill Paul 			/* Don't care. Yet. */
17955c1cfac4SBill Paul 			break;
17965c1cfac4SBill Paul 		}
17975c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
17985c1cfac4SBill Paul 		ptr++;
17995c1cfac4SBill Paul 	}
1800abe4e865SPyun YongHyeon 	return (error);
18015c1cfac4SBill Paul }
18025c1cfac4SBill Paul 
180356e5e7aeSMaxime Henrion static void
180456e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
180556e5e7aeSMaxime Henrion {
18065f14ee23SPyun YongHyeon 	bus_addr_t *paddr;
180756e5e7aeSMaxime Henrion 
1808ebc284ccSMarius Strobl 	KASSERT(nseg == 1,
1809ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
181056e5e7aeSMaxime Henrion 	paddr = arg;
181156e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
181256e5e7aeSMaxime Henrion }
181356e5e7aeSMaxime Henrion 
18145f14ee23SPyun YongHyeon static int
18155f14ee23SPyun YongHyeon dc_dma_alloc(struct dc_softc *sc)
18165f14ee23SPyun YongHyeon {
18175f14ee23SPyun YongHyeon 	int error, i;
18185f14ee23SPyun YongHyeon 
18195f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dc_dev), 1, 0,
18205f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
18215f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
18225f14ee23SPyun YongHyeon 	    NULL, NULL, &sc->dc_ptag);
18235f14ee23SPyun YongHyeon 	if (error) {
18245f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18255f14ee23SPyun YongHyeon 		    "failed to allocate parent DMA tag\n");
18265f14ee23SPyun YongHyeon 		goto fail;
18275f14ee23SPyun YongHyeon 	}
18285f14ee23SPyun YongHyeon 
18295f14ee23SPyun YongHyeon 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
18305f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18315f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_RX_LIST_SZ, 1,
18325f14ee23SPyun YongHyeon 	    DC_RX_LIST_SZ, 0, NULL, NULL, &sc->dc_rx_ltag);
18335f14ee23SPyun YongHyeon 	if (error) {
18345f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX list DMA tag\n");
18355f14ee23SPyun YongHyeon 		goto fail;
18365f14ee23SPyun YongHyeon 	}
18375f14ee23SPyun YongHyeon 
18385f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18395f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_TX_LIST_SZ, 1,
18405f14ee23SPyun YongHyeon 	    DC_TX_LIST_SZ, 0, NULL, NULL, &sc->dc_tx_ltag);
18415f14ee23SPyun YongHyeon 	if (error) {
18425f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX list DMA tag\n");
18435f14ee23SPyun YongHyeon 		goto fail;
18445f14ee23SPyun YongHyeon 	}
18455f14ee23SPyun YongHyeon 
18465f14ee23SPyun YongHyeon 	/* RX descriptor list. */
18475f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_rx_ltag,
18485f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_rx_list, BUS_DMA_NOWAIT |
18495f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_rx_lmap);
18505f14ee23SPyun YongHyeon 	if (error) {
18515f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18525f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for RX list\n");
18535f14ee23SPyun YongHyeon 		goto fail;
18545f14ee23SPyun YongHyeon 	}
18555f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_rx_ltag, sc->dc_rx_lmap,
18565f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_rx_list, DC_RX_LIST_SZ, dc_dma_map_addr,
18575f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_rx_list_paddr, BUS_DMA_NOWAIT);
18585f14ee23SPyun YongHyeon 	if (error) {
18595f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18605f14ee23SPyun YongHyeon 		    "failed to load DMA'able memory for RX list\n");
18615f14ee23SPyun YongHyeon 		goto fail;
18625f14ee23SPyun YongHyeon 	}
18635f14ee23SPyun YongHyeon 	/* TX descriptor list. */
18645f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_tx_ltag,
18655f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_tx_list, BUS_DMA_NOWAIT |
18665f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_tx_lmap);
18675f14ee23SPyun YongHyeon 	if (error) {
18685f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18695f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for TX list\n");
18705f14ee23SPyun YongHyeon 		goto fail;
18715f14ee23SPyun YongHyeon 	}
18725f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_tx_ltag, sc->dc_tx_lmap,
18735f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_tx_list, DC_TX_LIST_SZ, dc_dma_map_addr,
18745f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_tx_list_paddr, BUS_DMA_NOWAIT);
18755f14ee23SPyun YongHyeon 	if (error) {
18765f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18775f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for TX list\n");
18785f14ee23SPyun YongHyeon 		goto fail;
18795f14ee23SPyun YongHyeon 	}
18805f14ee23SPyun YongHyeon 
18815f14ee23SPyun YongHyeon 	/*
18825f14ee23SPyun YongHyeon 	 * Allocate a busdma tag and DMA safe memory for the multicast
18835f14ee23SPyun YongHyeon 	 * setup frame.
18845f14ee23SPyun YongHyeon 	 */
18855f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18865f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
18875f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
18885f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_stag);
18895f14ee23SPyun YongHyeon 	if (error) {
18905f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18915f14ee23SPyun YongHyeon 		    "failed to create DMA tag for setup frame\n");
18925f14ee23SPyun YongHyeon 		goto fail;
18935f14ee23SPyun YongHyeon 	}
18945f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
18955f14ee23SPyun YongHyeon 	    BUS_DMA_NOWAIT, &sc->dc_smap);
18965f14ee23SPyun YongHyeon 	if (error) {
18975f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18985f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for setup frame\n");
18995f14ee23SPyun YongHyeon 		goto fail;
19005f14ee23SPyun YongHyeon 	}
19015f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
19025f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
19035f14ee23SPyun YongHyeon 	if (error) {
19045f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19055f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for setup frame\n");
19065f14ee23SPyun YongHyeon 		goto fail;
19075f14ee23SPyun YongHyeon 	}
19085f14ee23SPyun YongHyeon 
19095f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for RX mbufs. */
19105f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_RXBUF_ALIGN, 0,
19115f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19125f14ee23SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->dc_rx_mtag);
19135f14ee23SPyun YongHyeon 	if (error) {
19145f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX mbuf tag\n");
19155f14ee23SPyun YongHyeon 		goto fail;
19165f14ee23SPyun YongHyeon 	}
19175f14ee23SPyun YongHyeon 
19185f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for TX mbufs. */
19195f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, 1, 0,
19205f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19215f14ee23SPyun YongHyeon 	    MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
19225f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_tx_mtag);
19235f14ee23SPyun YongHyeon 	if (error) {
19245f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX mbuf tag\n");
19255f14ee23SPyun YongHyeon 		goto fail;
19265f14ee23SPyun YongHyeon 	}
19275f14ee23SPyun YongHyeon 
19285f14ee23SPyun YongHyeon 	/* Create the TX/RX busdma maps. */
19295f14ee23SPyun YongHyeon 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
19305f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_tx_mtag, 0,
19315f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_tx_map[i]);
19325f14ee23SPyun YongHyeon 		if (error) {
19335f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19345f14ee23SPyun YongHyeon 			    "failed to create TX mbuf dmamap\n");
19355f14ee23SPyun YongHyeon 			goto fail;
19365f14ee23SPyun YongHyeon 		}
19375f14ee23SPyun YongHyeon 	}
19385f14ee23SPyun YongHyeon 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
19395f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_rx_mtag, 0,
19405f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_rx_map[i]);
19415f14ee23SPyun YongHyeon 		if (error) {
19425f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19435f14ee23SPyun YongHyeon 			    "failed to create RX mbuf dmamap\n");
19445f14ee23SPyun YongHyeon 			goto fail;
19455f14ee23SPyun YongHyeon 		}
19465f14ee23SPyun YongHyeon 	}
19475f14ee23SPyun YongHyeon 	error = bus_dmamap_create(sc->dc_rx_mtag, 0, &sc->dc_sparemap);
19485f14ee23SPyun YongHyeon 	if (error) {
19495f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19505f14ee23SPyun YongHyeon 		    "failed to create spare RX mbuf dmamap\n");
19515f14ee23SPyun YongHyeon 		goto fail;
19525f14ee23SPyun YongHyeon 	}
19535f14ee23SPyun YongHyeon 
19545f14ee23SPyun YongHyeon fail:
19555f14ee23SPyun YongHyeon 	return (error);
19565f14ee23SPyun YongHyeon }
19575f14ee23SPyun YongHyeon 
19585f14ee23SPyun YongHyeon static void
19595f14ee23SPyun YongHyeon dc_dma_free(struct dc_softc *sc)
19605f14ee23SPyun YongHyeon {
19615f14ee23SPyun YongHyeon 	int i;
19625f14ee23SPyun YongHyeon 
19635f14ee23SPyun YongHyeon 	/* RX buffers. */
19645f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19655f14ee23SPyun YongHyeon 		for (i = 0; i < DC_RX_LIST_CNT; i++) {
19665f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
19675f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_rx_mtag,
19685f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_rx_map[i]);
19695f14ee23SPyun YongHyeon 		}
19705f14ee23SPyun YongHyeon 		if (sc->dc_sparemap != NULL)
19715f14ee23SPyun YongHyeon 			bus_dmamap_destroy(sc->dc_rx_mtag, sc->dc_sparemap);
19725f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_mtag);
19735f14ee23SPyun YongHyeon 	}
19745f14ee23SPyun YongHyeon 
19755f14ee23SPyun YongHyeon 	/* TX buffers. */
19765f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19775f14ee23SPyun YongHyeon 		for (i = 0; i < DC_TX_LIST_CNT; i++) {
19785f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
19795f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_tx_mtag,
19805f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_tx_map[i]);
19815f14ee23SPyun YongHyeon 		}
19825f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_mtag);
19835f14ee23SPyun YongHyeon 	}
19845f14ee23SPyun YongHyeon 
19855f14ee23SPyun YongHyeon 	/* RX descriptor list. */
19865f14ee23SPyun YongHyeon 	if (sc->dc_rx_ltag) {
19875f14ee23SPyun YongHyeon 		if (sc->dc_rx_lmap != NULL)
19885f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_ltag, sc->dc_rx_lmap);
19895f14ee23SPyun YongHyeon 		if (sc->dc_rx_lmap != NULL && sc->dc_ldata.dc_rx_list != NULL)
19905f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_rx_ltag, sc->dc_ldata.dc_rx_list,
19915f14ee23SPyun YongHyeon 			    sc->dc_rx_lmap);
19925f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_ltag);
19935f14ee23SPyun YongHyeon 	}
19945f14ee23SPyun YongHyeon 
19955f14ee23SPyun YongHyeon 	/* TX descriptor list. */
19965f14ee23SPyun YongHyeon 	if (sc->dc_tx_ltag) {
19975f14ee23SPyun YongHyeon 		if (sc->dc_tx_lmap != NULL)
19985f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_ltag, sc->dc_tx_lmap);
19995f14ee23SPyun YongHyeon 		if (sc->dc_tx_lmap != NULL && sc->dc_ldata.dc_tx_list != NULL)
20005f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_tx_ltag, sc->dc_ldata.dc_tx_list,
20015f14ee23SPyun YongHyeon 			    sc->dc_tx_lmap);
20025f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_ltag);
20035f14ee23SPyun YongHyeon 	}
20045f14ee23SPyun YongHyeon 
20055f14ee23SPyun YongHyeon 	/* multicast setup frame. */
20065f14ee23SPyun YongHyeon 	if (sc->dc_stag) {
20075f14ee23SPyun YongHyeon 		if (sc->dc_smap != NULL)
20085f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_stag, sc->dc_smap);
20095f14ee23SPyun YongHyeon 		if (sc->dc_smap != NULL && sc->dc_cdata.dc_sbuf != NULL)
20105f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf,
20115f14ee23SPyun YongHyeon 			    sc->dc_smap);
20125f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_stag);
20135f14ee23SPyun YongHyeon 	}
20145f14ee23SPyun YongHyeon }
20155f14ee23SPyun YongHyeon 
201696f2e892SBill Paul /*
201796f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
201896f2e892SBill Paul  * setup and ethernet/BPF attach.
201996f2e892SBill Paul  */
2020e3d2833aSAlfred Perlstein static int
20210934f18aSMaxime Henrion dc_attach(device_t dev)
202296f2e892SBill Paul {
20238df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
2024ee320f98SPyun YongHyeon 	uint32_t command;
202596f2e892SBill Paul 	struct dc_softc *sc;
202696f2e892SBill Paul 	struct ifnet *ifp;
2027b289c607SPyun YongHyeon 	struct dc_mediainfo *m;
2028ee320f98SPyun YongHyeon 	uint32_t reg, revision;
202952ca7ee2SPyun YongHyeon 	uint16_t *srom;
203052ca7ee2SPyun YongHyeon 	int error, mac_offset, n, phy, rid, tmp;
2031ee320f98SPyun YongHyeon 	uint8_t *mac;
203296f2e892SBill Paul 
203396f2e892SBill Paul 	sc = device_get_softc(dev);
20346b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
203596f2e892SBill Paul 
20366008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2037c8b27acaSJohn Baldwin 	    MTX_DEF);
2038c3e7434fSWarner Losh 
203996f2e892SBill Paul 	/*
204096f2e892SBill Paul 	 * Map control/status registers.
204196f2e892SBill Paul 	 */
204207f65363SBill Paul 	pci_enable_busmaster(dev);
204396f2e892SBill Paul 
204496f2e892SBill Paul 	rid = DC_RID;
20455f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
204696f2e892SBill Paul 
204796f2e892SBill Paul 	if (sc->dc_res == NULL) {
204822f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
204996f2e892SBill Paul 		error = ENXIO;
2050608654d4SNate Lawson 		goto fail;
205196f2e892SBill Paul 	}
205296f2e892SBill Paul 
205396f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
205496f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
205596f2e892SBill Paul 
20560934f18aSMaxime Henrion 	/* Allocate interrupt. */
205754f1f1d1SNate Lawson 	rid = 0;
20585f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
205954f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
206054f1f1d1SNate Lawson 
206154f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
206222f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
206354f1f1d1SNate Lawson 		error = ENXIO;
206454f1f1d1SNate Lawson 		goto fail;
206554f1f1d1SNate Lawson 	}
206654f1f1d1SNate Lawson 
206796f2e892SBill Paul 	/* Need this info to decide on a chip type. */
206896f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
20691e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
207096f2e892SBill Paul 
2071abe4e865SPyun YongHyeon 	error = 0;
20726d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
20731e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
20741e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
20751e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
20761e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
2077eecb3844SMartin Blapp 		dc_eeprom_width(sc);
2078eecb3844SMartin Blapp 
20791e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
20801e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
208196f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
208296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2083042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
20845c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
2085abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2086abe4e865SPyun YongHyeon 		if (error != 0)
2087abe4e865SPyun YongHyeon 			goto fail;
208896f2e892SBill Paul 		break;
20891e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
20901e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
20911e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
209296f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
2093318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
2094318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
20957dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
20964a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
20971e2e70b1SJohn Baldwin 
20980a46b1dcSBill Paul 		/* Increase the latency timer value. */
20991e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
210096f2e892SBill Paul 		break;
21011e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
210296f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
210396f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
210496f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
210596f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2106abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2107abe4e865SPyun YongHyeon 		if (error != 0)
2108abe4e865SPyun YongHyeon 			goto fail;
210996f2e892SBill Paul 		break;
2110593a1aeaSMartin Blapp 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983):
21111e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
21121e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
21131e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
21141e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
21151e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
21161e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
21171e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
21181e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
21191e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
21201e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
21211e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
21221e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
212317762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
212417762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
2125593a1aeaSMartin Blapp 		sc->dc_type = DC_TYPE_AN983;
2126acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
212796f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
212896f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
212996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2130129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
213196f2e892SBill Paul 		break;
21321e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
21331e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
213496f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
213596f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
213696f2e892SBill Paul 		}
2137318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
213896f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
2139318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
2140318b02fdSBill Paul 		}
2141318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
214296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
214396f2e892SBill Paul 		break;
21441e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
21451e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
214679d11e09SBill Paul 		/*
214779d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
214879d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
214979d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
215079d11e09SBill Paul 		 * get the right number of bits out of the
215179d11e09SBill Paul 		 * CRC routine.
215279d11e09SBill Paul 		 */
215379d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
215479d11e09SBill Paul 		    revision < DC_REVISION_98725)
215579d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
215696f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
215796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2158318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
215996f2e892SBill Paul 		break;
21601e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
2161ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
2162ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2163ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2164ead7cde9SBill Paul 		break;
21651e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
216696f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
216779d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
2168318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
216996f2e892SBill Paul 		break;
21701e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
217196f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
217291cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
217396f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
217496f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
2175abe4e865SPyun YongHyeon 		if (sc->dc_pnic_rx_buf == NULL) {
2176abe4e865SPyun YongHyeon 			device_printf(sc->dc_dev,
2177abe4e865SPyun YongHyeon 			    "Could not allocate PNIC RX buffer\n");
2178abe4e865SPyun YongHyeon 			error = ENOMEM;
2179abe4e865SPyun YongHyeon 			goto fail;
2180abe4e865SPyun YongHyeon 		}
218196f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
218296f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
218396f2e892SBill Paul 		break;
21841e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
218596f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
218696f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
218796f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
218896f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
218996f2e892SBill Paul 		break;
21901e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
2191feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
21922dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
21932dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2194feb78939SJonathan Chen 		/*
2195feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2196feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
21972dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2198feb78939SJonathan Chen 		 */
21993097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2200feb78939SJonathan Chen 		break;
22011e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
22021af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
22031af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
22041af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
22051af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2206abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2207abe4e865SPyun YongHyeon 		if (error != 0)
2208abe4e865SPyun YongHyeon 			goto fail;
22091af8bec7SBill Paul 		break;
221052ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261):
221152ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263):
221252ca7ee2SPyun YongHyeon 		if (sc->dc_info->dc_devid ==
221352ca7ee2SPyun YongHyeon 		    DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261))
221452ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5261;
221552ca7ee2SPyun YongHyeon 		else
221652ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5263;
221752ca7ee2SPyun YongHyeon 		/* TX buffers should be aligned on 4 byte boundary. */
221852ca7ee2SPyun YongHyeon 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
221952ca7ee2SPyun YongHyeon 		    DC_TX_ALIGN;
222052ca7ee2SPyun YongHyeon 		sc->dc_pmode = DC_PMODE_MII;
222152ca7ee2SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
222252ca7ee2SPyun YongHyeon 		if (error != 0)
222352ca7ee2SPyun YongHyeon 			goto fail;
222452ca7ee2SPyun YongHyeon 		break;
222596f2e892SBill Paul 	default:
22261e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
22271e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
222896f2e892SBill Paul 		break;
222996f2e892SBill Paul 	}
223096f2e892SBill Paul 
223196f2e892SBill Paul 	/* Save the cache line size. */
223288d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
223388d739dcSBill Paul 		sc->dc_cachesize = 0;
223488d739dcSBill Paul 	else
22351e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
223696f2e892SBill Paul 
223796f2e892SBill Paul 	/* Reset the adapter. */
223896f2e892SBill Paul 	dc_reset(sc);
223996f2e892SBill Paul 
224096f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2241feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
224296f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
224396f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
224496f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
224596f2e892SBill Paul 	}
224696f2e892SBill Paul 
224796f2e892SBill Paul 	/*
224896f2e892SBill Paul 	 * Try to learn something about the supported media.
224996f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
225096f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
225196f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
225296f2e892SBill Paul 	 * Intel 21143.
225396f2e892SBill Paul 	 */
2254abe4e865SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
2255abe4e865SPyun YongHyeon 		error = dc_parse_21143_srom(sc);
2256abe4e865SPyun YongHyeon 		if (error != 0)
2257abe4e865SPyun YongHyeon 			goto fail;
2258abe4e865SPyun YongHyeon 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
225996f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
226096f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
226196f2e892SBill Paul 		else
226296f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
226396f2e892SBill Paul 	} else if (!sc->dc_pmode)
226496f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
226596f2e892SBill Paul 
226696f2e892SBill Paul 	/*
226796f2e892SBill Paul 	 * Get station address from the EEPROM.
226896f2e892SBill Paul 	 */
226996f2e892SBill Paul 	switch(sc->dc_type) {
227096f2e892SBill Paul 	case DC_TYPE_98713:
227196f2e892SBill Paul 	case DC_TYPE_98713A:
227296f2e892SBill Paul 	case DC_TYPE_987x5:
227396f2e892SBill Paul 	case DC_TYPE_PNICII:
227496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
227596f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
227696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
227796f2e892SBill Paul 		break;
227896f2e892SBill Paul 	case DC_TYPE_PNIC:
227996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
228096f2e892SBill Paul 		break;
228196f2e892SBill Paul 	case DC_TYPE_DM9102:
2282ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2283ec6a7299SMaxime Henrion #ifdef __sparc64__
2284ec6a7299SMaxime Henrion 		/*
2285ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2286802cab03SMarius Strobl 		 * the EEPROM is all zero and we have to get it from the FCode.
2287ec6a7299SMaxime Henrion 		 */
2288802cab03SMarius Strobl 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
22898069c79dSRuslan Ermilov 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2290ec6a7299SMaxime Henrion #endif
2291ec6a7299SMaxime Henrion 		break;
229296f2e892SBill Paul 	case DC_TYPE_21143:
229396f2e892SBill Paul 	case DC_TYPE_ASIX:
229496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
229596f2e892SBill Paul 		break;
229696f2e892SBill Paul 	case DC_TYPE_AL981:
2297593a1aeaSMartin Blapp 	case DC_TYPE_AN983:
22982e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR0);
22992e3d4b79SPyun YongHyeon 		mac = (uint8_t *)&eaddr[0];
23002e3d4b79SPyun YongHyeon 		mac[0] = (reg >> 0) & 0xff;
23012e3d4b79SPyun YongHyeon 		mac[1] = (reg >> 8) & 0xff;
23022e3d4b79SPyun YongHyeon 		mac[2] = (reg >> 16) & 0xff;
23032e3d4b79SPyun YongHyeon 		mac[3] = (reg >> 24) & 0xff;
23042e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR1);
23052e3d4b79SPyun YongHyeon 		mac[4] = (reg >> 0) & 0xff;
23062e3d4b79SPyun YongHyeon 		mac[5] = (reg >> 8) & 0xff;
230796f2e892SBill Paul 		break;
23081af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
23090934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
23100934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
23111af8bec7SBill Paul 		break;
2312feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
23130934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2314e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2315e7b01d07SWarner Losh 		if (!mac) {
2316e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2317608654d4SNate Lawson 			error = ENXIO;
2318e7b01d07SWarner Losh 			goto fail;
2319e7b01d07SWarner Losh 		}
2320e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2321feb78939SJonathan Chen 		break;
232252ca7ee2SPyun YongHyeon 	case DC_TYPE_ULI_M5261:
232352ca7ee2SPyun YongHyeon 	case DC_TYPE_ULI_M5263:
232452ca7ee2SPyun YongHyeon 		srom = (uint16_t *)sc->dc_srom;
232552ca7ee2SPyun YongHyeon 		if (srom == NULL || *srom == 0xFFFF || *srom == 0) {
232652ca7ee2SPyun YongHyeon 			/*
232752ca7ee2SPyun YongHyeon 			 * No valid SROM present, read station address
232852ca7ee2SPyun YongHyeon 			 * from ID Table.
232952ca7ee2SPyun YongHyeon 			 */
233052ca7ee2SPyun YongHyeon 			device_printf(dev,
233152ca7ee2SPyun YongHyeon 			    "Reading station address from ID Table.\n");
233252ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_BUSCTL, 0x10000);
233352ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x01C0);
233452ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0000);
233552ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0010);
233652ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0000);
233752ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x0000);
233852ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x01B0);
233952ca7ee2SPyun YongHyeon 			mac = (uint8_t *)eaddr;
234052ca7ee2SPyun YongHyeon 			for (n = 0; n < ETHER_ADDR_LEN; n++)
234152ca7ee2SPyun YongHyeon 				mac[n] = (uint8_t)CSR_READ_4(sc, DC_10BTCTRL);
234252ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x0000);
234352ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_BUSCTL, 0x0000);
234452ca7ee2SPyun YongHyeon 			DELAY(10);
234552ca7ee2SPyun YongHyeon 		} else
234652ca7ee2SPyun YongHyeon 			dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3,
234752ca7ee2SPyun YongHyeon 			    0);
234852ca7ee2SPyun YongHyeon 		break;
234996f2e892SBill Paul 	default:
235096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
235196f2e892SBill Paul 		break;
235296f2e892SBill Paul 	}
235396f2e892SBill Paul 
235439d76ed6SPyun YongHyeon 	bcopy(eaddr, sc->dc_eaddr, sizeof(eaddr));
235539d76ed6SPyun YongHyeon 	/*
235639d76ed6SPyun YongHyeon 	 * If we still have invalid station address, see whether we can
235739d76ed6SPyun YongHyeon 	 * find station address for chip 0.  Some multi-port controllers
235839d76ed6SPyun YongHyeon 	 * just store station address for chip 0 if they have a shared
235939d76ed6SPyun YongHyeon 	 * SROM.
236039d76ed6SPyun YongHyeon 	 */
236139d76ed6SPyun YongHyeon 	if ((sc->dc_eaddr[0] == 0 && (sc->dc_eaddr[1] & ~0xffff) == 0) ||
236239d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[0] == 0xffffffff &&
236339d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[1] & 0xffff) == 0xffff)) {
2364b289c607SPyun YongHyeon 		error = dc_check_multiport(sc);
2365b289c607SPyun YongHyeon 		if (error == 0) {
236639d76ed6SPyun YongHyeon 			bcopy(sc->dc_eaddr, eaddr, sizeof(eaddr));
2367b289c607SPyun YongHyeon 			/* Extract media information. */
2368b289c607SPyun YongHyeon 			if (DC_IS_INTEL(sc) && sc->dc_srom != NULL) {
2369b289c607SPyun YongHyeon 				while (sc->dc_mi != NULL) {
2370b289c607SPyun YongHyeon 					m = sc->dc_mi->dc_next;
2371b289c607SPyun YongHyeon 					free(sc->dc_mi, M_DEVBUF);
2372b289c607SPyun YongHyeon 					sc->dc_mi = m;
2373b289c607SPyun YongHyeon 				}
2374b289c607SPyun YongHyeon 				error = dc_parse_21143_srom(sc);
2375b289c607SPyun YongHyeon 				if (error != 0)
2376b289c607SPyun YongHyeon 					goto fail;
2377b289c607SPyun YongHyeon 			}
2378b289c607SPyun YongHyeon 		} else if (error == ENOMEM)
2379b289c607SPyun YongHyeon 			goto fail;
2380b289c607SPyun YongHyeon 		else
2381b289c607SPyun YongHyeon 			error = 0;
238239d76ed6SPyun YongHyeon 	}
238339d76ed6SPyun YongHyeon 
23845f14ee23SPyun YongHyeon 	if ((error = dc_dma_alloc(sc)) != 0)
238556e5e7aeSMaxime Henrion 		goto fail;
238696f2e892SBill Paul 
2387fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2388fc74a9f9SBrooks Davis 	if (ifp == NULL) {
238922f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2390fc74a9f9SBrooks Davis 		error = ENOSPC;
2391fc74a9f9SBrooks Davis 		goto fail;
2392fc74a9f9SBrooks Davis 	}
239396f2e892SBill Paul 	ifp->if_softc = sc;
23949bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
23953d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
239696f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
239796f2e892SBill Paul 	ifp->if_start = dc_start;
239896f2e892SBill Paul 	ifp->if_init = dc_init;
2399cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2400cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2401cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
240296f2e892SBill Paul 
240396f2e892SBill Paul 	/*
24045c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
24055c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
24065c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
24075c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
24085c1cfac4SBill Paul 	 * driver instead.
240996f2e892SBill Paul 	 */
24108e5d93dbSMarius Strobl 	tmp = 0;
24115c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
24125c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
24135c1cfac4SBill Paul 		tmp = sc->dc_pmode;
24145c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
24155c1cfac4SBill Paul 	}
24165c1cfac4SBill Paul 
24176d431b17SWarner Losh 	/*
24186d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
24198e5d93dbSMarius Strobl 	 * to the MII.  This needs to be done before mii_attach so that
24206d431b17SWarner Losh 	 * we can actually see them.
24216d431b17SWarner Losh 	 */
24226d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
24236d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
24246d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24256d431b17SWarner Losh 		DELAY(10);
24266d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
24276d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24286d431b17SWarner Losh 		DELAY(10);
24296d431b17SWarner Losh 	}
24306d431b17SWarner Losh 
24318e5d93dbSMarius Strobl 	phy = MII_PHY_ANY;
24328e5d93dbSMarius Strobl 	/*
24338e5d93dbSMarius Strobl 	 * Note: both the AL981 and AN983 have internal PHYs, however the
24348e5d93dbSMarius Strobl 	 * AL981 provides direct access to the PHY registers while the AN983
24358e5d93dbSMarius Strobl 	 * uses a serial MII interface. The AN983's MII interface is also
24368e5d93dbSMarius Strobl 	 * buggy in that you can read from any MII address (0 to 31), but
24378e5d93dbSMarius Strobl 	 * only address 1 behaves normally. To deal with both cases, we
24388e5d93dbSMarius Strobl 	 * pretend that the PHY is at MII address 1.
24398e5d93dbSMarius Strobl 	 */
24408e5d93dbSMarius Strobl 	if (DC_IS_ADMTEK(sc))
24418e5d93dbSMarius Strobl 		phy = DC_ADMTEK_PHYADDR;
24428e5d93dbSMarius Strobl 
24438e5d93dbSMarius Strobl 	/*
24448e5d93dbSMarius Strobl 	 * Note: the ukphy probes of the RS7112 report a PHY at MII address
24458e5d93dbSMarius Strobl 	 * 0 (possibly HomePNA?) and 1 (ethernet) so we only respond to the
24468e5d93dbSMarius Strobl 	 * correct one.
24478e5d93dbSMarius Strobl 	 */
24488e5d93dbSMarius Strobl 	if (DC_IS_CONEXANT(sc))
24498e5d93dbSMarius Strobl 		phy = DC_CONEXANT_PHYADDR;
24508e5d93dbSMarius Strobl 
24518e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24528e5d93dbSMarius Strobl 	    dc_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
245396f2e892SBill Paul 
245496f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
24555c1cfac4SBill Paul 		sc->dc_pmode = tmp;
24565c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
245796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2458042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
24598e5d93dbSMarius Strobl 		mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24608e5d93dbSMarius Strobl 		    dc_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
24618e5d93dbSMarius Strobl 		    MII_OFFSET_ANY, 0);
246278999dd1SBill Paul 		/*
246378999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
246478999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
246578999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
246678999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
246778999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
246878999dd1SBill Paul 		 */
24691e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
24701e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
247178999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
247296f2e892SBill Paul 		error = 0;
247396f2e892SBill Paul 	}
247496f2e892SBill Paul 
247596f2e892SBill Paul 	if (error) {
24768e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
247796f2e892SBill Paul 		goto fail;
247896f2e892SBill Paul 	}
247996f2e892SBill Paul 
2480028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2481028a8491SMartin Blapp 		/*
2482028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2483028a8491SMartin Blapp 		 */
2484028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2485028a8491SMartin Blapp 	}
2486028a8491SMartin Blapp 
248796f2e892SBill Paul 	/*
2488db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2489db40c1aeSDoug Ambrisko 	 */
2490db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
24919ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
249240929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2493e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2494e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2495e695984eSRuslan Ermilov #endif
2496db40c1aeSDoug Ambrisko 
2497c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2498b1d16143SMarius Strobl 	callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
249996f2e892SBill Paul 
2500608654d4SNate Lawson 	/*
2501608654d4SNate Lawson 	 * Call MI attach routine.
2502608654d4SNate Lawson 	 */
25038df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2504608654d4SNate Lawson 
250554f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2506c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2507ef544f63SPaolo Pisati 	    NULL, dc_intr, sc, &sc->dc_intrhand);
2508608654d4SNate Lawson 
2509608654d4SNate Lawson 	if (error) {
251022f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2511693f4477SNate Lawson 		ether_ifdetach(ifp);
251254f1f1d1SNate Lawson 		goto fail;
2513608654d4SNate Lawson 	}
2514510a809eSMike Smith 
251596f2e892SBill Paul fail:
251654f1f1d1SNate Lawson 	if (error)
251754f1f1d1SNate Lawson 		dc_detach(dev);
251896f2e892SBill Paul 	return (error);
251996f2e892SBill Paul }
252096f2e892SBill Paul 
2521693f4477SNate Lawson /*
2522693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2523693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2524693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2525693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2526693f4477SNate Lawson  * allocated.
2527693f4477SNate Lawson  */
2528e3d2833aSAlfred Perlstein static int
25290934f18aSMaxime Henrion dc_detach(device_t dev)
253096f2e892SBill Paul {
253196f2e892SBill Paul 	struct dc_softc *sc;
253296f2e892SBill Paul 	struct ifnet *ifp;
25335c1cfac4SBill Paul 	struct dc_mediainfo *m;
253496f2e892SBill Paul 
253596f2e892SBill Paul 	sc = device_get_softc(dev);
253659f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2537d1ce9105SBill Paul 
2538fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
253996f2e892SBill Paul 
254040929967SGleb Smirnoff #ifdef DEVICE_POLLING
254140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
254240929967SGleb Smirnoff 		ether_poll_deregister(ifp);
254340929967SGleb Smirnoff #endif
254440929967SGleb Smirnoff 
2545693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2546214073e5SWarner Losh 	if (device_is_attached(dev)) {
2547c8b27acaSJohn Baldwin 		DC_LOCK(sc);
254896f2e892SBill Paul 		dc_stop(sc);
2549c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2550c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
2551b1d16143SMarius Strobl 		callout_drain(&sc->dc_wdog_ch);
25529ef8b520SSam Leffler 		ether_ifdetach(ifp);
2553693f4477SNate Lawson 	}
2554693f4477SNate Lawson 	if (sc->dc_miibus)
255596f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
255654f1f1d1SNate Lawson 	bus_generic_detach(dev);
255796f2e892SBill Paul 
255854f1f1d1SNate Lawson 	if (sc->dc_intrhand)
255996f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
256054f1f1d1SNate Lawson 	if (sc->dc_irq)
256196f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
256254f1f1d1SNate Lawson 	if (sc->dc_res)
256396f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
256496f2e892SBill Paul 
25656a3033a8SWarner Losh 	if (ifp)
25666a3033a8SWarner Losh 		if_free(ifp);
25676a3033a8SWarner Losh 
25685f14ee23SPyun YongHyeon 	dc_dma_free(sc);
256956e5e7aeSMaxime Henrion 
257096f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
257196f2e892SBill Paul 
25725c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
25735c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
25745c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
25755c1cfac4SBill Paul 		sc->dc_mi = m;
25765c1cfac4SBill Paul 	}
25777efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
25785c1cfac4SBill Paul 
2579d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
258096f2e892SBill Paul 
258196f2e892SBill Paul 	return (0);
258296f2e892SBill Paul }
258396f2e892SBill Paul 
258496f2e892SBill Paul /*
258596f2e892SBill Paul  * Initialize the transmit descriptors.
258696f2e892SBill Paul  */
2587e3d2833aSAlfred Perlstein static int
25880934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
258996f2e892SBill Paul {
259096f2e892SBill Paul 	struct dc_chain_data *cd;
259196f2e892SBill Paul 	struct dc_list_data *ld;
259201faf54bSLuigi Rizzo 	int i, nexti;
259396f2e892SBill Paul 
259496f2e892SBill Paul 	cd = &sc->dc_cdata;
25955f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
259696f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2597b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2598b3811c95SMaxime Henrion 			nexti = 0;
2599b3811c95SMaxime Henrion 		else
2600b3811c95SMaxime Henrion 			nexti = i + 1;
260152c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_status = 0;
260252c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_ctl = 0;
260352c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_data = 0;
2604af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
260596f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
260696f2e892SBill Paul 	}
260796f2e892SBill Paul 
260896f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
260906d23883SPyun YongHyeon 	cd->dc_tx_pkts = 0;
26105f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
261156e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
261296f2e892SBill Paul 	return (0);
261396f2e892SBill Paul }
261496f2e892SBill Paul 
261596f2e892SBill Paul 
261696f2e892SBill Paul /*
261796f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
261896f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
261996f2e892SBill Paul  * points back to the first.
262096f2e892SBill Paul  */
2621e3d2833aSAlfred Perlstein static int
26220934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
262396f2e892SBill Paul {
262496f2e892SBill Paul 	struct dc_chain_data *cd;
262596f2e892SBill Paul 	struct dc_list_data *ld;
262601faf54bSLuigi Rizzo 	int i, nexti;
262796f2e892SBill Paul 
262896f2e892SBill Paul 	cd = &sc->dc_cdata;
26295f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
263096f2e892SBill Paul 
263196f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
26325f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0)
263396f2e892SBill Paul 			return (ENOBUFS);
2634b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2635b3811c95SMaxime Henrion 			nexti = 0;
2636b3811c95SMaxime Henrion 		else
2637b3811c95SMaxime Henrion 			nexti = i + 1;
2638af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
263996f2e892SBill Paul 	}
264096f2e892SBill Paul 
264196f2e892SBill Paul 	cd->dc_rx_prod = 0;
26425f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
264356e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
264496f2e892SBill Paul 	return (0);
264596f2e892SBill Paul }
264696f2e892SBill Paul 
264796f2e892SBill Paul /*
264896f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
264996f2e892SBill Paul  */
2650e3d2833aSAlfred Perlstein static int
26515f14ee23SPyun YongHyeon dc_newbuf(struct dc_softc *sc, int i)
265296f2e892SBill Paul {
26535f14ee23SPyun YongHyeon 	struct mbuf *m;
26545f14ee23SPyun YongHyeon 	bus_dmamap_t map;
265582a67a70SMarius Strobl 	bus_dma_segment_t segs[1];
265682a67a70SMarius Strobl 	int error, nseg;
265796f2e892SBill Paul 
26585f14ee23SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
26595f14ee23SPyun YongHyeon 	if (m == NULL)
266096f2e892SBill Paul 		return (ENOBUFS);
26615f14ee23SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
26625f14ee23SPyun YongHyeon 	m_adj(m, sizeof(u_int64_t));
266396f2e892SBill Paul 
266496f2e892SBill Paul 	/*
266596f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
266696f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
266796f2e892SBill Paul 	 * 82c169 chips.
266896f2e892SBill Paul 	 */
266996f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
26705f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
267196f2e892SBill Paul 
26725f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_rx_mtag, sc->dc_sparemap,
26735f14ee23SPyun YongHyeon 	    m, segs, &nseg, 0);
267456e5e7aeSMaxime Henrion 	if (error) {
26755f14ee23SPyun YongHyeon 		m_freem(m);
267656e5e7aeSMaxime Henrion 		return (error);
267756e5e7aeSMaxime Henrion 	}
26785f14ee23SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: wrong number of segments (%d)", __func__,
26795f14ee23SPyun YongHyeon 	    nseg));
26805f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_rx_chain[i] != NULL)
26815f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i]);
268296f2e892SBill Paul 
26835f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_rx_map[i];
26845f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
26855f14ee23SPyun YongHyeon 	sc->dc_sparemap = map;
26865f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_chain[i] = m;
26875f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
268856e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
26895f14ee23SPyun YongHyeon 
26905f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
26915f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_data =
26925f14ee23SPyun YongHyeon 	    htole32(DC_ADDR_LO(segs[0].ds_addr));
26935f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
26945f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
269556e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
269696f2e892SBill Paul 	return (0);
269796f2e892SBill Paul }
269896f2e892SBill Paul 
269996f2e892SBill Paul /*
270096f2e892SBill Paul  * Grrrrr.
270196f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
270296f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
270396f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
270496f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
270596f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
270696f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
270796f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
270896f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
270996f2e892SBill Paul  *
271096f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
271196f2e892SBill Paul  * Here's what we know:
271296f2e892SBill Paul  *
271396f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
271496f2e892SBill Paul  *   descriptors uploaded.
271596f2e892SBill Paul  *
271696f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
271796f2e892SBill Paul  *   total data upload.
271896f2e892SBill Paul  *
271996f2e892SBill Paul  * - We know the size of the desired received frame because it will be
272096f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
272196f2e892SBill Paul  *
272296f2e892SBill Paul  * Here's what we do:
272396f2e892SBill Paul  *
272496f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
272596f2e892SBill Paul  *   This means that we know that the buffer contents should be all
272696f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
272796f2e892SBill Paul  *
272896f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
272996f2e892SBill Paul  *   ethernet CRC at the end.
273096f2e892SBill Paul  *
273196f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
273296f2e892SBill Paul  *
273396f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
273496f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
273596f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
273696f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
273796f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
273896f2e892SBill Paul  *   we won't be fooled.
273996f2e892SBill Paul  *
274096f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
274196f2e892SBill Paul  *   that value from the current pointer location. This brings us
274296f2e892SBill Paul  *   to the start of the actual received packet.
274396f2e892SBill Paul  *
274496f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
274596f2e892SBill Paul  *   frame length.
274696f2e892SBill Paul  *
274796f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
274896f2e892SBill Paul  * the time.
274996f2e892SBill Paul  */
275096f2e892SBill Paul 
275196f2e892SBill Paul #define	DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2752e3d2833aSAlfred Perlstein static void
27530934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
275496f2e892SBill Paul {
275596f2e892SBill Paul 	struct dc_desc *cur_rx;
275696f2e892SBill Paul 	struct dc_desc *c = NULL;
275796f2e892SBill Paul 	struct mbuf *m = NULL;
275896f2e892SBill Paul 	unsigned char *ptr;
275996f2e892SBill Paul 	int i, total_len;
2760ee320f98SPyun YongHyeon 	uint32_t rxstat = 0;
276196f2e892SBill Paul 
276296f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
27635f14ee23SPyun YongHyeon 	cur_rx = &sc->dc_ldata.dc_rx_list[idx];
276496f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
27651edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
276696f2e892SBill Paul 
276796f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
276896f2e892SBill Paul 	while (1) {
27695f14ee23SPyun YongHyeon 		c = &sc->dc_ldata.dc_rx_list[i];
2770af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
277196f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
277296f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
277396f2e892SBill Paul 		ptr += DC_RXLEN;
277496f2e892SBill Paul 		/* If this is the last buffer, break out. */
277596f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
277696f2e892SBill Paul 			break;
27775f14ee23SPyun YongHyeon 		dc_discard_rxbuf(sc, i);
277896f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
277996f2e892SBill Paul 	}
278096f2e892SBill Paul 
278196f2e892SBill Paul 	/* Find the length of the actual receive frame. */
278296f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
278396f2e892SBill Paul 
278496f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
278596f2e892SBill Paul 	while (*ptr == 0x00)
278696f2e892SBill Paul 		ptr--;
278796f2e892SBill Paul 
278896f2e892SBill Paul 	/* Round off. */
278996f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
279096f2e892SBill Paul 		ptr -= 1;
279196f2e892SBill Paul 
279296f2e892SBill Paul 	/* Now find the start of the frame. */
279396f2e892SBill Paul 	ptr -= total_len;
279496f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
279596f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
279696f2e892SBill Paul 
279796f2e892SBill Paul 	/*
279896f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
279996f2e892SBill Paul 	 * the status word to make it look like a successful
280096f2e892SBill Paul 	 * frame reception.
280196f2e892SBill Paul 	 */
280296f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2803af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
280496f2e892SBill Paul }
280596f2e892SBill Paul 
280696f2e892SBill Paul /*
280773bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
280873bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
280973bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
281073bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
281173bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
281273bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
281373bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
281473bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
281573bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
281673bf949cSBill Paul  */
2817e3d2833aSAlfred Perlstein static int
28180934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
281973bf949cSBill Paul {
282073bf949cSBill Paul 	struct dc_desc *cur_rx;
28210934f18aSMaxime Henrion 	int i, pos;
282273bf949cSBill Paul 
282373bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
282473bf949cSBill Paul 
282573bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
28265f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[pos];
2827af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
282873bf949cSBill Paul 			break;
282973bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
283073bf949cSBill Paul 	}
283173bf949cSBill Paul 
283273bf949cSBill Paul 	/* If the ring really is empty, then just return. */
283373bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
283473bf949cSBill Paul 		return (0);
283573bf949cSBill Paul 
283673bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
283773bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
283873bf949cSBill Paul 
283973bf949cSBill Paul 	return (EAGAIN);
284073bf949cSBill Paul }
284173bf949cSBill Paul 
28425f14ee23SPyun YongHyeon static void
28435f14ee23SPyun YongHyeon dc_discard_rxbuf(struct dc_softc *sc, int i)
28445f14ee23SPyun YongHyeon {
28455f14ee23SPyun YongHyeon 	struct mbuf *m;
28465f14ee23SPyun YongHyeon 
28475f14ee23SPyun YongHyeon 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
28485f14ee23SPyun YongHyeon 		m = sc->dc_cdata.dc_rx_chain[i];
28495f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
28505f14ee23SPyun YongHyeon 	}
28515f14ee23SPyun YongHyeon 
28525f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
28535f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
28545f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_PREREAD |
28555f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
28565f14ee23SPyun YongHyeon }
28575f14ee23SPyun YongHyeon 
285873bf949cSBill Paul /*
285996f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
286096f2e892SBill Paul  * the higher level protocols.
286196f2e892SBill Paul  */
28621abcdbd1SAttilio Rao static int
28630934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
286496f2e892SBill Paul {
28655f14ee23SPyun YongHyeon 	struct mbuf *m;
286696f2e892SBill Paul 	struct ifnet *ifp;
286796f2e892SBill Paul 	struct dc_desc *cur_rx;
28681abcdbd1SAttilio Rao 	int i, total_len, rx_npkts;
2869ee320f98SPyun YongHyeon 	uint32_t rxstat;
287096f2e892SBill Paul 
28715120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
28725120abbfSSam Leffler 
2873fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
28741abcdbd1SAttilio Rao 	rx_npkts = 0;
287596f2e892SBill Paul 
28765f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_POSTREAD |
28775f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
28785f14ee23SPyun YongHyeon 	for (i = sc->dc_cdata.dc_rx_prod;
28795f14ee23SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
28805f14ee23SPyun YongHyeon 	    DC_INC(i, DC_RX_LIST_CNT)) {
2881e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
288240929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2883e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2884e4fc250cSLuigi Rizzo 				break;
2885e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2886e4fc250cSLuigi Rizzo 		}
28870934f18aSMaxime Henrion #endif
28885f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[i];
2889af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
28905f14ee23SPyun YongHyeon 		if ((rxstat & DC_RXSTAT_OWN) != 0)
28915f14ee23SPyun YongHyeon 			break;
289296f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
28935f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
289456e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
289596f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
2896848a02fcSPyun YongHyeon 		rx_npkts++;
289796f2e892SBill Paul 
289896f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
289996f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
290096f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
290196f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
29025f14ee23SPyun YongHyeon 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0)
290396f2e892SBill Paul 					continue;
290496f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2905af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
290696f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
290796f2e892SBill Paul 			}
290896f2e892SBill Paul 		}
290996f2e892SBill Paul 
291096f2e892SBill Paul 		/*
291196f2e892SBill Paul 		 * If an error occurs, update stats, clear the
291296f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
291396f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2914db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
29150934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
291696f2e892SBill Paul 		 */
2917db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2918db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2919db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2920db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2921db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
292296f2e892SBill Paul 				ifp->if_ierrors++;
292396f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
292496f2e892SBill Paul 					ifp->if_collisions++;
29255f14ee23SPyun YongHyeon 				dc_discard_rxbuf(sc, i);
29265f14ee23SPyun YongHyeon 				if (rxstat & DC_RXSTAT_CRCERR)
292796f2e892SBill Paul 					continue;
29285f14ee23SPyun YongHyeon 				else {
29298f382a1fSPyun YongHyeon 					ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2930c8b27acaSJohn Baldwin 					dc_init_locked(sc);
29311abcdbd1SAttilio Rao 					return (rx_npkts);
293296f2e892SBill Paul 				}
293396f2e892SBill Paul 			}
2934db40c1aeSDoug Ambrisko 		}
293596f2e892SBill Paul 
293696f2e892SBill Paul 		/* No errors; receive the packet. */
293796f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2938432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
293901faf54bSLuigi Rizzo 		/*
2940432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
294101faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
294201faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
294301faf54bSLuigi Rizzo 		 * copy done in m_devget().
294401faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
294501faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
294601faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
294701faf54bSLuigi Rizzo 		 */
29485f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0) {
29495f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
29505f14ee23SPyun YongHyeon 			ifp->if_iqdrops++;
29515f14ee23SPyun YongHyeon 			continue;
29525f14ee23SPyun YongHyeon 		}
295301faf54bSLuigi Rizzo 		m->m_pkthdr.rcvif = ifp;
295401faf54bSLuigi Rizzo 		m->m_pkthdr.len = m->m_len = total_len;
29555f14ee23SPyun YongHyeon #else
295601faf54bSLuigi Rizzo 		{
29575f14ee23SPyun YongHyeon 			struct mbuf *m0;
29585f14ee23SPyun YongHyeon 
295901faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
296001faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
29615f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
296296f2e892SBill Paul 			if (m0 == NULL) {
29635f14ee23SPyun YongHyeon 				ifp->if_iqdrops++;
296496f2e892SBill Paul 				continue;
296596f2e892SBill Paul 			}
296696f2e892SBill Paul 			m = m0;
296701faf54bSLuigi Rizzo 		}
29685f14ee23SPyun YongHyeon #endif
296996f2e892SBill Paul 
297096f2e892SBill Paul 		ifp->if_ipackets++;
29715120abbfSSam Leffler 		DC_UNLOCK(sc);
29729ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
29735120abbfSSam Leffler 		DC_LOCK(sc);
297496f2e892SBill Paul 	}
297596f2e892SBill Paul 
297696f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
29771abcdbd1SAttilio Rao 	return (rx_npkts);
297896f2e892SBill Paul }
297996f2e892SBill Paul 
298096f2e892SBill Paul /*
298196f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
298296f2e892SBill Paul  * the list buffers.
298396f2e892SBill Paul  */
2984e3d2833aSAlfred Perlstein static void
29850934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
298696f2e892SBill Paul {
29875f14ee23SPyun YongHyeon 	struct dc_desc *cur_tx;
298896f2e892SBill Paul 	struct ifnet *ifp;
29895f14ee23SPyun YongHyeon 	int idx, setup;
2990ee320f98SPyun YongHyeon 	uint32_t ctl, txstat;
299196f2e892SBill Paul 
299206d23883SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt == 0)
299306d23883SPyun YongHyeon 		return;
299406d23883SPyun YongHyeon 
2995fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
299696f2e892SBill Paul 
299796f2e892SBill Paul 	/*
299896f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
299996f2e892SBill Paul 	 * frames that have been transmitted.
300096f2e892SBill Paul 	 */
3001cb94db27SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_POSTREAD |
30025f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
30035f14ee23SPyun YongHyeon 	setup = 0;
30045f14ee23SPyun YongHyeon 	for (idx = sc->dc_cdata.dc_tx_cons; idx != sc->dc_cdata.dc_tx_prod;
30055f14ee23SPyun YongHyeon 	    DC_INC(idx, DC_TX_LIST_CNT), sc->dc_cdata.dc_tx_cnt--) {
30065f14ee23SPyun YongHyeon 		cur_tx = &sc->dc_ldata.dc_tx_list[idx];
3007af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
3008af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
300996f2e892SBill Paul 
301096f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
301196f2e892SBill Paul 			break;
301296f2e892SBill Paul 
30135f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_chain[idx] == NULL)
30145f14ee23SPyun YongHyeon 			continue;
30155f14ee23SPyun YongHyeon 
3016af4358c7SMaxime Henrion 		if (ctl & DC_TXCTL_SETUP) {
30175f14ee23SPyun YongHyeon 			cur_tx->dc_ctl = htole32(ctl & ~DC_TXCTL_SETUP);
30185f14ee23SPyun YongHyeon 			setup++;
30195f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
30205f14ee23SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
302196f2e892SBill Paul 			/*
302296f2e892SBill Paul 			 * Yes, the PNIC is so brain damaged
302396f2e892SBill Paul 			 * that it will sometimes generate a TX
302496f2e892SBill Paul 			 * underrun error while DMAing the RX
302596f2e892SBill Paul 			 * filter setup frame. If we detect this,
302696f2e892SBill Paul 			 * we have to send the setup frame again,
302796f2e892SBill Paul 			 * or else the filter won't be programmed
302896f2e892SBill Paul 			 * correctly.
302996f2e892SBill Paul 			 */
303096f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
303196f2e892SBill Paul 				if (txstat & DC_TXSTAT_ERRSUM)
303296f2e892SBill Paul 					dc_setfilt(sc);
303396f2e892SBill Paul 			}
303496f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
303596f2e892SBill Paul 			continue;
303696f2e892SBill Paul 		}
303796f2e892SBill Paul 
303829a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
3039feb78939SJonathan Chen 			/*
3040feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
3041feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
304229a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
304329a2220aSBill Paul 			 * Who knows, but Conexant chips have the
304429a2220aSBill Paul 			 * same problem. Maybe they took lessons
304529a2220aSBill Paul 			 * from Xircom.
304629a2220aSBill Paul 			 */
3047feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
3048feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
3049feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
3050feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
3051feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
3052feb78939SJonathan Chen 		} else {
305396f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
305496f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
305596f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
305696f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
305796f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
3058feb78939SJonathan Chen 		}
305996f2e892SBill Paul 
306096f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
306196f2e892SBill Paul 			ifp->if_oerrors++;
306296f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
306396f2e892SBill Paul 				ifp->if_collisions++;
306496f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
306596f2e892SBill Paul 				ifp->if_collisions++;
306696f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
30678f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3068c8b27acaSJohn Baldwin 				dc_init_locked(sc);
306996f2e892SBill Paul 				return;
307096f2e892SBill Paul 			}
307152c43a47SPyun YongHyeon 		} else
307252c43a47SPyun YongHyeon 			ifp->if_opackets++;
307396f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
307496f2e892SBill Paul 
30755f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
307656e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
30775f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
307896f2e892SBill Paul 		m_freem(sc->dc_cdata.dc_tx_chain[idx]);
307996f2e892SBill Paul 		sc->dc_cdata.dc_tx_chain[idx] = NULL;
308096f2e892SBill Paul 	}
308196f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
308282a67a70SMarius Strobl 
30835f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
308413f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
30853e0e6726SMarius Strobl 		if (sc->dc_cdata.dc_tx_cnt == 0)
30863e0e6726SMarius Strobl 			sc->dc_wdog_timer = 0;
308796f2e892SBill Paul 	}
30885f14ee23SPyun YongHyeon 	if (setup > 0)
30895f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
30905f14ee23SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
30915f14ee23SPyun YongHyeon }
309296f2e892SBill Paul 
3093e3d2833aSAlfred Perlstein static void
30940934f18aSMaxime Henrion dc_tick(void *xsc)
309596f2e892SBill Paul {
309696f2e892SBill Paul 	struct dc_softc *sc;
309796f2e892SBill Paul 	struct mii_data *mii;
309896f2e892SBill Paul 	struct ifnet *ifp;
3099ee320f98SPyun YongHyeon 	uint32_t r;
310096f2e892SBill Paul 
310196f2e892SBill Paul 	sc = xsc;
3102c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3103fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
310496f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
310596f2e892SBill Paul 
310606d23883SPyun YongHyeon 	/*
310706d23883SPyun YongHyeon 	 * Reclaim transmitted frames for controllers that do
310806d23883SPyun YongHyeon 	 * not generate TX completion interrupt for every frame.
310906d23883SPyun YongHyeon 	 */
311006d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR)
311106d23883SPyun YongHyeon 		dc_txeof(sc);
311206d23883SPyun YongHyeon 
311396f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
3114318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
3115318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
3116318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3117318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
311896f2e892SBill Paul 				sc->dc_link = 0;
3119318b02fdSBill Paul 				mii_mediachg(mii);
3120318b02fdSBill Paul 			}
3121318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3122318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
3123318b02fdSBill Paul 				sc->dc_link = 0;
3124318b02fdSBill Paul 				mii_mediachg(mii);
3125318b02fdSBill Paul 			}
3126d675147eSBill Paul 			if (sc->dc_link == 0)
312796f2e892SBill Paul 				mii_tick(mii);
312896f2e892SBill Paul 		} else {
3129d0d67284SMarius Strobl 			/*
3130d0d67284SMarius Strobl 			 * For NICs which never report DC_RXSTATE_WAIT, we
3131d0d67284SMarius Strobl 			 * have to bite the bullet...
3132d0d67284SMarius Strobl 			 */
3133d0d67284SMarius Strobl 			if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
3134d0d67284SMarius Strobl 			    DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
3135d314ebf5SPyun YongHyeon 			    sc->dc_cdata.dc_tx_cnt == 0)
313696f2e892SBill Paul 				mii_tick(mii);
3137259b8d84SMartin Blapp 		}
313896f2e892SBill Paul 	} else
313996f2e892SBill Paul 		mii_tick(mii);
314096f2e892SBill Paul 
314196f2e892SBill Paul 	/*
314296f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
314396f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
314496f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
314596f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
314696f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
314796f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
314896f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
314996f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
315096f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
315196f2e892SBill Paul 	 * a screeching halt for several seconds.
315296f2e892SBill Paul 	 *
315396f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
315496f2e892SBill Paul 	 * any packets until a link has been established. After the
315596f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
315696f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
315796f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
315896f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
315996f2e892SBill Paul 	 */
3160d314ebf5SPyun YongHyeon 	if (sc->dc_link != 0 && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3161c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
316296f2e892SBill Paul 
3163318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
3164b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3165318b02fdSBill Paul 	else
3166b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
316796f2e892SBill Paul }
316896f2e892SBill Paul 
3169d467c136SBill Paul /*
3170d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
3171d467c136SBill Paul  * or switch to store and forward mode if we have to.
3172d467c136SBill Paul  */
3173e3d2833aSAlfred Perlstein static void
31740934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
3175d467c136SBill Paul {
3176d9efae03SPyun YongHyeon 	uint32_t netcfg, isr;
3177d9efae03SPyun YongHyeon 	int i, reinit;
3178d467c136SBill Paul 
3179d9efae03SPyun YongHyeon 	reinit = 0;
3180d9efae03SPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
3181d9efae03SPyun YongHyeon 	device_printf(sc->dc_dev, "TX underrun -- ");
3182d9efae03SPyun YongHyeon 	if ((sc->dc_flags & DC_TX_STORENFWD) == 0) {
3183d9efae03SPyun YongHyeon 		if (sc->dc_txthresh + DC_TXTHRESH_INC > DC_TXTHRESH_MAX) {
3184d9efae03SPyun YongHyeon 			printf("using store and forward mode\n");
3185d9efae03SPyun YongHyeon 			netcfg |= DC_NETCFG_STORENFWD;
3186d9efae03SPyun YongHyeon 		} else {
3187d9efae03SPyun YongHyeon 			printf("increasing TX threshold\n");
3188d9efae03SPyun YongHyeon 			sc->dc_txthresh += DC_TXTHRESH_INC;
3189d9efae03SPyun YongHyeon 			netcfg &= ~DC_NETCFG_TX_THRESH;
3190d9efae03SPyun YongHyeon 			netcfg |= sc->dc_txthresh;
31918f382a1fSPyun YongHyeon 		}
3192d467c136SBill Paul 
3193d467c136SBill Paul 		if (DC_IS_INTEL(sc)) {
3194d467c136SBill Paul 			/*
3195d467c136SBill Paul 			 * The real 21143 requires that the transmitter be idle
3196d467c136SBill Paul 			 * in order to change the transmit threshold or store
3197d467c136SBill Paul 			 * and forward state.
3198d467c136SBill Paul 			 */
3199d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg & ~DC_NETCFG_TX_ON);
3200d467c136SBill Paul 
3201d467c136SBill Paul 			for (i = 0; i < DC_TIMEOUT; i++) {
3202d467c136SBill Paul 				isr = CSR_READ_4(sc, DC_ISR);
3203d467c136SBill Paul 				if (isr & DC_ISR_TX_IDLE)
3204d467c136SBill Paul 					break;
3205d467c136SBill Paul 				DELAY(10);
3206d467c136SBill Paul 			}
3207d467c136SBill Paul 			if (i == DC_TIMEOUT) {
32086b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
3209432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
3210432120f2SMarius Strobl 				    __func__);
3211d9efae03SPyun YongHyeon 				reinit++;
3212d9efae03SPyun YongHyeon 			}
3213d9efae03SPyun YongHyeon 		}
3214d9efae03SPyun YongHyeon 	} else {
3215d9efae03SPyun YongHyeon 		printf("resetting\n");
3216d9efae03SPyun YongHyeon 		reinit++;
3217d9efae03SPyun YongHyeon 	}
3218d9efae03SPyun YongHyeon 
3219d9efae03SPyun YongHyeon 	if (reinit == 0) {
3220d9efae03SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, netcfg);
3221d9efae03SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3222d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg | DC_NETCFG_TX_ON);
3223d9efae03SPyun YongHyeon 	} else {
32248f382a1fSPyun YongHyeon 		sc->dc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3225c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3226d467c136SBill Paul 	}
3227d467c136SBill Paul }
3228d467c136SBill Paul 
3229e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3230e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3231e4fc250cSLuigi Rizzo 
32321abcdbd1SAttilio Rao static int
3233e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3234e4fc250cSLuigi Rizzo {
3235e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
32361abcdbd1SAttilio Rao 	int rx_npkts = 0;
3237e4fc250cSLuigi Rizzo 
323840929967SGleb Smirnoff 	DC_LOCK(sc);
323940929967SGleb Smirnoff 
324040929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
324140929967SGleb Smirnoff 		DC_UNLOCK(sc);
32421abcdbd1SAttilio Rao 		return (rx_npkts);
3243e4fc250cSLuigi Rizzo 	}
324440929967SGleb Smirnoff 
3245e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
32461abcdbd1SAttilio Rao 	rx_npkts = dc_rxeof(sc);
3247e4fc250cSLuigi Rizzo 	dc_txeof(sc);
324813f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
324913f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3250c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3251e4fc250cSLuigi Rizzo 
3252e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3253ee320f98SPyun YongHyeon 		uint32_t	status;
3254e4fc250cSLuigi Rizzo 
3255e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3256e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3257e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3258e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
32595120abbfSSam Leffler 		if (!status) {
32605120abbfSSam Leffler 			DC_UNLOCK(sc);
32611abcdbd1SAttilio Rao 			return (rx_npkts);
32625120abbfSSam Leffler 		}
3263e4fc250cSLuigi Rizzo 		/* ack what we have */
3264e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3265e4fc250cSLuigi Rizzo 
3266e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3267ee320f98SPyun YongHyeon 			uint32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3268e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3269e4fc250cSLuigi Rizzo 
3270e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3271e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3272e4fc250cSLuigi Rizzo 		}
3273e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3274e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3275e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3276e4fc250cSLuigi Rizzo 
3277e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3278e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3279e4fc250cSLuigi Rizzo 
3280e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
32816b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
32828f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3283c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3284e4fc250cSLuigi Rizzo 		}
3285e4fc250cSLuigi Rizzo 	}
32865120abbfSSam Leffler 	DC_UNLOCK(sc);
32871abcdbd1SAttilio Rao 	return (rx_npkts);
3288e4fc250cSLuigi Rizzo }
3289e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3290e4fc250cSLuigi Rizzo 
3291e3d2833aSAlfred Perlstein static void
32920934f18aSMaxime Henrion dc_intr(void *arg)
329396f2e892SBill Paul {
329496f2e892SBill Paul 	struct dc_softc *sc;
329596f2e892SBill Paul 	struct ifnet *ifp;
3296ee320f98SPyun YongHyeon 	uint32_t r, status;
3297848a02fcSPyun YongHyeon 	int n;
329896f2e892SBill Paul 
329996f2e892SBill Paul 	sc = arg;
3300d2a1864bSWarner Losh 
33010934f18aSMaxime Henrion 	if (sc->suspended)
3302e8388e14SMitsuru IWASAKI 		return;
3303e8388e14SMitsuru IWASAKI 
3304d1ce9105SBill Paul 	DC_LOCK(sc);
3305a84b4e80SPyun YongHyeon 	status = CSR_READ_4(sc, DC_ISR);
3306a84b4e80SPyun YongHyeon 	if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0) {
3307a84b4e80SPyun YongHyeon 		DC_UNLOCK(sc);
3308a84b4e80SPyun YongHyeon 		return;
3309a84b4e80SPyun YongHyeon 	}
3310fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3311e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
331240929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
331340929967SGleb Smirnoff 		DC_UNLOCK(sc);
331440929967SGleb Smirnoff 		return;
3315e4fc250cSLuigi Rizzo 	}
33160934f18aSMaxime Henrion #endif
331796f2e892SBill Paul 	/* Disable interrupts. */
331896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
331996f2e892SBill Paul 
3320a84b4e80SPyun YongHyeon 	for (n = 16; n > 0; n--) {
3321a84b4e80SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3322a84b4e80SPyun YongHyeon 			break;
3323a84b4e80SPyun YongHyeon 		/* Ack interrupts. */
332496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
332596f2e892SBill Paul 
332673bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
3327848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
332873bf949cSBill Paul 				while (dc_rx_resync(sc))
332973bf949cSBill Paul 					dc_rxeof(sc);
333073bf949cSBill Paul 			}
333173bf949cSBill Paul 		}
333296f2e892SBill Paul 
333396f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
333496f2e892SBill Paul 			dc_txeof(sc);
333596f2e892SBill Paul 
333696f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
333796f2e892SBill Paul 			dc_txeof(sc);
333896f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
333996f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
334096f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
334196f2e892SBill Paul 			}
334296f2e892SBill Paul 		}
334396f2e892SBill Paul 
3344d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3345d467c136SBill Paul 			dc_tx_underrun(sc);
334696f2e892SBill Paul 
334796f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
334873bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
334926b40a65SPyun YongHyeon 			r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
335026b40a65SPyun YongHyeon 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3351848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
335273bf949cSBill Paul 				while (dc_rx_resync(sc))
335373bf949cSBill Paul 					dc_rxeof(sc);
335473bf949cSBill Paul 			}
335573bf949cSBill Paul 		}
335696f2e892SBill Paul 
3357a84b4e80SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3358a84b4e80SPyun YongHyeon 			dc_start_locked(ifp);
3359a84b4e80SPyun YongHyeon 
336096f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
33618f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3362c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3363a84b4e80SPyun YongHyeon 			DC_UNLOCK(sc);
3364a84b4e80SPyun YongHyeon 			return;
336596f2e892SBill Paul 		}
3366a84b4e80SPyun YongHyeon 		status = CSR_READ_4(sc, DC_ISR);
3367a84b4e80SPyun YongHyeon 		if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0)
3368a84b4e80SPyun YongHyeon 			break;
336996f2e892SBill Paul 	}
337096f2e892SBill Paul 
337196f2e892SBill Paul 	/* Re-enable interrupts. */
3372a84b4e80SPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
337396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
337496f2e892SBill Paul 
3375d1ce9105SBill Paul 	DC_UNLOCK(sc);
337696f2e892SBill Paul }
337796f2e892SBill Paul 
337896f2e892SBill Paul /*
337996f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
338096f2e892SBill Paul  * pointers to the fragment pointers.
338196f2e892SBill Paul  */
3382e3d2833aSAlfred Perlstein static int
3383a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
338496f2e892SBill Paul {
3385ebc284ccSMarius Strobl 	bus_dma_segment_t segs[DC_MAXFRAGS];
33865f14ee23SPyun YongHyeon 	bus_dmamap_t map;
3387ebc284ccSMarius Strobl 	struct dc_desc *f;
338896f2e892SBill Paul 	struct mbuf *m;
3389993a741aSMarius Strobl 	int cur, defragged, error, first, frag, i, idx, nseg;
3390cda97c50SMike Silbersack 
3391993a741aSMarius Strobl 	m = NULL;
3392993a741aSMarius Strobl 	defragged = 0;
3393993a741aSMarius Strobl 	if (sc->dc_flags & DC_TX_COALESCE &&
3394993a741aSMarius Strobl 	    ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3395993a741aSMarius Strobl 		m = m_defrag(*m_head, M_DONTWAIT);
3396993a741aSMarius Strobl 		defragged = 1;
3397993a741aSMarius Strobl 	} else {
3398cda97c50SMike Silbersack 		/*
3399993a741aSMarius Strobl 		 * Count the number of frags in this chain to see if we
3400993a741aSMarius Strobl 		 * need to m_collapse.  Since the descriptor list is shared
3401993a741aSMarius Strobl 		 * by all packets, we'll m_collapse long chains so that they
3402cda97c50SMike Silbersack 		 * do not use up the entire list, even if they would fit.
3403cda97c50SMike Silbersack 		 */
3404993a741aSMarius Strobl 		i = 0;
3405a10c0e45SMike Silbersack 		for (m = *m_head; m != NULL; m = m->m_next)
3406993a741aSMarius Strobl 			i++;
3407993a741aSMarius Strobl 		if (i > DC_TX_LIST_CNT / 4 ||
3408993a741aSMarius Strobl 		    DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3409993a741aSMarius Strobl 		    DC_TX_LIST_RSVD) {
3410993a741aSMarius Strobl 			m = m_collapse(*m_head, M_DONTWAIT, DC_MAXFRAGS);
3411993a741aSMarius Strobl 			defragged = 1;
3412993a741aSMarius Strobl 		}
3413993a741aSMarius Strobl 	}
3414993a741aSMarius Strobl 	if (defragged != 0) {
341582a67a70SMarius Strobl 		if (m == NULL) {
341682a67a70SMarius Strobl 			m_freem(*m_head);
341782a67a70SMarius Strobl 			*m_head = NULL;
3418cda97c50SMike Silbersack 			return (ENOBUFS);
341982a67a70SMarius Strobl 		}
3420a10c0e45SMike Silbersack 		*m_head = m;
3421cda97c50SMike Silbersack 	}
3422993a741aSMarius Strobl 
342356e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
34245f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3425ebc284ccSMarius Strobl 	    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3426ebc284ccSMarius Strobl 	if (error == EFBIG) {
3427993a741aSMarius Strobl 		if (defragged != 0 || (m = m_collapse(*m_head, M_DONTWAIT,
3428993a741aSMarius Strobl 		    DC_MAXFRAGS)) == NULL) {
3429ebc284ccSMarius Strobl 			m_freem(*m_head);
343082a67a70SMarius Strobl 			*m_head = NULL;
3431993a741aSMarius Strobl 			return (defragged != 0 ? error : ENOBUFS);
343282a67a70SMarius Strobl 		}
3433ebc284ccSMarius Strobl 		*m_head = m;
34345f14ee23SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3435ebc284ccSMarius Strobl 		    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3436ebc284ccSMarius Strobl 		if (error != 0) {
3437ebc284ccSMarius Strobl 			m_freem(*m_head);
3438ebc284ccSMarius Strobl 			*m_head = NULL;
3439ebc284ccSMarius Strobl 			return (error);
344082a67a70SMarius Strobl 		}
3441ebc284ccSMarius Strobl 	} else if (error != 0)
3442ebc284ccSMarius Strobl 		return (error);
3443ebc284ccSMarius Strobl 	KASSERT(nseg <= DC_MAXFRAGS,
3444ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
3445ebc284ccSMarius Strobl 	if (nseg == 0) {
3446ebc284ccSMarius Strobl 		m_freem(*m_head);
3447ebc284ccSMarius Strobl 		*m_head = NULL;
3448ebc284ccSMarius Strobl 		return (EIO);
3449ebc284ccSMarius Strobl 	}
3450ebc284ccSMarius Strobl 
34515f14ee23SPyun YongHyeon 	/* Check descriptor overruns. */
34525f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt + nseg > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
34535f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
34545f14ee23SPyun YongHyeon 		return (ENOBUFS);
34555f14ee23SPyun YongHyeon 	}
34565f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
34575f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
34585f14ee23SPyun YongHyeon 
3459ebc284ccSMarius Strobl 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3460ebc284ccSMarius Strobl 	for (i = 0; i < nseg; i++) {
3461ebc284ccSMarius Strobl 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3462ebc284ccSMarius Strobl 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3463ebc284ccSMarius Strobl 		    (first != sc->dc_cdata.dc_tx_first)) {
34645f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_mtag,
3465ebc284ccSMarius Strobl 			    sc->dc_cdata.dc_tx_map[first]);
3466ebc284ccSMarius Strobl 			m_freem(*m_head);
3467ebc284ccSMarius Strobl 			*m_head = NULL;
3468ebc284ccSMarius Strobl 			return (ENOBUFS);
3469ebc284ccSMarius Strobl 		}
3470ebc284ccSMarius Strobl 
34715f14ee23SPyun YongHyeon 		f = &sc->dc_ldata.dc_tx_list[frag];
3472ebc284ccSMarius Strobl 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3473ebc284ccSMarius Strobl 		if (i == 0) {
3474ebc284ccSMarius Strobl 			f->dc_status = 0;
3475ebc284ccSMarius Strobl 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3476ebc284ccSMarius Strobl 		} else
3477ebc284ccSMarius Strobl 			f->dc_status = htole32(DC_TXSTAT_OWN);
34785f14ee23SPyun YongHyeon 		f->dc_data = htole32(DC_ADDR_LO(segs[i].ds_addr));
3479ebc284ccSMarius Strobl 		cur = frag;
3480ebc284ccSMarius Strobl 		DC_INC(frag, DC_TX_LIST_CNT);
3481ebc284ccSMarius Strobl 	}
3482ebc284ccSMarius Strobl 
3483ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_prod = frag;
3484ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_cnt += nseg;
3485ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_chain[cur] = *m_head;
34865f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3487ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
34885f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[first].dc_ctl |=
3489ebc284ccSMarius Strobl 		    htole32(DC_TXCTL_FINT);
3490ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
34915f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
349206d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR &&
349306d23883SPyun YongHyeon 	    ++sc->dc_cdata.dc_tx_pkts >= 8) {
349406d23883SPyun YongHyeon 		sc->dc_cdata.dc_tx_pkts = 0;
34955f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
349606d23883SPyun YongHyeon 	}
34975f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3498ebc284ccSMarius Strobl 
34995f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
35005f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
35015f14ee23SPyun YongHyeon 
35025f14ee23SPyun YongHyeon 	/*
35035f14ee23SPyun YongHyeon 	 * Swap the last and the first dmamaps to ensure the map for
35045f14ee23SPyun YongHyeon 	 * this transmission is placed at the last descriptor.
35055f14ee23SPyun YongHyeon 	 */
35065f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_tx_map[cur];
35075f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[cur] = sc->dc_cdata.dc_tx_map[first];
35085f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[first] = map;
35095f14ee23SPyun YongHyeon 
351096f2e892SBill Paul 	return (0);
351196f2e892SBill Paul }
351296f2e892SBill Paul 
3513e3d2833aSAlfred Perlstein static void
35140934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
351596f2e892SBill Paul {
351696f2e892SBill Paul 	struct dc_softc *sc;
3517c8b27acaSJohn Baldwin 
3518c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3519c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3520c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3521c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3522c8b27acaSJohn Baldwin }
3523c8b27acaSJohn Baldwin 
3524ebc284ccSMarius Strobl /*
3525ebc284ccSMarius Strobl  * Main transmit routine
3526ebc284ccSMarius Strobl  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3527ebc284ccSMarius Strobl  * regions directly in the transmit lists.  We also save a copy of the
3528ebc284ccSMarius Strobl  * pointers since the transmit list fragment pointers are physical
3529ebc284ccSMarius Strobl  * addresses.
3530ebc284ccSMarius Strobl  */
3531c8b27acaSJohn Baldwin static void
3532c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3533c8b27acaSJohn Baldwin {
3534c8b27acaSJohn Baldwin 	struct dc_softc *sc;
35355f14ee23SPyun YongHyeon 	struct mbuf *m_head;
35365f14ee23SPyun YongHyeon 	int queued;
353796f2e892SBill Paul 
353896f2e892SBill Paul 	sc = ifp->if_softc;
353996f2e892SBill Paul 
3540c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
354196f2e892SBill Paul 
354276d40c85SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
354376d40c85SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->dc_link == 0)
3544d1ce9105SBill Paul 		return;
354596f2e892SBill Paul 
35465f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
354796f2e892SBill Paul 
35485f14ee23SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
35495f14ee23SPyun YongHyeon 		/*
35505f14ee23SPyun YongHyeon 		 * If there's no way we can send any packets, return now.
35515f14ee23SPyun YongHyeon 		 */
35525f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
35535f14ee23SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
35545f14ee23SPyun YongHyeon 			break;
35555f14ee23SPyun YongHyeon 		}
3556cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
355796f2e892SBill Paul 		if (m_head == NULL)
355896f2e892SBill Paul 			break;
355996f2e892SBill Paul 
3560a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
356182a67a70SMarius Strobl 			if (m_head == NULL)
356282a67a70SMarius Strobl 				break;
3563cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
356413f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
356596f2e892SBill Paul 			break;
356696f2e892SBill Paul 		}
356796f2e892SBill Paul 
3568cbaf877fSBrian Feldman 		queued++;
356996f2e892SBill Paul 		/*
357096f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
357196f2e892SBill Paul 		 * to him.
357296f2e892SBill Paul 		 */
35739ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
357496f2e892SBill Paul 	}
357596f2e892SBill Paul 
3576cbaf877fSBrian Feldman 	if (queued > 0) {
357796f2e892SBill Paul 		/* Transmit */
357896f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
357996f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
358096f2e892SBill Paul 
358196f2e892SBill Paul 		/*
358296f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
358396f2e892SBill Paul 		 */
3584b1d16143SMarius Strobl 		sc->dc_wdog_timer = 5;
3585cbaf877fSBrian Feldman 	}
358696f2e892SBill Paul }
358796f2e892SBill Paul 
3588e3d2833aSAlfred Perlstein static void
35890934f18aSMaxime Henrion dc_init(void *xsc)
359096f2e892SBill Paul {
359196f2e892SBill Paul 	struct dc_softc *sc = xsc;
3592c8b27acaSJohn Baldwin 
3593c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3594c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3595c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3596c8b27acaSJohn Baldwin }
3597c8b27acaSJohn Baldwin 
3598c8b27acaSJohn Baldwin static void
3599c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3600c8b27acaSJohn Baldwin {
3601fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
360296f2e892SBill Paul 	struct mii_data *mii;
3603d314ebf5SPyun YongHyeon 	struct ifmedia *ifm;
360496f2e892SBill Paul 
3605c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
360696f2e892SBill Paul 
36078f382a1fSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
36088f382a1fSPyun YongHyeon 		return;
36098f382a1fSPyun YongHyeon 
361096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
361196f2e892SBill Paul 
361296f2e892SBill Paul 	/*
361396f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
361496f2e892SBill Paul 	 */
361596f2e892SBill Paul 	dc_stop(sc);
361696f2e892SBill Paul 	dc_reset(sc);
3617d314ebf5SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
3618d314ebf5SPyun YongHyeon 		ifm = &mii->mii_media;
3619d314ebf5SPyun YongHyeon 		dc_apply_fixup(sc, ifm->ifm_media);
3620d314ebf5SPyun YongHyeon 	}
362196f2e892SBill Paul 
362296f2e892SBill Paul 	/*
362396f2e892SBill Paul 	 * Set cache alignment and burst length.
362496f2e892SBill Paul 	 */
362552ca7ee2SPyun YongHyeon 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc) || DC_IS_ULI(sc))
362696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
362796f2e892SBill Paul 	else
362896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3629935fe010SLuigi Rizzo 	/*
3630935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3631935fe010SLuigi Rizzo 	 */
3632935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3633935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
363496f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
363596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
363696f2e892SBill Paul 	} else {
363796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
363896f2e892SBill Paul 	}
363996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
364096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
364196f2e892SBill Paul 	switch(sc->dc_cachesize) {
364296f2e892SBill Paul 	case 32:
364396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
364496f2e892SBill Paul 		break;
364596f2e892SBill Paul 	case 16:
364696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
364796f2e892SBill Paul 		break;
364896f2e892SBill Paul 	case 8:
364996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
365096f2e892SBill Paul 		break;
365196f2e892SBill Paul 	case 0:
365296f2e892SBill Paul 	default:
365396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
365496f2e892SBill Paul 		break;
365596f2e892SBill Paul 	}
365696f2e892SBill Paul 
365796f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
365896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365996f2e892SBill Paul 	else {
3660d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
366196f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
366296f2e892SBill Paul 		} else {
366396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
366496f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
366596f2e892SBill Paul 		}
366696f2e892SBill Paul 	}
366796f2e892SBill Paul 
366896f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
366996f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
367096f2e892SBill Paul 
367196f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
367296f2e892SBill Paul 		/*
367396f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
367496f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
367596f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
367696f2e892SBill Paul 		 * document the meaning of these bits so there's no way
367796f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
367896f2e892SBill Paul 		 * number all its own; the rest all use a different one.
367996f2e892SBill Paul 		 */
368096f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
368196f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
368296f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
368396f2e892SBill Paul 		else
368496f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
368596f2e892SBill Paul 	}
368696f2e892SBill Paul 
3687feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3688feb78939SJonathan Chen 		/*
3689feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3690feb78939SJonathan Chen 		 * can talk to the MII.
3691feb78939SJonathan Chen 		 */
3692feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3693feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3694feb78939SJonathan Chen 		DELAY(10);
3695feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3696feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3697feb78939SJonathan Chen 		DELAY(10);
3698feb78939SJonathan Chen 	}
3699feb78939SJonathan Chen 
370096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3701d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
370296f2e892SBill Paul 
370396f2e892SBill Paul 	/* Init circular RX list. */
370496f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
37056b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
370622f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
370796f2e892SBill Paul 		dc_stop(sc);
370896f2e892SBill Paul 		return;
370996f2e892SBill Paul 	}
371096f2e892SBill Paul 
371196f2e892SBill Paul 	/*
371256e5e7aeSMaxime Henrion 	 * Init TX descriptors.
371396f2e892SBill Paul 	 */
371496f2e892SBill Paul 	dc_list_tx_init(sc);
371596f2e892SBill Paul 
371696f2e892SBill Paul 	/*
371796f2e892SBill Paul 	 * Load the address of the RX list.
371896f2e892SBill Paul 	 */
371956e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
372056e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
372196f2e892SBill Paul 
372296f2e892SBill Paul 	/*
372396f2e892SBill Paul 	 * Enable interrupts.
372496f2e892SBill Paul 	 */
3725e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3726e4fc250cSLuigi Rizzo 	/*
3727e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3728e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3729e4fc250cSLuigi Rizzo 	 * after a reset.
3730e4fc250cSLuigi Rizzo 	 */
373140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3732e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3733e4fc250cSLuigi Rizzo 	else
3734e4fc250cSLuigi Rizzo #endif
373596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
373696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
373796f2e892SBill Paul 
373852ca7ee2SPyun YongHyeon 	/* Initialize TX jabber and RX watchdog timer. */
373952ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
374052ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_WATCHDOG, DC_WDOG_JABBERCLK |
374152ca7ee2SPyun YongHyeon 		    DC_WDOG_HOSTUNJAB);
374252ca7ee2SPyun YongHyeon 
374396f2e892SBill Paul 	/* Enable transmitter. */
374496f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
374596f2e892SBill Paul 
374696f2e892SBill Paul 	/*
3747918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3748918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3749918434c8SBill Paul 	 * link and activity indications.
3750918434c8SBill Paul 	 */
375178999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3752918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3753918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
375478999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3755918434c8SBill Paul 	}
3756918434c8SBill Paul 
3757918434c8SBill Paul 	/*
375896f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
375996f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
376096f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
376196f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
376296f2e892SBill Paul 	 */
376396f2e892SBill Paul 	dc_setfilt(sc);
376496f2e892SBill Paul 
376596f2e892SBill Paul 	/* Enable receiver. */
376696f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
376796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
376896f2e892SBill Paul 
376913f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
377013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
377196f2e892SBill Paul 
3772d7e9ac75SPyun YongHyeon 	dc_ifmedia_upd_locked(sc);
3773d314ebf5SPyun YongHyeon 
377426b40a65SPyun YongHyeon 	/* Clear missed frames and overflow counter. */
377526b40a65SPyun YongHyeon 	CSR_READ_4(sc, DC_FRAMESDISCARDED);
377626b40a65SPyun YongHyeon 
3777857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
377845521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3779857fd445SBill Paul 		sc->dc_link = 1;
3780857fd445SBill Paul 	else {
3781318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3782b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3783318b02fdSBill Paul 		else
3784b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3785857fd445SBill Paul 	}
3786b1d16143SMarius Strobl 
3787b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
3788b1d16143SMarius Strobl 	callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
378996f2e892SBill Paul }
379096f2e892SBill Paul 
379196f2e892SBill Paul /*
379296f2e892SBill Paul  * Set media options.
379396f2e892SBill Paul  */
3794e3d2833aSAlfred Perlstein static int
37950934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
379696f2e892SBill Paul {
379796f2e892SBill Paul 	struct dc_softc *sc;
3798d7e9ac75SPyun YongHyeon 	int error;
379996f2e892SBill Paul 
380096f2e892SBill Paul 	sc = ifp->if_softc;
3801c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3802d7e9ac75SPyun YongHyeon 	error = dc_ifmedia_upd_locked(sc);
3803d7e9ac75SPyun YongHyeon 	DC_UNLOCK(sc);
3804d7e9ac75SPyun YongHyeon 	return (error);
3805d7e9ac75SPyun YongHyeon }
3806f43d9309SBill Paul 
3807d7e9ac75SPyun YongHyeon static int
3808d7e9ac75SPyun YongHyeon dc_ifmedia_upd_locked(struct dc_softc *sc)
3809d7e9ac75SPyun YongHyeon {
3810d7e9ac75SPyun YongHyeon 	struct mii_data *mii;
3811d7e9ac75SPyun YongHyeon 	struct ifmedia *ifm;
3812d7e9ac75SPyun YongHyeon 	int error;
3813d7e9ac75SPyun YongHyeon 
3814d7e9ac75SPyun YongHyeon 	DC_LOCK_ASSERT(sc);
3815d7e9ac75SPyun YongHyeon 
3816d7e9ac75SPyun YongHyeon 	sc->dc_link = 0;
3817d7e9ac75SPyun YongHyeon 	mii = device_get_softc(sc->dc_miibus);
3818d7e9ac75SPyun YongHyeon 	error = mii_mediachg(mii);
3819d7e9ac75SPyun YongHyeon 	if (error == 0) {
3820d7e9ac75SPyun YongHyeon 		ifm = &mii->mii_media;
3821d314ebf5SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3822d314ebf5SPyun YongHyeon 			dc_setcfg(sc, ifm->ifm_media);
3823d314ebf5SPyun YongHyeon 		else if (DC_IS_DAVICOM(sc) &&
382445521525SPoul-Henning Kamp 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3825f43d9309SBill Paul 			dc_setcfg(sc, ifm->ifm_media);
3826d7e9ac75SPyun YongHyeon 	}
382796f2e892SBill Paul 
3828d7e9ac75SPyun YongHyeon 	return (error);
382996f2e892SBill Paul }
383096f2e892SBill Paul 
383196f2e892SBill Paul /*
383296f2e892SBill Paul  * Report current media status.
383396f2e892SBill Paul  */
3834e3d2833aSAlfred Perlstein static void
38350934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
383696f2e892SBill Paul {
383796f2e892SBill Paul 	struct dc_softc *sc;
383896f2e892SBill Paul 	struct mii_data *mii;
3839f43d9309SBill Paul 	struct ifmedia *ifm;
384096f2e892SBill Paul 
384196f2e892SBill Paul 	sc = ifp->if_softc;
384296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3843c8b27acaSJohn Baldwin 	DC_LOCK(sc);
384496f2e892SBill Paul 	mii_pollstat(mii);
3845f43d9309SBill Paul 	ifm = &mii->mii_media;
3846f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
384745521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3848f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3849f43d9309SBill Paul 			ifmr->ifm_status = 0;
3850432120f2SMarius Strobl 			DC_UNLOCK(sc);
3851f43d9309SBill Paul 			return;
3852f43d9309SBill Paul 		}
3853f43d9309SBill Paul 	}
385496f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
385596f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3856c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
385796f2e892SBill Paul }
385896f2e892SBill Paul 
3859e3d2833aSAlfred Perlstein static int
38600934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
386196f2e892SBill Paul {
386296f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
386396f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
386496f2e892SBill Paul 	struct mii_data *mii;
3865d1ce9105SBill Paul 	int error = 0;
386696f2e892SBill Paul 
386796f2e892SBill Paul 	switch (command) {
386896f2e892SBill Paul 	case SIOCSIFFLAGS:
3869c8b27acaSJohn Baldwin 		DC_LOCK(sc);
387096f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
38715d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
38725d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
38735d6dfbbbSLuigi Rizzo 
387413f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
38755d6dfbbbSLuigi Rizzo 				if (need_setfilt)
387696f2e892SBill Paul 					dc_setfilt(sc);
38775d6dfbbbSLuigi Rizzo 			} else {
38788f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3879c8b27acaSJohn Baldwin 				dc_init_locked(sc);
388096f2e892SBill Paul 			}
388196f2e892SBill Paul 		} else {
388213f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
388396f2e892SBill Paul 				dc_stop(sc);
388496f2e892SBill Paul 		}
388596f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3886c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
388796f2e892SBill Paul 		break;
388896f2e892SBill Paul 	case SIOCADDMULTI:
388996f2e892SBill Paul 	case SIOCDELMULTI:
3890c8b27acaSJohn Baldwin 		DC_LOCK(sc);
389124507bc1SPyun YongHyeon 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
389296f2e892SBill Paul 			dc_setfilt(sc);
3893c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
389496f2e892SBill Paul 		break;
389596f2e892SBill Paul 	case SIOCGIFMEDIA:
389696f2e892SBill Paul 	case SIOCSIFMEDIA:
389796f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
389896f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
389996f2e892SBill Paul 		break;
3900e695984eSRuslan Ermilov 	case SIOCSIFCAP:
390140929967SGleb Smirnoff #ifdef DEVICE_POLLING
390240929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
390340929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
390440929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
390540929967SGleb Smirnoff 			if (error)
390640929967SGleb Smirnoff 				return(error);
3907c8b27acaSJohn Baldwin 			DC_LOCK(sc);
390840929967SGleb Smirnoff 			/* Disable interrupts */
390940929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
391040929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3911c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
391240929967SGleb Smirnoff 			return (error);
391340929967SGleb Smirnoff 		}
391440929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
391540929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
391640929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
391740929967SGleb Smirnoff 			/* Enable interrupts. */
391840929967SGleb Smirnoff 			DC_LOCK(sc);
391940929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
392040929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
392140929967SGleb Smirnoff 			DC_UNLOCK(sc);
392240929967SGleb Smirnoff 			return (error);
392340929967SGleb Smirnoff 		}
392440929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3925e695984eSRuslan Ermilov 		break;
392696f2e892SBill Paul 	default:
39279ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
392896f2e892SBill Paul 		break;
392996f2e892SBill Paul 	}
393096f2e892SBill Paul 
393196f2e892SBill Paul 	return (error);
393296f2e892SBill Paul }
393396f2e892SBill Paul 
3934e3d2833aSAlfred Perlstein static void
3935b1d16143SMarius Strobl dc_watchdog(void *xsc)
393696f2e892SBill Paul {
3937b1d16143SMarius Strobl 	struct dc_softc *sc = xsc;
3938b1d16143SMarius Strobl 	struct ifnet *ifp;
393996f2e892SBill Paul 
3940b1d16143SMarius Strobl 	DC_LOCK_ASSERT(sc);
394196f2e892SBill Paul 
3942b1d16143SMarius Strobl 	if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3943b1d16143SMarius Strobl 		callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3944b1d16143SMarius Strobl 		return;
3945b1d16143SMarius Strobl 	}
3946d1ce9105SBill Paul 
3947b1d16143SMarius Strobl 	ifp = sc->dc_ifp;
394896f2e892SBill Paul 	ifp->if_oerrors++;
3949b1d16143SMarius Strobl 	device_printf(sc->dc_dev, "watchdog timeout\n");
395096f2e892SBill Paul 
39518f382a1fSPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3952c8b27acaSJohn Baldwin 	dc_init_locked(sc);
395396f2e892SBill Paul 
3954cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3955c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
395696f2e892SBill Paul }
395796f2e892SBill Paul 
395896f2e892SBill Paul /*
395996f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
396096f2e892SBill Paul  * RX and TX lists.
396196f2e892SBill Paul  */
3962e3d2833aSAlfred Perlstein static void
39630934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
396496f2e892SBill Paul {
396596f2e892SBill Paul 	struct ifnet *ifp;
3966b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3967b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3968b3811c95SMaxime Henrion 	int i;
39691da7683aSPyun YongHyeon 	uint32_t ctl, netcfg;
397096f2e892SBill Paul 
3971c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3972d1ce9105SBill Paul 
3973fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
39745f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
3975b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
397696f2e892SBill Paul 
3977b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
3978b1d16143SMarius Strobl 	callout_stop(&sc->dc_wdog_ch);
3979b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
39801da7683aSPyun YongHyeon 	sc->dc_link = 0;
398196f2e892SBill Paul 
398213f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
39833b3ec200SPeter Wemm 
39841da7683aSPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
39851da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39861da7683aSPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
39871da7683aSPyun YongHyeon 		   netcfg & ~(DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
398896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
39891da7683aSPyun YongHyeon 	/* Wait the completion of TX/RX SM. */
39901da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39911da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
39921da7683aSPyun YongHyeon 
399396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
399496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
399596f2e892SBill Paul 
399696f2e892SBill Paul 	/*
399796f2e892SBill Paul 	 * Free data in the RX lists.
399896f2e892SBill Paul 	 */
399996f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
4000b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
40015f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_rx_mtag,
40025f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i], BUS_DMASYNC_POSTREAD);
40035f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_mtag,
40045f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i]);
400556e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
400656e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
400796f2e892SBill Paul 		}
400896f2e892SBill Paul 	}
40095f14ee23SPyun YongHyeon 	bzero(ld->dc_rx_list, DC_RX_LIST_SZ);
40105f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
40115f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
401296f2e892SBill Paul 
401396f2e892SBill Paul 	/*
401496f2e892SBill Paul 	 * Free the TX list buffers.
401596f2e892SBill Paul 	 */
401696f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
4017b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
4018af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
40195f14ee23SPyun YongHyeon 			if (ctl & DC_TXCTL_SETUP) {
40205f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
40215f14ee23SPyun YongHyeon 				    BUS_DMASYNC_POSTWRITE);
40225f14ee23SPyun YongHyeon 			} else {
40235f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_tx_mtag,
40245f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i], BUS_DMASYNC_POSTWRITE);
40255f14ee23SPyun YongHyeon 				bus_dmamap_unload(sc->dc_tx_mtag,
40265f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i]);
402756e5e7aeSMaxime Henrion 				m_freem(cd->dc_tx_chain[i]);
40285f14ee23SPyun YongHyeon 			}
4029b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
403096f2e892SBill Paul 		}
403196f2e892SBill Paul 	}
40325f14ee23SPyun YongHyeon 	bzero(ld->dc_tx_list, DC_TX_LIST_SZ);
40335f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
40345f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
403596f2e892SBill Paul }
403696f2e892SBill Paul 
403796f2e892SBill Paul /*
4038e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
4039e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
4040e8388e14SMitsuru IWASAKI  * resume.
4041e8388e14SMitsuru IWASAKI  */
4042e3d2833aSAlfred Perlstein static int
40430934f18aSMaxime Henrion dc_suspend(device_t dev)
4044e8388e14SMitsuru IWASAKI {
4045e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4046e8388e14SMitsuru IWASAKI 
4047e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4048c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4049e8388e14SMitsuru IWASAKI 	dc_stop(sc);
4050e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
4051c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4052e8388e14SMitsuru IWASAKI 
4053e8388e14SMitsuru IWASAKI 	return (0);
4054e8388e14SMitsuru IWASAKI }
4055e8388e14SMitsuru IWASAKI 
4056e8388e14SMitsuru IWASAKI /*
4057e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
4058e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
4059e8388e14SMitsuru IWASAKI  * appropriate.
4060e8388e14SMitsuru IWASAKI  */
4061e3d2833aSAlfred Perlstein static int
40620934f18aSMaxime Henrion dc_resume(device_t dev)
4063e8388e14SMitsuru IWASAKI {
4064e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4065e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
4066e8388e14SMitsuru IWASAKI 
4067e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4068fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
4069e8388e14SMitsuru IWASAKI 
4070e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
4071c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4072e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
4073c8b27acaSJohn Baldwin 		dc_init_locked(sc);
4074e8388e14SMitsuru IWASAKI 
4075e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
4076c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4077e8388e14SMitsuru IWASAKI 
4078e8388e14SMitsuru IWASAKI 	return (0);
4079e8388e14SMitsuru IWASAKI }
4080e8388e14SMitsuru IWASAKI 
4081e8388e14SMitsuru IWASAKI /*
408296f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
408396f2e892SBill Paul  * get confused by errant DMAs when rebooting.
408496f2e892SBill Paul  */
40856a087a87SPyun YongHyeon static int
40860934f18aSMaxime Henrion dc_shutdown(device_t dev)
408796f2e892SBill Paul {
408896f2e892SBill Paul 	struct dc_softc *sc;
408996f2e892SBill Paul 
409096f2e892SBill Paul 	sc = device_get_softc(dev);
409196f2e892SBill Paul 
4092c8b27acaSJohn Baldwin 	DC_LOCK(sc);
409396f2e892SBill Paul 	dc_stop(sc);
4094c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
40956a087a87SPyun YongHyeon 
40966a087a87SPyun YongHyeon 	return (0);
409796f2e892SBill Paul }
409839d76ed6SPyun YongHyeon 
409939d76ed6SPyun YongHyeon static int
410039d76ed6SPyun YongHyeon dc_check_multiport(struct dc_softc *sc)
410139d76ed6SPyun YongHyeon {
410239d76ed6SPyun YongHyeon 	struct dc_softc *dsc;
410339d76ed6SPyun YongHyeon 	devclass_t dc;
410439d76ed6SPyun YongHyeon 	device_t child;
410539d76ed6SPyun YongHyeon 	uint8_t *eaddr;
410639d76ed6SPyun YongHyeon 	int unit;
410739d76ed6SPyun YongHyeon 
410839d76ed6SPyun YongHyeon 	dc = devclass_find("dc");
410939d76ed6SPyun YongHyeon 	for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
411039d76ed6SPyun YongHyeon 		child = devclass_get_device(dc, unit);
411139d76ed6SPyun YongHyeon 		if (child == NULL)
411239d76ed6SPyun YongHyeon 			continue;
411339d76ed6SPyun YongHyeon 		if (child == sc->dc_dev)
411439d76ed6SPyun YongHyeon 			continue;
411539d76ed6SPyun YongHyeon 		if (device_get_parent(child) != device_get_parent(sc->dc_dev))
411639d76ed6SPyun YongHyeon 			continue;
411739d76ed6SPyun YongHyeon 		if (unit > device_get_unit(sc->dc_dev))
411839d76ed6SPyun YongHyeon 			continue;
4119b289c607SPyun YongHyeon 		if (device_is_attached(child) == 0)
4120b289c607SPyun YongHyeon 			continue;
412139d76ed6SPyun YongHyeon 		dsc = device_get_softc(child);
4122b289c607SPyun YongHyeon 		device_printf(sc->dc_dev,
4123b289c607SPyun YongHyeon 		    "Using station address of %s as base\n",
412439d76ed6SPyun YongHyeon 		    device_get_nameunit(child));
412539d76ed6SPyun YongHyeon 		bcopy(dsc->dc_eaddr, sc->dc_eaddr, ETHER_ADDR_LEN);
412639d76ed6SPyun YongHyeon 		eaddr = (uint8_t *)sc->dc_eaddr;
412739d76ed6SPyun YongHyeon 		eaddr[5]++;
4128b289c607SPyun YongHyeon 		/* Prepare SROM to parse again. */
4129b289c607SPyun YongHyeon 		if (DC_IS_INTEL(sc) && dsc->dc_srom != NULL &&
4130b289c607SPyun YongHyeon 		    sc->dc_romwidth != 0) {
4131b289c607SPyun YongHyeon 			free(sc->dc_srom, M_DEVBUF);
4132b289c607SPyun YongHyeon 			sc->dc_romwidth = dsc->dc_romwidth;
4133b289c607SPyun YongHyeon 			sc->dc_srom = malloc(DC_ROM_SIZE(sc->dc_romwidth),
4134b289c607SPyun YongHyeon 			    M_DEVBUF, M_NOWAIT);
4135b289c607SPyun YongHyeon 			if (sc->dc_srom == NULL) {
4136b289c607SPyun YongHyeon 				device_printf(sc->dc_dev,
4137b289c607SPyun YongHyeon 				    "Could not allocate SROM buffer\n");
4138b289c607SPyun YongHyeon 				return (ENOMEM);
4139b289c607SPyun YongHyeon 			}
4140b289c607SPyun YongHyeon 			bcopy(dsc->dc_srom, sc->dc_srom,
4141b289c607SPyun YongHyeon 			    DC_ROM_SIZE(sc->dc_romwidth));
4142b289c607SPyun YongHyeon 		}
414339d76ed6SPyun YongHyeon 		return (0);
414439d76ed6SPyun YongHyeon 	}
414539d76ed6SPyun YongHyeon 	return (ENOENT);
414639d76ed6SPyun YongHyeon }
4147