xref: /freebsd/sys/dev/dc/if_dc.c (revision c6499eccad497913a5025fbde8ae76da70e08043)
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  */
15329658c96SDimitry Andric static const struct dc_type 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 	/* MII interface */
34196f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
34296f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
34396f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
344f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
34596f2e892SBill Paul 
3464b7ec270SMarius Strobl 	DEVMETHOD_END
34796f2e892SBill Paul };
34896f2e892SBill Paul 
34996f2e892SBill Paul static driver_t dc_driver = {
35096f2e892SBill Paul 	"dc",
35196f2e892SBill Paul 	dc_methods,
35296f2e892SBill Paul 	sizeof(struct dc_softc)
35396f2e892SBill Paul };
35496f2e892SBill Paul 
35596f2e892SBill Paul static devclass_t dc_devclass;
35696f2e892SBill Paul 
357e4029d4cSMarius Strobl DRIVER_MODULE_ORDERED(dc, pci, dc_driver, dc_devclass, NULL, NULL,
358e4029d4cSMarius Strobl     SI_ORDER_ANY);
359e4029d4cSMarius Strobl DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, NULL, NULL);
36096f2e892SBill Paul 
36196f2e892SBill Paul #define	DC_SETBIT(sc, reg, x)				\
36296f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
36396f2e892SBill Paul 
36496f2e892SBill Paul #define	DC_CLRBIT(sc, reg, x)				\
36596f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
36696f2e892SBill Paul 
36796f2e892SBill Paul #define	SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
36896f2e892SBill Paul #define	SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
36996f2e892SBill Paul 
370e3d2833aSAlfred Perlstein static void
3710934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
37296f2e892SBill Paul {
37396f2e892SBill Paul 	int idx;
37496f2e892SBill Paul 
37596f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
37696f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
37796f2e892SBill Paul }
37896f2e892SBill Paul 
3792c876e15SPoul-Henning Kamp static void
3800934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3813097aa70SWarner Losh {
3823097aa70SWarner Losh 	int i;
3833097aa70SWarner Losh 
3843097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3853097aa70SWarner Losh 	dc_eeprom_idle(sc);
3863097aa70SWarner Losh 
3873097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3883097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3893097aa70SWarner Losh 	dc_delay(sc);
3903097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3913097aa70SWarner Losh 	dc_delay(sc);
3923097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3933097aa70SWarner Losh 	dc_delay(sc);
3943097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3953097aa70SWarner Losh 	dc_delay(sc);
3963097aa70SWarner Losh 
3973097aa70SWarner Losh 	for (i = 3; i--;) {
3983097aa70SWarner Losh 		if (6 & (1 << i))
3993097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4003097aa70SWarner Losh 		else
4013097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4023097aa70SWarner Losh 		dc_delay(sc);
4033097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4043097aa70SWarner Losh 		dc_delay(sc);
4053097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4063097aa70SWarner Losh 		dc_delay(sc);
4073097aa70SWarner Losh 	}
4083097aa70SWarner Losh 
4093097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
4103097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4113097aa70SWarner Losh 		dc_delay(sc);
4123097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
4133097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4143097aa70SWarner Losh 			dc_delay(sc);
4153097aa70SWarner Losh 			break;
4163097aa70SWarner Losh 		}
4173097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4183097aa70SWarner Losh 		dc_delay(sc);
4193097aa70SWarner Losh 	}
4203097aa70SWarner Losh 
4213097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4223097aa70SWarner Losh 	dc_eeprom_idle(sc);
4233097aa70SWarner Losh 
4243097aa70SWarner Losh 	if (i < 4 || i > 12)
4253097aa70SWarner Losh 		sc->dc_romwidth = 6;
4263097aa70SWarner Losh 	else
4273097aa70SWarner Losh 		sc->dc_romwidth = i;
4283097aa70SWarner Losh 
4293097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4303097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4313097aa70SWarner Losh 	dc_delay(sc);
4323097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4333097aa70SWarner Losh 	dc_delay(sc);
4343097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4353097aa70SWarner Losh 	dc_delay(sc);
4363097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4373097aa70SWarner Losh 	dc_delay(sc);
4383097aa70SWarner Losh 
4393097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4403097aa70SWarner Losh 	dc_eeprom_idle(sc);
4413097aa70SWarner Losh }
4423097aa70SWarner Losh 
443e3d2833aSAlfred Perlstein static void
4440934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
44596f2e892SBill Paul {
4460934f18aSMaxime Henrion 	int i;
44796f2e892SBill Paul 
44896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
44996f2e892SBill Paul 	dc_delay(sc);
45096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
45196f2e892SBill Paul 	dc_delay(sc);
45296f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45396f2e892SBill Paul 	dc_delay(sc);
45496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
45596f2e892SBill Paul 	dc_delay(sc);
45696f2e892SBill Paul 
45796f2e892SBill Paul 	for (i = 0; i < 25; i++) {
45896f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45996f2e892SBill Paul 		dc_delay(sc);
46096f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46196f2e892SBill Paul 		dc_delay(sc);
46296f2e892SBill Paul 	}
46396f2e892SBill Paul 
46496f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
46596f2e892SBill Paul 	dc_delay(sc);
46696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
46796f2e892SBill Paul 	dc_delay(sc);
46896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
46996f2e892SBill Paul }
47096f2e892SBill Paul 
47196f2e892SBill Paul /*
47296f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
47396f2e892SBill Paul  */
474e3d2833aSAlfred Perlstein static void
4750934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
47696f2e892SBill Paul {
4770934f18aSMaxime Henrion 	int d, i;
47896f2e892SBill Paul 
4793097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4803097aa70SWarner Losh 	for (i = 3; i--; ) {
4813097aa70SWarner Losh 		if (d & (1 << i))
4823097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
48396f2e892SBill Paul 		else
4843097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4853097aa70SWarner Losh 		dc_delay(sc);
4863097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4873097aa70SWarner Losh 		dc_delay(sc);
4883097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4893097aa70SWarner Losh 		dc_delay(sc);
4903097aa70SWarner Losh 	}
49196f2e892SBill Paul 
49296f2e892SBill Paul 	/*
49396f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
49496f2e892SBill Paul 	 */
4953097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4963097aa70SWarner Losh 		if (addr & (1 << i)) {
49796f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
49896f2e892SBill Paul 		} else {
49996f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
50096f2e892SBill Paul 		}
50196f2e892SBill Paul 		dc_delay(sc);
50296f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
50396f2e892SBill Paul 		dc_delay(sc);
50496f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
50596f2e892SBill Paul 		dc_delay(sc);
50696f2e892SBill Paul 	}
50796f2e892SBill Paul }
50896f2e892SBill Paul 
50996f2e892SBill Paul /*
51096f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
51196f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
51296f2e892SBill Paul  * the EEPROM.
51396f2e892SBill Paul  */
514e3d2833aSAlfred Perlstein static void
515ee320f98SPyun YongHyeon dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, uint16_t *dest)
51696f2e892SBill Paul {
5170934f18aSMaxime Henrion 	int i;
518ee320f98SPyun YongHyeon 	uint32_t r;
51996f2e892SBill Paul 
52096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
52196f2e892SBill Paul 
52296f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
52396f2e892SBill Paul 		DELAY(1);
52496f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
52596f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
526ee320f98SPyun YongHyeon 			*dest = (uint16_t)(r & 0xFFFF);
52796f2e892SBill Paul 			return;
52896f2e892SBill Paul 		}
52996f2e892SBill Paul 	}
53096f2e892SBill Paul }
53196f2e892SBill Paul 
53296f2e892SBill Paul /*
53396f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
534feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
535feb78939SJonathan Chen  * the EEPROM, too.
536feb78939SJonathan Chen  */
537e3d2833aSAlfred Perlstein static void
538ee320f98SPyun YongHyeon dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, uint16_t *dest)
539feb78939SJonathan Chen {
5400934f18aSMaxime Henrion 
541feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
542feb78939SJonathan Chen 
543feb78939SJonathan Chen 	addr *= 2;
544feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
545ee320f98SPyun YongHyeon 	*dest = (uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
546feb78939SJonathan Chen 	addr += 1;
547feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
548ee320f98SPyun YongHyeon 	*dest |= ((uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
549feb78939SJonathan Chen 
550feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
551feb78939SJonathan Chen }
552feb78939SJonathan Chen 
553feb78939SJonathan Chen /*
554feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
55596f2e892SBill Paul  */
556e3d2833aSAlfred Perlstein static void
557ee320f98SPyun YongHyeon dc_eeprom_getword(struct dc_softc *sc, int addr, uint16_t *dest)
55896f2e892SBill Paul {
5590934f18aSMaxime Henrion 	int i;
560ee320f98SPyun YongHyeon 	uint16_t word = 0;
56196f2e892SBill Paul 
56296f2e892SBill Paul 	/* Force EEPROM to idle state. */
56396f2e892SBill Paul 	dc_eeprom_idle(sc);
56496f2e892SBill Paul 
56596f2e892SBill Paul 	/* Enter EEPROM access mode. */
56696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
56796f2e892SBill Paul 	dc_delay(sc);
56896f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
56996f2e892SBill Paul 	dc_delay(sc);
57096f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
57196f2e892SBill Paul 	dc_delay(sc);
57296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
57396f2e892SBill Paul 	dc_delay(sc);
57496f2e892SBill Paul 
57596f2e892SBill Paul 	/*
57696f2e892SBill Paul 	 * Send address of word we want to read.
57796f2e892SBill Paul 	 */
57896f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
57996f2e892SBill Paul 
58096f2e892SBill Paul 	/*
58196f2e892SBill Paul 	 * Start reading bits from EEPROM.
58296f2e892SBill Paul 	 */
58396f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
58496f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
58596f2e892SBill Paul 		dc_delay(sc);
58696f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
58796f2e892SBill Paul 			word |= i;
58896f2e892SBill Paul 		dc_delay(sc);
58996f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
59096f2e892SBill Paul 		dc_delay(sc);
59196f2e892SBill Paul 	}
59296f2e892SBill Paul 
59396f2e892SBill Paul 	/* Turn off EEPROM access mode. */
59496f2e892SBill Paul 	dc_eeprom_idle(sc);
59596f2e892SBill Paul 
59696f2e892SBill Paul 	*dest = word;
59796f2e892SBill Paul }
59896f2e892SBill Paul 
59996f2e892SBill Paul /*
60096f2e892SBill Paul  * Read a sequence of words from the EEPROM.
60196f2e892SBill Paul  */
602e3d2833aSAlfred Perlstein static void
6038c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
60496f2e892SBill Paul {
60596f2e892SBill Paul 	int i;
606ee320f98SPyun YongHyeon 	uint16_t word = 0, *ptr;
60796f2e892SBill Paul 
60896f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
60996f2e892SBill Paul 		if (DC_IS_PNIC(sc))
61096f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
611feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
612feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
61396f2e892SBill Paul 		else
61496f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
615ee320f98SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
6168c7ff1f3SMaxime Henrion 		if (be)
6178c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
61896f2e892SBill Paul 		else
6198c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
62096f2e892SBill Paul 	}
62196f2e892SBill Paul }
62296f2e892SBill Paul 
62396f2e892SBill Paul /*
6248c1093fcSMarius Strobl  * Write the MII serial port for the MII bit-bang module.
62596f2e892SBill Paul  */
626e3d2833aSAlfred Perlstein static void
6278c1093fcSMarius Strobl dc_mii_bitbang_write(device_t dev, uint32_t val)
62896f2e892SBill Paul {
6298c1093fcSMarius Strobl 	struct dc_softc *sc;
6300934f18aSMaxime Henrion 
6318c1093fcSMarius Strobl 	sc = device_get_softc(dev);
63296f2e892SBill Paul 
6338c1093fcSMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, val);
63415578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
63515578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
63696f2e892SBill Paul }
63796f2e892SBill Paul 
63896f2e892SBill Paul /*
6398c1093fcSMarius Strobl  * Read the MII serial port for the MII bit-bang module.
64096f2e892SBill Paul  */
6418c1093fcSMarius Strobl static uint32_t
6428c1093fcSMarius Strobl dc_mii_bitbang_read(device_t dev)
64396f2e892SBill Paul {
6448c1093fcSMarius Strobl 	struct dc_softc *sc;
6458c1093fcSMarius Strobl 	uint32_t val;
6460934f18aSMaxime Henrion 
6478c1093fcSMarius Strobl 	sc = device_get_softc(dev);
6488c1093fcSMarius Strobl 
6498c1093fcSMarius Strobl 	val = CSR_READ_4(sc, DC_SIO);
65015578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
65115578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
65296f2e892SBill Paul 
6538c1093fcSMarius Strobl 	return (val);
65496f2e892SBill Paul }
65596f2e892SBill Paul 
656e3d2833aSAlfred Perlstein static int
6570934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
65896f2e892SBill Paul {
65996f2e892SBill Paul 	struct dc_softc *sc;
660c85c4667SBill Paul 	int i, rval, phy_reg = 0;
66196f2e892SBill Paul 
66296f2e892SBill Paul 	sc = device_get_softc(dev);
66396f2e892SBill Paul 
6645c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
66596f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
66696f2e892SBill Paul 			switch (reg) {
66796f2e892SBill Paul 			case MII_BMSR:
66896f2e892SBill Paul 			/*
66996f2e892SBill Paul 			 * Fake something to make the probe
67096f2e892SBill Paul 			 * code think there's a PHY here.
67196f2e892SBill Paul 			 */
67296f2e892SBill Paul 				return (BMSR_MEDIAMASK);
67396f2e892SBill Paul 				break;
67496f2e892SBill Paul 			case MII_PHYIDR1:
67596f2e892SBill Paul 				if (DC_IS_PNIC(sc))
67696f2e892SBill Paul 					return (DC_VENDORID_LO);
67796f2e892SBill Paul 				return (DC_VENDORID_DEC);
67896f2e892SBill Paul 				break;
67996f2e892SBill Paul 			case MII_PHYIDR2:
68096f2e892SBill Paul 				if (DC_IS_PNIC(sc))
68196f2e892SBill Paul 					return (DC_DEVICEID_82C168);
68296f2e892SBill Paul 				return (DC_DEVICEID_21143);
68396f2e892SBill Paul 				break;
68496f2e892SBill Paul 			default:
68596f2e892SBill Paul 				return (0);
68696f2e892SBill Paul 				break;
68796f2e892SBill Paul 			}
68896f2e892SBill Paul 		} else
68996f2e892SBill Paul 			return (0);
69096f2e892SBill Paul 	}
69196f2e892SBill Paul 
69296f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
69396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
69496f2e892SBill Paul 		    (phy << 23) | (reg << 18));
69596f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
69696f2e892SBill Paul 			DELAY(1);
69796f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
69896f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
69996f2e892SBill Paul 				rval &= 0xFFFF;
70096f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
70196f2e892SBill Paul 			}
70296f2e892SBill Paul 		}
70396f2e892SBill Paul 		return (0);
70496f2e892SBill Paul 	}
70596f2e892SBill Paul 
70652ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
70752ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
70852ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
70952ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
71052ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_READ);
71152ca7ee2SPyun YongHyeon 		for (i = 0; i < DC_TIMEOUT; i++) {
71252ca7ee2SPyun YongHyeon 			DELAY(1);
71352ca7ee2SPyun YongHyeon 			rval = CSR_READ_4(sc, DC_ROM);
71452ca7ee2SPyun YongHyeon 			if ((rval & DC_ULI_PHY_OP_DONE) != 0) {
71552ca7ee2SPyun YongHyeon 				return (rval & DC_ULI_PHY_DATA_MASK);
71652ca7ee2SPyun YongHyeon 			}
71752ca7ee2SPyun YongHyeon 		}
71852ca7ee2SPyun YongHyeon 		if (i == DC_TIMEOUT)
71952ca7ee2SPyun YongHyeon 			device_printf(dev, "phy read timed out\n");
72052ca7ee2SPyun YongHyeon 		return (0);
72152ca7ee2SPyun YongHyeon 	}
72252ca7ee2SPyun YongHyeon 
72396f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
72496f2e892SBill Paul 		switch (reg) {
72596f2e892SBill Paul 		case MII_BMCR:
72696f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
72796f2e892SBill Paul 			break;
72896f2e892SBill Paul 		case MII_BMSR:
72996f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
73096f2e892SBill Paul 			break;
73196f2e892SBill Paul 		case MII_PHYIDR1:
73296f2e892SBill Paul 			phy_reg = DC_AL_VENID;
73396f2e892SBill Paul 			break;
73496f2e892SBill Paul 		case MII_PHYIDR2:
73596f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
73696f2e892SBill Paul 			break;
73796f2e892SBill Paul 		case MII_ANAR:
73896f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
73996f2e892SBill Paul 			break;
74096f2e892SBill Paul 		case MII_ANLPAR:
74196f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
74296f2e892SBill Paul 			break;
74396f2e892SBill Paul 		case MII_ANER:
74496f2e892SBill Paul 			phy_reg = DC_AL_ANER;
74596f2e892SBill Paul 			break;
74696f2e892SBill Paul 		default:
74722f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
74822f6205dSJohn Baldwin 			    reg);
74996f2e892SBill Paul 			return (0);
75096f2e892SBill Paul 			break;
75196f2e892SBill Paul 		}
75296f2e892SBill Paul 
75396f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
75496f2e892SBill Paul 		if (rval == 0xFFFF)
75596f2e892SBill Paul 			return (0);
75696f2e892SBill Paul 		return (rval);
75796f2e892SBill Paul 	}
75896f2e892SBill Paul 
759419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
760f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
761f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
762419146d9SBill Paul 	}
7638c1093fcSMarius Strobl 	rval = mii_bitbang_readreg(dev, &dc_mii_bitbang_ops, phy, reg);
764419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
765f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
76696f2e892SBill Paul 
7678c1093fcSMarius Strobl 	return (rval);
76896f2e892SBill Paul }
76996f2e892SBill Paul 
770e3d2833aSAlfred Perlstein static int
7710934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
77296f2e892SBill Paul {
77396f2e892SBill Paul 	struct dc_softc *sc;
774c85c4667SBill Paul 	int i, phy_reg = 0;
77596f2e892SBill Paul 
77696f2e892SBill Paul 	sc = device_get_softc(dev);
77796f2e892SBill Paul 
77896f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
77996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
78096f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
78196f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
78296f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
78396f2e892SBill Paul 				break;
78496f2e892SBill Paul 		}
78596f2e892SBill Paul 		return (0);
78696f2e892SBill Paul 	}
78796f2e892SBill Paul 
78852ca7ee2SPyun YongHyeon 	if (sc->dc_type == DC_TYPE_ULI_M5263) {
78952ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_ROM,
79052ca7ee2SPyun YongHyeon 		    ((phy << DC_ULI_PHY_ADDR_SHIFT) & DC_ULI_PHY_ADDR_MASK) |
79152ca7ee2SPyun YongHyeon 		    ((reg << DC_ULI_PHY_REG_SHIFT) & DC_ULI_PHY_REG_MASK) |
79252ca7ee2SPyun YongHyeon 		    ((data << DC_ULI_PHY_DATA_SHIFT) & DC_ULI_PHY_DATA_MASK) |
79352ca7ee2SPyun YongHyeon 		    DC_ULI_PHY_OP_WRITE);
79452ca7ee2SPyun YongHyeon 		DELAY(1);
79552ca7ee2SPyun YongHyeon 		return (0);
79652ca7ee2SPyun YongHyeon 	}
79752ca7ee2SPyun YongHyeon 
79896f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
79996f2e892SBill Paul 		switch (reg) {
80096f2e892SBill Paul 		case MII_BMCR:
80196f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
80296f2e892SBill Paul 			break;
80396f2e892SBill Paul 		case MII_BMSR:
80496f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
80596f2e892SBill Paul 			break;
80696f2e892SBill Paul 		case MII_PHYIDR1:
80796f2e892SBill Paul 			phy_reg = DC_AL_VENID;
80896f2e892SBill Paul 			break;
80996f2e892SBill Paul 		case MII_PHYIDR2:
81096f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
81196f2e892SBill Paul 			break;
81296f2e892SBill Paul 		case MII_ANAR:
81396f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
81496f2e892SBill Paul 			break;
81596f2e892SBill Paul 		case MII_ANLPAR:
81696f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
81796f2e892SBill Paul 			break;
81896f2e892SBill Paul 		case MII_ANER:
81996f2e892SBill Paul 			phy_reg = DC_AL_ANER;
82096f2e892SBill Paul 			break;
82196f2e892SBill Paul 		default:
82222f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
82322f6205dSJohn Baldwin 			    reg);
82496f2e892SBill Paul 			return (0);
82596f2e892SBill Paul 			break;
82696f2e892SBill Paul 		}
82796f2e892SBill Paul 
82896f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
82996f2e892SBill Paul 		return (0);
83096f2e892SBill Paul 	}
83196f2e892SBill Paul 
832419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
833f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
834f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
835419146d9SBill Paul 	}
8368c1093fcSMarius Strobl 	mii_bitbang_writereg(dev, &dc_mii_bitbang_ops, phy, reg, data);
837419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
838f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
83996f2e892SBill Paul 
84096f2e892SBill Paul 	return (0);
84196f2e892SBill Paul }
84296f2e892SBill Paul 
843e3d2833aSAlfred Perlstein static void
8440934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
84596f2e892SBill Paul {
84696f2e892SBill Paul 	struct dc_softc *sc;
847d314ebf5SPyun YongHyeon 	struct ifnet *ifp;
84896f2e892SBill Paul 	struct mii_data *mii;
849f43d9309SBill Paul 	struct ifmedia *ifm;
85096f2e892SBill Paul 
85196f2e892SBill Paul 	sc = device_get_softc(dev);
8525c1cfac4SBill Paul 
85396f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
854d314ebf5SPyun YongHyeon 	ifp = sc->dc_ifp;
855d314ebf5SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
856d314ebf5SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
857d314ebf5SPyun YongHyeon 		return;
858d314ebf5SPyun YongHyeon 
859f43d9309SBill Paul 	ifm = &mii->mii_media;
86089b2411bSPyun YongHyeon 	if (DC_IS_DAVICOM(sc) && IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
861f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
862d314ebf5SPyun YongHyeon 		return;
86389b2411bSPyun YongHyeon 	} else if (!DC_IS_ADMTEK(sc))
86489b2411bSPyun YongHyeon 		dc_setcfg(sc, mii->mii_media_active);
865d314ebf5SPyun YongHyeon 
866d314ebf5SPyun YongHyeon 	sc->dc_link = 0;
867d314ebf5SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
868d314ebf5SPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
869d314ebf5SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
870d314ebf5SPyun YongHyeon 		case IFM_10_T:
871d314ebf5SPyun YongHyeon 		case IFM_100_TX:
872d314ebf5SPyun YongHyeon 			sc->dc_link = 1;
873d314ebf5SPyun YongHyeon 			break;
874d314ebf5SPyun YongHyeon 		}
875d314ebf5SPyun YongHyeon 	}
876f43d9309SBill Paul }
877f43d9309SBill Paul 
878f43d9309SBill Paul /*
879f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
880f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
881f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
882f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
883f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
884f43d9309SBill Paul  * with it itself. *sigh*
885f43d9309SBill Paul  */
886e3d2833aSAlfred Perlstein static void
8870934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
888f43d9309SBill Paul {
889f43d9309SBill Paul 	struct dc_softc *sc;
890f43d9309SBill Paul 	struct mii_data *mii;
891f43d9309SBill Paul 	struct ifmedia *ifm;
892f43d9309SBill Paul 	int rev;
893f43d9309SBill Paul 
8941e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
895f43d9309SBill Paul 
896f43d9309SBill Paul 	sc = device_get_softc(dev);
897f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
898f43d9309SBill Paul 	ifm = &mii->mii_media;
899f43d9309SBill Paul 
900f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
90145521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
90296f2e892SBill Paul }
90396f2e892SBill Paul 
90479d11e09SBill Paul #define	DC_BITS_512	9
90579d11e09SBill Paul #define	DC_BITS_128	7
90679d11e09SBill Paul #define	DC_BITS_64	6
90796f2e892SBill Paul 
9083373489bSWarner Losh static uint32_t
9093373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
91096f2e892SBill Paul {
9113373489bSWarner Losh 	uint32_t crc;
91296f2e892SBill Paul 
91396f2e892SBill Paul 	/* Compute CRC for the address value. */
9140e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
91596f2e892SBill Paul 
91679d11e09SBill Paul 	/*
91779d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
91879d11e09SBill Paul 	 * chips is only 128 bits wide.
91979d11e09SBill Paul 	 */
92079d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
92179d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
92296f2e892SBill Paul 
92379d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
92479d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
92579d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
92679d11e09SBill Paul 
927feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
928feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
929feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
930feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
9310934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
932feb78939SJonathan Chen 		else
9330934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
9340934f18aSMaxime Henrion 			    (12 << 4));
935feb78939SJonathan Chen 	}
936feb78939SJonathan Chen 
93779d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
93896f2e892SBill Paul }
93996f2e892SBill Paul 
94096f2e892SBill Paul /*
94196f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
94296f2e892SBill Paul  */
9433373489bSWarner Losh static uint32_t
9443373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
94596f2e892SBill Paul {
9460e939c0cSChristian Weisgerber 	uint32_t crc;
94796f2e892SBill Paul 
94896f2e892SBill Paul 	/* Compute CRC for the address value. */
9490e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
95096f2e892SBill Paul 
9510934f18aSMaxime Henrion 	/* Return the filter bit position. */
95296f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
95396f2e892SBill Paul }
95496f2e892SBill Paul 
95596f2e892SBill Paul /*
95696f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
95796f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
95896f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
95996f2e892SBill Paul  *
96096f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
96196f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
96296f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
96396f2e892SBill Paul  * we need that too.
96496f2e892SBill Paul  */
9652c876e15SPoul-Henning Kamp static void
9660934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
96796f2e892SBill Paul {
9688df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
96996f2e892SBill Paul 	struct dc_desc *sframe;
970ee320f98SPyun YongHyeon 	uint32_t h, *sp;
97196f2e892SBill Paul 	struct ifmultiaddr *ifma;
97296f2e892SBill Paul 	struct ifnet *ifp;
97396f2e892SBill Paul 	int i;
97496f2e892SBill Paul 
975fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
97696f2e892SBill Paul 
97796f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
97896f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
97996f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
9805f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
98156e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
9820934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
98396f2e892SBill Paul 
9845f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
985af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
986af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
98796f2e892SBill Paul 
98856e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
98996f2e892SBill Paul 
99096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
99196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
99296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99396f2e892SBill Paul 	else
99496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99596f2e892SBill Paul 
99696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
99796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
99896f2e892SBill Paul 	else
99996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100096f2e892SBill Paul 
1001eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
10026817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
100396f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100496f2e892SBill Paul 			continue;
1005aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
100696f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1007af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
100896f2e892SBill Paul 	}
1009eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
101096f2e892SBill Paul 
101196f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1012aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1013af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
101496f2e892SBill Paul 	}
101596f2e892SBill Paul 
10168df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
10178df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10188df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
10198df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
10208df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
102196f2e892SBill Paul 
1022af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
10238c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
10248c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
10255f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
102696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
102796f2e892SBill Paul 
102896f2e892SBill Paul 	/*
102996f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
103096f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
103196f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
103296f2e892SBill Paul 	 * medicine.
103396f2e892SBill Paul 	 */
103496f2e892SBill Paul 	DELAY(10000);
103596f2e892SBill Paul 
1036b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
103796f2e892SBill Paul }
103896f2e892SBill Paul 
10392c876e15SPoul-Henning Kamp static void
10400934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
104196f2e892SBill Paul {
10422e3d4b79SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
104396f2e892SBill Paul 	struct ifnet *ifp;
10440934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
104596f2e892SBill Paul 	int h = 0;
1046ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
104796f2e892SBill Paul 
1048fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
104996f2e892SBill Paul 
10500934f18aSMaxime Henrion 	/* Init our MAC address. */
10518df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
10522e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
10532e3d4b79SPyun YongHyeon 	    eaddr[1] << 8 | eaddr[0]);
10542e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
105596f2e892SBill Paul 
105696f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
105796f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
105896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
105996f2e892SBill Paul 	else
106096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
106196f2e892SBill Paul 
106296f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
106396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106496f2e892SBill Paul 	else
106596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106696f2e892SBill Paul 
10670934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
106896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
106996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
107096f2e892SBill Paul 
107196f2e892SBill Paul 	/*
107296f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
107396f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
107496f2e892SBill Paul 	 */
107596f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
107696f2e892SBill Paul 		return;
107796f2e892SBill Paul 
10780934f18aSMaxime Henrion 	/* Now program new ones. */
1079eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
10806817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
108196f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
108296f2e892SBill Paul 			continue;
1083acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1084aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1085aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1086acc1bcccSMartin Blapp 		else
1087aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1088aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
108996f2e892SBill Paul 		if (h < 32)
109096f2e892SBill Paul 			hashes[0] |= (1 << h);
109196f2e892SBill Paul 		else
109296f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
109396f2e892SBill Paul 	}
1094eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
109596f2e892SBill Paul 
109696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
109796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
109896f2e892SBill Paul }
109996f2e892SBill Paul 
11002c876e15SPoul-Henning Kamp static void
11010934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
110296f2e892SBill Paul {
11038df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
110496f2e892SBill Paul 	struct ifnet *ifp;
11050934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
110696f2e892SBill Paul 	int h = 0;
1107ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
110896f2e892SBill Paul 
1109fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
111096f2e892SBill Paul 
11118df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
11128df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
111396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
11148df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
111596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
11168df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
111796f2e892SBill Paul 
111896f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
111996f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
112096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112196f2e892SBill Paul 	else
112296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112396f2e892SBill Paul 
112496f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
112596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112696f2e892SBill Paul 	else
112796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112896f2e892SBill Paul 
112996f2e892SBill Paul 	/*
113096f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
113196f2e892SBill Paul 	 * of broadcast frames.
113296f2e892SBill Paul 	 */
113396f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
113496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113596f2e892SBill Paul 	else
113696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113796f2e892SBill Paul 
113896f2e892SBill Paul 	/* first, zot all the existing hash bits */
113996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
114096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
114296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114396f2e892SBill Paul 
114496f2e892SBill Paul 	/*
114596f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
114696f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
114796f2e892SBill Paul 	 */
114896f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
114996f2e892SBill Paul 		return;
115096f2e892SBill Paul 
115196f2e892SBill Paul 	/* now program new ones */
1152eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
11536817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
115496f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
115596f2e892SBill Paul 			continue;
1156aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
115796f2e892SBill Paul 		if (h < 32)
115896f2e892SBill Paul 			hashes[0] |= (1 << h);
115996f2e892SBill Paul 		else
116096f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
116196f2e892SBill Paul 	}
1162eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
116396f2e892SBill Paul 
116496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
116596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
116696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
116796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
116896f2e892SBill Paul }
116996f2e892SBill Paul 
11702c876e15SPoul-Henning Kamp static void
117152ca7ee2SPyun YongHyeon dc_setfilt_uli(struct dc_softc *sc)
117252ca7ee2SPyun YongHyeon {
117352ca7ee2SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
117452ca7ee2SPyun YongHyeon 	struct ifnet *ifp;
117552ca7ee2SPyun YongHyeon 	struct ifmultiaddr *ifma;
117652ca7ee2SPyun YongHyeon 	struct dc_desc *sframe;
117752ca7ee2SPyun YongHyeon 	uint32_t filter, *sp;
117852ca7ee2SPyun YongHyeon 	uint8_t *ma;
117952ca7ee2SPyun YongHyeon 	int i, mcnt;
118052ca7ee2SPyun YongHyeon 
118152ca7ee2SPyun YongHyeon 	ifp = sc->dc_ifp;
118252ca7ee2SPyun YongHyeon 
118352ca7ee2SPyun YongHyeon 	i = sc->dc_cdata.dc_tx_prod;
118452ca7ee2SPyun YongHyeon 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
118552ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_cnt++;
118652ca7ee2SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
118752ca7ee2SPyun YongHyeon 	sp = sc->dc_cdata.dc_sbuf;
118852ca7ee2SPyun YongHyeon 	bzero(sp, DC_SFRAME_LEN);
118952ca7ee2SPyun YongHyeon 
119052ca7ee2SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
119152ca7ee2SPyun YongHyeon 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
119252ca7ee2SPyun YongHyeon 	    DC_TXCTL_TLINK | DC_FILTER_PERFECT | DC_TXCTL_FINT);
119352ca7ee2SPyun YongHyeon 
119452ca7ee2SPyun YongHyeon 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
119552ca7ee2SPyun YongHyeon 
119652ca7ee2SPyun YongHyeon 	/* Set station address. */
119752ca7ee2SPyun YongHyeon 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
119852ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[1] << 8 | eaddr[0]);
119952ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[3] << 8 | eaddr[2]);
120052ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(eaddr[5] << 8 | eaddr[4]);
120152ca7ee2SPyun YongHyeon 
120252ca7ee2SPyun YongHyeon 	/* Set broadcast address. */
120352ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120452ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120552ca7ee2SPyun YongHyeon 	*sp++ = DC_SP_MAC(0xFFFF);
120652ca7ee2SPyun YongHyeon 
120752ca7ee2SPyun YongHyeon 	/* Extract current filter configuration. */
120852ca7ee2SPyun YongHyeon 	filter = CSR_READ_4(sc, DC_NETCFG);
120952ca7ee2SPyun YongHyeon 	filter &= ~(DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI);
121052ca7ee2SPyun YongHyeon 
121152ca7ee2SPyun YongHyeon 	/* Now build perfect filters. */
121252ca7ee2SPyun YongHyeon 	mcnt = 0;
121352ca7ee2SPyun YongHyeon 	if_maddr_rlock(ifp);
121452ca7ee2SPyun YongHyeon 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
121552ca7ee2SPyun YongHyeon 		if (ifma->ifma_addr->sa_family != AF_LINK)
121652ca7ee2SPyun YongHyeon 			continue;
121752ca7ee2SPyun YongHyeon 		if (mcnt >= DC_ULI_FILTER_NPERF) {
121852ca7ee2SPyun YongHyeon 			filter |= DC_NETCFG_RX_ALLMULTI;
121952ca7ee2SPyun YongHyeon 			break;
122052ca7ee2SPyun YongHyeon 		}
122152ca7ee2SPyun YongHyeon 		ma = LLADDR((struct sockaddr_dl *)ifma->ifma_addr);
122252ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[1] << 8 | ma[0]);
122352ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[3] << 8 | ma[2]);
122452ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(ma[5] << 8 | ma[4]);
122552ca7ee2SPyun YongHyeon 		mcnt++;
122652ca7ee2SPyun YongHyeon 	}
122752ca7ee2SPyun YongHyeon 	if_maddr_runlock(ifp);
122852ca7ee2SPyun YongHyeon 
122952ca7ee2SPyun YongHyeon 	for (; mcnt < DC_ULI_FILTER_NPERF; mcnt++) {
123052ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123152ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123252ca7ee2SPyun YongHyeon 		*sp++ = DC_SP_MAC(0xFFFF);
123352ca7ee2SPyun YongHyeon 	}
123452ca7ee2SPyun YongHyeon 
123552ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
123652ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
123752ca7ee2SPyun YongHyeon 		    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
123852ca7ee2SPyun YongHyeon 	if (ifp->if_flags & IFF_PROMISC)
123952ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_PROMISC | DC_NETCFG_RX_ALLMULTI;
124052ca7ee2SPyun YongHyeon 	if (ifp->if_flags & IFF_ALLMULTI)
124152ca7ee2SPyun YongHyeon 		filter |= DC_NETCFG_RX_ALLMULTI;
124252ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_NETCFG,
124352ca7ee2SPyun YongHyeon 	    filter & ~(DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
124452ca7ee2SPyun YongHyeon 	if (filter & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON))
124552ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, filter);
124652ca7ee2SPyun YongHyeon 
124752ca7ee2SPyun YongHyeon 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
124852ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
124952ca7ee2SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
125052ca7ee2SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
125152ca7ee2SPyun YongHyeon 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
125252ca7ee2SPyun YongHyeon 
125352ca7ee2SPyun YongHyeon 	/*
125452ca7ee2SPyun YongHyeon 	 * Wait some time...
125552ca7ee2SPyun YongHyeon 	 */
125652ca7ee2SPyun YongHyeon 	DELAY(1000);
125752ca7ee2SPyun YongHyeon 
125852ca7ee2SPyun YongHyeon 	sc->dc_wdog_timer = 5;
125952ca7ee2SPyun YongHyeon }
126052ca7ee2SPyun YongHyeon 
126152ca7ee2SPyun YongHyeon static void
12620934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1263feb78939SJonathan Chen {
12648df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
12650934f18aSMaxime Henrion 	struct ifnet *ifp;
12660934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1267feb78939SJonathan Chen 	struct dc_desc *sframe;
1268ee320f98SPyun YongHyeon 	uint32_t h, *sp;
1269feb78939SJonathan Chen 	int i;
1270feb78939SJonathan Chen 
1271fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1272feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1273feb78939SJonathan Chen 
1274feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1275feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1276feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
12775f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
127856e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
12790934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1280feb78939SJonathan Chen 
12815f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1282af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1283af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1284feb78939SJonathan Chen 
128556e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1286feb78939SJonathan Chen 
1287feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1288feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1289feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1290feb78939SJonathan Chen 	else
1291feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1292feb78939SJonathan Chen 
1293feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1294feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1295feb78939SJonathan Chen 	else
1296feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1297feb78939SJonathan Chen 
1298eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
12996817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1300feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1301feb78939SJonathan Chen 			continue;
1302aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13031d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1304af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1305feb78939SJonathan Chen 	}
1306eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
1307feb78939SJonathan Chen 
1308feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1309aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1310af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1311feb78939SJonathan Chen 	}
1312feb78939SJonathan Chen 
13138df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
13148df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13158df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13168df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13178df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1318feb78939SJonathan Chen 
1319feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1320feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1321af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
13228c094eccSPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_PREREAD |
13238c094eccSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
13245f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1325feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1326feb78939SJonathan Chen 
1327feb78939SJonathan Chen 	/*
13280934f18aSMaxime Henrion 	 * Wait some time...
1329feb78939SJonathan Chen 	 */
1330feb78939SJonathan Chen 	DELAY(1000);
1331feb78939SJonathan Chen 
1332b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
1333feb78939SJonathan Chen }
1334feb78939SJonathan Chen 
1335e3d2833aSAlfred Perlstein static void
13360934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
133796f2e892SBill Paul {
13380934f18aSMaxime Henrion 
133996f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13401af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
134196f2e892SBill Paul 		dc_setfilt_21143(sc);
134296f2e892SBill Paul 
134396f2e892SBill Paul 	if (DC_IS_ASIX(sc))
134496f2e892SBill Paul 		dc_setfilt_asix(sc);
134596f2e892SBill Paul 
134696f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
134796f2e892SBill Paul 		dc_setfilt_admtek(sc);
134896f2e892SBill Paul 
134952ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
135052ca7ee2SPyun YongHyeon 		dc_setfilt_uli(sc);
135152ca7ee2SPyun YongHyeon 
1352feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1353feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
135496f2e892SBill Paul }
135596f2e892SBill Paul 
1356e3d2833aSAlfred Perlstein static void
13571da7683aSPyun YongHyeon dc_netcfg_wait(struct dc_softc *sc)
135896f2e892SBill Paul {
1359ee320f98SPyun YongHyeon 	uint32_t isr;
13601da7683aSPyun YongHyeon 	int i;
136196f2e892SBill Paul 
136296f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
136396f2e892SBill Paul 		isr = CSR_READ_4(sc, DC_ISR);
1364d467c136SBill Paul 		if (isr & DC_ISR_TX_IDLE &&
1365351267c1SMartin Blapp 		    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1366351267c1SMartin Blapp 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
136796f2e892SBill Paul 			break;
1368d467c136SBill Paul 		DELAY(10);
136996f2e892SBill Paul 	}
13707a6fab66SWarner Losh 	if (i == DC_TIMEOUT && bus_child_present(sc->dc_dev)) {
1371432120f2SMarius Strobl 		if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13726b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
13731da7683aSPyun YongHyeon 			    "%s: failed to force tx to idle state\n", __func__);
1374432120f2SMarius Strobl 		if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1375432120f2SMarius Strobl 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1376d0d67284SMarius Strobl 		    !DC_HAS_BROKEN_RXSTATE(sc))
1377432120f2SMarius Strobl 			device_printf(sc->dc_dev,
13781da7683aSPyun YongHyeon 			    "%s: failed to force rx to idle state\n", __func__);
1379432120f2SMarius Strobl 	}
138096f2e892SBill Paul }
138196f2e892SBill Paul 
13821da7683aSPyun YongHyeon /*
13831da7683aSPyun YongHyeon  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13841da7683aSPyun YongHyeon  * the netconfig register, we first have to put the transmit and/or
13851da7683aSPyun YongHyeon  * receive logic in the idle state.
13861da7683aSPyun YongHyeon  */
13871da7683aSPyun YongHyeon static void
13881da7683aSPyun YongHyeon dc_setcfg(struct dc_softc *sc, int media)
13891da7683aSPyun YongHyeon {
13901da7683aSPyun YongHyeon 	int restart = 0, watchdogreg;
13911da7683aSPyun YongHyeon 
13921da7683aSPyun YongHyeon 	if (IFM_SUBTYPE(media) == IFM_NONE)
13931da7683aSPyun YongHyeon 		return;
13941da7683aSPyun YongHyeon 
13951da7683aSPyun YongHyeon 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
13961da7683aSPyun YongHyeon 		restart = 1;
13971da7683aSPyun YongHyeon 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
13981da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
13991da7683aSPyun YongHyeon 	}
14001da7683aSPyun YongHyeon 
140196f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1402042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1403042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
140496f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1405bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14060934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14078273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14088273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14098273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14104c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1411bf645417SBill Paul 			} else {
1412bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1413bf645417SBill Paul 			}
141496f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141596f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
141696f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
141796f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141896f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
141988d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
142096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
142196f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
142296f2e892SBill Paul 		} else {
142396f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
142496f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
142596f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
142696f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
142796f2e892SBill Paul 			}
1428318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1429318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1430318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
143196f2e892SBill Paul 		}
143296f2e892SBill Paul 	}
143396f2e892SBill Paul 
143496f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1435042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1436042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
143796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14380934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14394c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14408273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14418273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14428273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14438273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14444c2efe27SBill Paul 			} else {
14454c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14464c2efe27SBill Paul 			}
144796f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
144896f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
144996f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
145096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
145188d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
145296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
145396f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
145496f2e892SBill Paul 		} else {
145596f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
145696f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
145796f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
145896f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
145996f2e892SBill Paul 			}
146096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1461318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146296f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14635c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14645c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14655c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14665c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14675c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14685c1cfac4SBill Paul 				else
14695c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14705c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14715c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14725c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14735c1cfac4SBill Paul 				DELAY(20000);
14745c1cfac4SBill Paul 			}
147596f2e892SBill Paul 		}
147696f2e892SBill Paul 	}
147796f2e892SBill Paul 
1478f43d9309SBill Paul 	/*
1479f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1480f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1481f43d9309SBill Paul 	 * on the external MII port.
1482f43d9309SBill Paul 	 */
1483f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
148445521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1485f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1486f43d9309SBill Paul 			sc->dc_link = 1;
1487f43d9309SBill Paul 		} else {
1488f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1489f43d9309SBill Paul 		}
1490f43d9309SBill Paul 	}
1491f43d9309SBill Paul 
149296f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
149396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149496f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149596f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149696f2e892SBill Paul 	} else {
149796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149896f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149996f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
150096f2e892SBill Paul 	}
150196f2e892SBill Paul 
150296f2e892SBill Paul 	if (restart)
150396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
150496f2e892SBill Paul }
150596f2e892SBill Paul 
1506e3d2833aSAlfred Perlstein static void
15070934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
150896f2e892SBill Paul {
15090934f18aSMaxime Henrion 	int i;
151096f2e892SBill Paul 
151196f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
151296f2e892SBill Paul 
151396f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
151496f2e892SBill Paul 		DELAY(10);
151596f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
151696f2e892SBill Paul 			break;
151796f2e892SBill Paul 	}
151896f2e892SBill Paul 
15191af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
152052ca7ee2SPyun YongHyeon 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc) || DC_IS_ULI(sc)) {
152196f2e892SBill Paul 		DELAY(10000);
152296f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152396f2e892SBill Paul 		i = 0;
152496f2e892SBill Paul 	}
152596f2e892SBill Paul 
152696f2e892SBill Paul 	if (i == DC_TIMEOUT)
15276b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev, "reset never completed!\n");
152896f2e892SBill Paul 
152996f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
153096f2e892SBill Paul 	DELAY(1000);
153196f2e892SBill Paul 
153296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
153396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
153496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
153596f2e892SBill Paul 
153691cc2adbSBill Paul 	/*
153791cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
153891cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
153991cc2adbSBill Paul 	 * into a state where it will never come out of reset
154091cc2adbSBill Paul 	 * until we reset the whole chip again.
154191cc2adbSBill Paul 	 */
15425c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
154391cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1544d314ebf5SPyun YongHyeon 		CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFFFFFF);
15455c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15465c1cfac4SBill Paul 	}
154796f2e892SBill Paul }
154896f2e892SBill Paul 
1549ebc284ccSMarius Strobl static const struct dc_type *
15500934f18aSMaxime Henrion dc_devtype(device_t dev)
155196f2e892SBill Paul {
1552ebc284ccSMarius Strobl 	const struct dc_type *t;
1553ee320f98SPyun YongHyeon 	uint32_t devid;
1554ee320f98SPyun YongHyeon 	uint8_t rev;
155596f2e892SBill Paul 
155696f2e892SBill Paul 	t = dc_devs;
15571e2e70b1SJohn Baldwin 	devid = pci_get_devid(dev);
15581e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
155996f2e892SBill Paul 
156096f2e892SBill Paul 	while (t->dc_name != NULL) {
15611e2e70b1SJohn Baldwin 		if (devid == t->dc_devid && rev >= t->dc_minrev)
156296f2e892SBill Paul 			return (t);
156396f2e892SBill Paul 		t++;
156496f2e892SBill Paul 	}
156596f2e892SBill Paul 
156696f2e892SBill Paul 	return (NULL);
156796f2e892SBill Paul }
156896f2e892SBill Paul 
156996f2e892SBill Paul /*
157096f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
157196f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
157296f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
157396f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
157496f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
157596f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
157696f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
157796f2e892SBill Paul  */
1578e3d2833aSAlfred Perlstein static int
15790934f18aSMaxime Henrion dc_probe(device_t dev)
158096f2e892SBill Paul {
1581ebc284ccSMarius Strobl 	const struct dc_type *t;
158296f2e892SBill Paul 
158396f2e892SBill Paul 	t = dc_devtype(dev);
158496f2e892SBill Paul 
158596f2e892SBill Paul 	if (t != NULL) {
158696f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1587d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
158896f2e892SBill Paul 	}
158996f2e892SBill Paul 
159096f2e892SBill Paul 	return (ENXIO);
159196f2e892SBill Paul }
159296f2e892SBill Paul 
1593e3d2833aSAlfred Perlstein static void
15940934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
15955c1cfac4SBill Paul {
15965c1cfac4SBill Paul 	struct dc_mediainfo *m;
1597ee320f98SPyun YongHyeon 	uint8_t *p;
15985c1cfac4SBill Paul 	int i;
1599ee320f98SPyun YongHyeon 	uint32_t reg;
16005c1cfac4SBill Paul 
16015c1cfac4SBill Paul 	m = sc->dc_mi;
16025c1cfac4SBill Paul 
16035c1cfac4SBill Paul 	while (m != NULL) {
16045c1cfac4SBill Paul 		if (m->dc_media == media)
16055c1cfac4SBill Paul 			break;
16065c1cfac4SBill Paul 		m = m->dc_next;
16075c1cfac4SBill Paul 	}
16085c1cfac4SBill Paul 
16095c1cfac4SBill Paul 	if (m == NULL)
16105c1cfac4SBill Paul 		return;
16115c1cfac4SBill Paul 
16125c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16135c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16145c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16155c1cfac4SBill Paul 	}
16165c1cfac4SBill Paul 
16175c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16185c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16195c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16205c1cfac4SBill Paul 	}
16215c1cfac4SBill Paul }
16225c1cfac4SBill Paul 
1623abe4e865SPyun YongHyeon static int
16240934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16255c1cfac4SBill Paul {
16265c1cfac4SBill Paul 	struct dc_mediainfo *m;
16275c1cfac4SBill Paul 
16280934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1629abe4e865SPyun YongHyeon 	if (m == NULL) {
1630abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1631abe4e865SPyun YongHyeon 		return (ENOMEM);
1632abe4e865SPyun YongHyeon 	}
163387f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
163487f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16355c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
163687f4fa15SMartin Blapp 		break;
163787f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16385c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
163987f4fa15SMartin Blapp 		break;
164087f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16415c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
164287f4fa15SMartin Blapp 		break;
164387f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16445c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
164587f4fa15SMartin Blapp 		break;
164687f4fa15SMartin Blapp 	default:
164787f4fa15SMartin Blapp 		break;
164887f4fa15SMartin Blapp 	}
16495c1cfac4SBill Paul 
165087f4fa15SMartin Blapp 	/*
165187f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
165287f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
165387f4fa15SMartin Blapp 	 * supply Media Specific Data.
165487f4fa15SMartin Blapp 	 */
165587f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16565c1cfac4SBill Paul 		m->dc_gp_len = 2;
165787f4fa15SMartin Blapp 		m->dc_gp_ptr =
1658ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
165987f4fa15SMartin Blapp 	} else {
166087f4fa15SMartin Blapp 		m->dc_gp_len = 2;
166187f4fa15SMartin Blapp 		m->dc_gp_ptr =
1662ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
166387f4fa15SMartin Blapp 	}
16645c1cfac4SBill Paul 
16655c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16665c1cfac4SBill Paul 	sc->dc_mi = m;
16675c1cfac4SBill Paul 
16685c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
1669abe4e865SPyun YongHyeon 	return (0);
16705c1cfac4SBill Paul }
16715c1cfac4SBill Paul 
1672abe4e865SPyun YongHyeon static int
16730934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16745c1cfac4SBill Paul {
16755c1cfac4SBill Paul 	struct dc_mediainfo *m;
16765c1cfac4SBill Paul 
16770934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1678abe4e865SPyun YongHyeon 	if (m == NULL) {
1679abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1680abe4e865SPyun YongHyeon 		return (ENOMEM);
1681abe4e865SPyun YongHyeon 	}
16825c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16835c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16845c1cfac4SBill Paul 
16855c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16865c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
16875c1cfac4SBill Paul 
16885c1cfac4SBill Paul 	m->dc_gp_len = 2;
1689ee320f98SPyun YongHyeon 	m->dc_gp_ptr = (uint8_t *)&l->dc_sym_gpio_ctl;
16905c1cfac4SBill Paul 
16915c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16925c1cfac4SBill Paul 	sc->dc_mi = m;
16935c1cfac4SBill Paul 
16945c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
1695abe4e865SPyun YongHyeon 	return (0);
16965c1cfac4SBill Paul }
16975c1cfac4SBill Paul 
1698abe4e865SPyun YongHyeon static int
16990934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17005c1cfac4SBill Paul {
17015c1cfac4SBill Paul 	struct dc_mediainfo *m;
1702ee320f98SPyun YongHyeon 	uint8_t *p;
17035c1cfac4SBill Paul 
17040934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1705abe4e865SPyun YongHyeon 	if (m == NULL) {
1706abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1707abe4e865SPyun YongHyeon 		return (ENOMEM);
1708abe4e865SPyun YongHyeon 	}
17095c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17105c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17115c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17125c1cfac4SBill Paul 
1713ee320f98SPyun YongHyeon 	p = (uint8_t *)l;
17145c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17155c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17165c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17175c1cfac4SBill Paul 	m->dc_reset_len = *p;
17185c1cfac4SBill Paul 	p++;
17195c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17205c1cfac4SBill Paul 
17215c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17225c1cfac4SBill Paul 	sc->dc_mi = m;
1723abe4e865SPyun YongHyeon 	return (0);
17245c1cfac4SBill Paul }
17255c1cfac4SBill Paul 
1726abe4e865SPyun YongHyeon static int
17270934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17283097aa70SWarner Losh {
17293097aa70SWarner Losh 	int size;
17303097aa70SWarner Losh 
1731abe4e865SPyun YongHyeon 	size = DC_ROM_SIZE(bits);
173252ca7ee2SPyun YongHyeon 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
1733abe4e865SPyun YongHyeon 	if (sc->dc_srom == NULL) {
1734abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate SROM buffer\n");
1735abe4e865SPyun YongHyeon 		return (ENOMEM);
1736abe4e865SPyun YongHyeon 	}
17373097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1738abe4e865SPyun YongHyeon 	return (0);
17393097aa70SWarner Losh }
17403097aa70SWarner Losh 
1741abe4e865SPyun YongHyeon static int
17420934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17435c1cfac4SBill Paul {
17445c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17455c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
1746abe4e865SPyun YongHyeon 	int error, have_mii, i, loff;
17475c1cfac4SBill Paul 	char *ptr;
17485c1cfac4SBill Paul 
1749f956e0b3SMartin Blapp 	have_mii = 0;
17505c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17515c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17525c1cfac4SBill Paul 
17535c1cfac4SBill Paul 	ptr = (char *)lhdr;
17545c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1755f956e0b3SMartin Blapp 	/*
1756f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1757f956e0b3SMartin Blapp 	 */
1758f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1759f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1760f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1761f956e0b3SMartin Blapp 		    have_mii++;
1762f956e0b3SMartin Blapp 
1763f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1764f956e0b3SMartin Blapp 		ptr++;
1765f956e0b3SMartin Blapp 	}
1766f956e0b3SMartin Blapp 
1767f956e0b3SMartin Blapp 	/*
1768f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1769f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1770f956e0b3SMartin Blapp 	 */
1771f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1772f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1773abe4e865SPyun YongHyeon 	error = 0;
17745c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17755c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17765c1cfac4SBill Paul 		switch (hdr->dc_type) {
17775c1cfac4SBill Paul 		case DC_EBLOCK_MII:
1778abe4e865SPyun YongHyeon 			error = dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17795c1cfac4SBill Paul 			break;
17805c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1781f956e0b3SMartin Blapp 			if (! have_mii)
1782abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sia(sc,
1783f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
17845c1cfac4SBill Paul 			break;
17855c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1786f956e0b3SMartin Blapp 			if (! have_mii)
1787abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sym(sc,
1788f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
17895c1cfac4SBill Paul 			break;
17905c1cfac4SBill Paul 		default:
17915c1cfac4SBill Paul 			/* Don't care. Yet. */
17925c1cfac4SBill Paul 			break;
17935c1cfac4SBill Paul 		}
17945c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
17955c1cfac4SBill Paul 		ptr++;
17965c1cfac4SBill Paul 	}
1797abe4e865SPyun YongHyeon 	return (error);
17985c1cfac4SBill Paul }
17995c1cfac4SBill Paul 
180056e5e7aeSMaxime Henrion static void
180156e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
180256e5e7aeSMaxime Henrion {
18035f14ee23SPyun YongHyeon 	bus_addr_t *paddr;
180456e5e7aeSMaxime Henrion 
1805ebc284ccSMarius Strobl 	KASSERT(nseg == 1,
1806ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
180756e5e7aeSMaxime Henrion 	paddr = arg;
180856e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
180956e5e7aeSMaxime Henrion }
181056e5e7aeSMaxime Henrion 
18115f14ee23SPyun YongHyeon static int
18125f14ee23SPyun YongHyeon dc_dma_alloc(struct dc_softc *sc)
18135f14ee23SPyun YongHyeon {
18145f14ee23SPyun YongHyeon 	int error, i;
18155f14ee23SPyun YongHyeon 
18165f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dc_dev), 1, 0,
18175f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
18185f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
18195f14ee23SPyun YongHyeon 	    NULL, NULL, &sc->dc_ptag);
18205f14ee23SPyun YongHyeon 	if (error) {
18215f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18225f14ee23SPyun YongHyeon 		    "failed to allocate parent DMA tag\n");
18235f14ee23SPyun YongHyeon 		goto fail;
18245f14ee23SPyun YongHyeon 	}
18255f14ee23SPyun YongHyeon 
18265f14ee23SPyun YongHyeon 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
18275f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18285f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_RX_LIST_SZ, 1,
18295f14ee23SPyun YongHyeon 	    DC_RX_LIST_SZ, 0, NULL, NULL, &sc->dc_rx_ltag);
18305f14ee23SPyun YongHyeon 	if (error) {
18315f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX list DMA tag\n");
18325f14ee23SPyun YongHyeon 		goto fail;
18335f14ee23SPyun YongHyeon 	}
18345f14ee23SPyun YongHyeon 
18355f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18365f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_TX_LIST_SZ, 1,
18375f14ee23SPyun YongHyeon 	    DC_TX_LIST_SZ, 0, NULL, NULL, &sc->dc_tx_ltag);
18385f14ee23SPyun YongHyeon 	if (error) {
18395f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX list DMA tag\n");
18405f14ee23SPyun YongHyeon 		goto fail;
18415f14ee23SPyun YongHyeon 	}
18425f14ee23SPyun YongHyeon 
18435f14ee23SPyun YongHyeon 	/* RX descriptor list. */
18445f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_rx_ltag,
18455f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_rx_list, BUS_DMA_NOWAIT |
18465f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_rx_lmap);
18475f14ee23SPyun YongHyeon 	if (error) {
18485f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18495f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for RX list\n");
18505f14ee23SPyun YongHyeon 		goto fail;
18515f14ee23SPyun YongHyeon 	}
18525f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_rx_ltag, sc->dc_rx_lmap,
18535f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_rx_list, DC_RX_LIST_SZ, dc_dma_map_addr,
18545f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_rx_list_paddr, BUS_DMA_NOWAIT);
18555f14ee23SPyun YongHyeon 	if (error) {
18565f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18575f14ee23SPyun YongHyeon 		    "failed to load DMA'able memory for RX list\n");
18585f14ee23SPyun YongHyeon 		goto fail;
18595f14ee23SPyun YongHyeon 	}
18605f14ee23SPyun YongHyeon 	/* TX descriptor list. */
18615f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_tx_ltag,
18625f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_tx_list, BUS_DMA_NOWAIT |
18635f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_tx_lmap);
18645f14ee23SPyun YongHyeon 	if (error) {
18655f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18665f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for TX list\n");
18675f14ee23SPyun YongHyeon 		goto fail;
18685f14ee23SPyun YongHyeon 	}
18695f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_tx_ltag, sc->dc_tx_lmap,
18705f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_tx_list, DC_TX_LIST_SZ, dc_dma_map_addr,
18715f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_tx_list_paddr, BUS_DMA_NOWAIT);
18725f14ee23SPyun YongHyeon 	if (error) {
18735f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18745f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for TX list\n");
18755f14ee23SPyun YongHyeon 		goto fail;
18765f14ee23SPyun YongHyeon 	}
18775f14ee23SPyun YongHyeon 
18785f14ee23SPyun YongHyeon 	/*
18795f14ee23SPyun YongHyeon 	 * Allocate a busdma tag and DMA safe memory for the multicast
18805f14ee23SPyun YongHyeon 	 * setup frame.
18815f14ee23SPyun YongHyeon 	 */
18825f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18835f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
18845f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
18855f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_stag);
18865f14ee23SPyun YongHyeon 	if (error) {
18875f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18885f14ee23SPyun YongHyeon 		    "failed to create DMA tag for setup frame\n");
18895f14ee23SPyun YongHyeon 		goto fail;
18905f14ee23SPyun YongHyeon 	}
18915f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
18925f14ee23SPyun YongHyeon 	    BUS_DMA_NOWAIT, &sc->dc_smap);
18935f14ee23SPyun YongHyeon 	if (error) {
18945f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18955f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for setup frame\n");
18965f14ee23SPyun YongHyeon 		goto fail;
18975f14ee23SPyun YongHyeon 	}
18985f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
18995f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
19005f14ee23SPyun YongHyeon 	if (error) {
19015f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19025f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for setup frame\n");
19035f14ee23SPyun YongHyeon 		goto fail;
19045f14ee23SPyun YongHyeon 	}
19055f14ee23SPyun YongHyeon 
19065f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for RX mbufs. */
19075f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_RXBUF_ALIGN, 0,
19085f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19095f14ee23SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->dc_rx_mtag);
19105f14ee23SPyun YongHyeon 	if (error) {
19115f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX mbuf tag\n");
19125f14ee23SPyun YongHyeon 		goto fail;
19135f14ee23SPyun YongHyeon 	}
19145f14ee23SPyun YongHyeon 
19155f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for TX mbufs. */
19165f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, 1, 0,
19175f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19185f14ee23SPyun YongHyeon 	    MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
19195f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_tx_mtag);
19205f14ee23SPyun YongHyeon 	if (error) {
19215f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX mbuf tag\n");
19225f14ee23SPyun YongHyeon 		goto fail;
19235f14ee23SPyun YongHyeon 	}
19245f14ee23SPyun YongHyeon 
19255f14ee23SPyun YongHyeon 	/* Create the TX/RX busdma maps. */
19265f14ee23SPyun YongHyeon 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
19275f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_tx_mtag, 0,
19285f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_tx_map[i]);
19295f14ee23SPyun YongHyeon 		if (error) {
19305f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19315f14ee23SPyun YongHyeon 			    "failed to create TX mbuf dmamap\n");
19325f14ee23SPyun YongHyeon 			goto fail;
19335f14ee23SPyun YongHyeon 		}
19345f14ee23SPyun YongHyeon 	}
19355f14ee23SPyun YongHyeon 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
19365f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_rx_mtag, 0,
19375f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_rx_map[i]);
19385f14ee23SPyun YongHyeon 		if (error) {
19395f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19405f14ee23SPyun YongHyeon 			    "failed to create RX mbuf dmamap\n");
19415f14ee23SPyun YongHyeon 			goto fail;
19425f14ee23SPyun YongHyeon 		}
19435f14ee23SPyun YongHyeon 	}
19445f14ee23SPyun YongHyeon 	error = bus_dmamap_create(sc->dc_rx_mtag, 0, &sc->dc_sparemap);
19455f14ee23SPyun YongHyeon 	if (error) {
19465f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19475f14ee23SPyun YongHyeon 		    "failed to create spare RX mbuf dmamap\n");
19485f14ee23SPyun YongHyeon 		goto fail;
19495f14ee23SPyun YongHyeon 	}
19505f14ee23SPyun YongHyeon 
19515f14ee23SPyun YongHyeon fail:
19525f14ee23SPyun YongHyeon 	return (error);
19535f14ee23SPyun YongHyeon }
19545f14ee23SPyun YongHyeon 
19555f14ee23SPyun YongHyeon static void
19565f14ee23SPyun YongHyeon dc_dma_free(struct dc_softc *sc)
19575f14ee23SPyun YongHyeon {
19585f14ee23SPyun YongHyeon 	int i;
19595f14ee23SPyun YongHyeon 
19605f14ee23SPyun YongHyeon 	/* RX buffers. */
19615f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19625f14ee23SPyun YongHyeon 		for (i = 0; i < DC_RX_LIST_CNT; i++) {
19635f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
19645f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_rx_mtag,
19655f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_rx_map[i]);
19665f14ee23SPyun YongHyeon 		}
19675f14ee23SPyun YongHyeon 		if (sc->dc_sparemap != NULL)
19685f14ee23SPyun YongHyeon 			bus_dmamap_destroy(sc->dc_rx_mtag, sc->dc_sparemap);
19695f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_mtag);
19705f14ee23SPyun YongHyeon 	}
19715f14ee23SPyun YongHyeon 
19725f14ee23SPyun YongHyeon 	/* TX buffers. */
19735f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19745f14ee23SPyun YongHyeon 		for (i = 0; i < DC_TX_LIST_CNT; i++) {
19755f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
19765f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_tx_mtag,
19775f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_tx_map[i]);
19785f14ee23SPyun YongHyeon 		}
19795f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_mtag);
19805f14ee23SPyun YongHyeon 	}
19815f14ee23SPyun YongHyeon 
19825f14ee23SPyun YongHyeon 	/* RX descriptor list. */
19835f14ee23SPyun YongHyeon 	if (sc->dc_rx_ltag) {
19845f14ee23SPyun YongHyeon 		if (sc->dc_rx_lmap != NULL)
19855f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_ltag, sc->dc_rx_lmap);
19865f14ee23SPyun YongHyeon 		if (sc->dc_rx_lmap != NULL && sc->dc_ldata.dc_rx_list != NULL)
19875f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_rx_ltag, sc->dc_ldata.dc_rx_list,
19885f14ee23SPyun YongHyeon 			    sc->dc_rx_lmap);
19895f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_ltag);
19905f14ee23SPyun YongHyeon 	}
19915f14ee23SPyun YongHyeon 
19925f14ee23SPyun YongHyeon 	/* TX descriptor list. */
19935f14ee23SPyun YongHyeon 	if (sc->dc_tx_ltag) {
19945f14ee23SPyun YongHyeon 		if (sc->dc_tx_lmap != NULL)
19955f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_ltag, sc->dc_tx_lmap);
19965f14ee23SPyun YongHyeon 		if (sc->dc_tx_lmap != NULL && sc->dc_ldata.dc_tx_list != NULL)
19975f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_tx_ltag, sc->dc_ldata.dc_tx_list,
19985f14ee23SPyun YongHyeon 			    sc->dc_tx_lmap);
19995f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_ltag);
20005f14ee23SPyun YongHyeon 	}
20015f14ee23SPyun YongHyeon 
20025f14ee23SPyun YongHyeon 	/* multicast setup frame. */
20035f14ee23SPyun YongHyeon 	if (sc->dc_stag) {
20045f14ee23SPyun YongHyeon 		if (sc->dc_smap != NULL)
20055f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_stag, sc->dc_smap);
20065f14ee23SPyun YongHyeon 		if (sc->dc_smap != NULL && sc->dc_cdata.dc_sbuf != NULL)
20075f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf,
20085f14ee23SPyun YongHyeon 			    sc->dc_smap);
20095f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_stag);
20105f14ee23SPyun YongHyeon 	}
20115f14ee23SPyun YongHyeon }
20125f14ee23SPyun YongHyeon 
201396f2e892SBill Paul /*
201496f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
201596f2e892SBill Paul  * setup and ethernet/BPF attach.
201696f2e892SBill Paul  */
2017e3d2833aSAlfred Perlstein static int
20180934f18aSMaxime Henrion dc_attach(device_t dev)
201996f2e892SBill Paul {
20208df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
2021ee320f98SPyun YongHyeon 	uint32_t command;
202296f2e892SBill Paul 	struct dc_softc *sc;
202396f2e892SBill Paul 	struct ifnet *ifp;
2024b289c607SPyun YongHyeon 	struct dc_mediainfo *m;
2025ee320f98SPyun YongHyeon 	uint32_t reg, revision;
202652ca7ee2SPyun YongHyeon 	uint16_t *srom;
202752ca7ee2SPyun YongHyeon 	int error, mac_offset, n, phy, rid, tmp;
2028ee320f98SPyun YongHyeon 	uint8_t *mac;
202996f2e892SBill Paul 
203096f2e892SBill Paul 	sc = device_get_softc(dev);
20316b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
203296f2e892SBill Paul 
20336008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2034c8b27acaSJohn Baldwin 	    MTX_DEF);
2035c3e7434fSWarner Losh 
203696f2e892SBill Paul 	/*
203796f2e892SBill Paul 	 * Map control/status registers.
203896f2e892SBill Paul 	 */
203907f65363SBill Paul 	pci_enable_busmaster(dev);
204096f2e892SBill Paul 
204196f2e892SBill Paul 	rid = DC_RID;
20425f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
204396f2e892SBill Paul 
204496f2e892SBill Paul 	if (sc->dc_res == NULL) {
204522f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
204696f2e892SBill Paul 		error = ENXIO;
2047608654d4SNate Lawson 		goto fail;
204896f2e892SBill Paul 	}
204996f2e892SBill Paul 
205096f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
205196f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
205296f2e892SBill Paul 
20530934f18aSMaxime Henrion 	/* Allocate interrupt. */
205454f1f1d1SNate Lawson 	rid = 0;
20555f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
205654f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
205754f1f1d1SNate Lawson 
205854f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
205922f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
206054f1f1d1SNate Lawson 		error = ENXIO;
206154f1f1d1SNate Lawson 		goto fail;
206254f1f1d1SNate Lawson 	}
206354f1f1d1SNate Lawson 
206496f2e892SBill Paul 	/* Need this info to decide on a chip type. */
206596f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
20661e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
206796f2e892SBill Paul 
2068abe4e865SPyun YongHyeon 	error = 0;
20696d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
20701e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
20711e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
20721e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
20731e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
2074eecb3844SMartin Blapp 		dc_eeprom_width(sc);
2075eecb3844SMartin Blapp 
20761e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
20771e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
207896f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
207996f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2080042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
20815c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
2082abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2083abe4e865SPyun YongHyeon 		if (error != 0)
2084abe4e865SPyun YongHyeon 			goto fail;
208596f2e892SBill Paul 		break;
20861e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
20871e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
20881e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
208996f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
2090318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
2091318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
20927dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
20934a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
20941e2e70b1SJohn Baldwin 
20950a46b1dcSBill Paul 		/* Increase the latency timer value. */
20961e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
209796f2e892SBill Paul 		break;
20981e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
209996f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
210096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
210196f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
210296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2103abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2104abe4e865SPyun YongHyeon 		if (error != 0)
2105abe4e865SPyun YongHyeon 			goto fail;
210696f2e892SBill Paul 		break;
2107593a1aeaSMartin Blapp 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983):
21081e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
21091e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
21101e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
21111e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
21121e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
21131e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
21141e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
21151e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
21161e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
21171e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
21181e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
21191e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
212017762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
212117762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
2122593a1aeaSMartin Blapp 		sc->dc_type = DC_TYPE_AN983;
2123acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
212496f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
212596f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
212696f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2127129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
212896f2e892SBill Paul 		break;
21291e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
21301e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
213196f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
213296f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
213396f2e892SBill Paul 		}
2134318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
213596f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
2136318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
2137318b02fdSBill Paul 		}
2138318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
213996f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
214096f2e892SBill Paul 		break;
21411e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
21421e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
214379d11e09SBill Paul 		/*
214479d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
214579d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
214679d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
214779d11e09SBill Paul 		 * get the right number of bits out of the
214879d11e09SBill Paul 		 * CRC routine.
214979d11e09SBill Paul 		 */
215079d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
215179d11e09SBill Paul 		    revision < DC_REVISION_98725)
215279d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
215396f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
215496f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2155318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
215696f2e892SBill Paul 		break;
21571e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
2158ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
2159ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2160ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2161ead7cde9SBill Paul 		break;
21621e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
216396f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
216479d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
2165318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
216696f2e892SBill Paul 		break;
21671e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
216896f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
216991cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
217096f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
217196f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
2172abe4e865SPyun YongHyeon 		if (sc->dc_pnic_rx_buf == NULL) {
2173abe4e865SPyun YongHyeon 			device_printf(sc->dc_dev,
2174abe4e865SPyun YongHyeon 			    "Could not allocate PNIC RX buffer\n");
2175abe4e865SPyun YongHyeon 			error = ENOMEM;
2176abe4e865SPyun YongHyeon 			goto fail;
2177abe4e865SPyun YongHyeon 		}
217896f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
217996f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
218096f2e892SBill Paul 		break;
21811e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
218296f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
218396f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
218496f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
218596f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
218696f2e892SBill Paul 		break;
21871e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
2188feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
21892dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
21902dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2191feb78939SJonathan Chen 		/*
2192feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2193feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
21942dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2195feb78939SJonathan Chen 		 */
21963097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2197feb78939SJonathan Chen 		break;
21981e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
21991af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
22001af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
22011af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
22021af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2203abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2204abe4e865SPyun YongHyeon 		if (error != 0)
2205abe4e865SPyun YongHyeon 			goto fail;
22061af8bec7SBill Paul 		break;
220752ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261):
220852ca7ee2SPyun YongHyeon 	case DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5263):
220952ca7ee2SPyun YongHyeon 		if (sc->dc_info->dc_devid ==
221052ca7ee2SPyun YongHyeon 		    DC_DEVID(DC_VENDORID_ULI, DC_DEVICEID_M5261))
221152ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5261;
221252ca7ee2SPyun YongHyeon 		else
221352ca7ee2SPyun YongHyeon 			sc->dc_type = DC_TYPE_ULI_M5263;
221452ca7ee2SPyun YongHyeon 		/* TX buffers should be aligned on 4 byte boundary. */
221552ca7ee2SPyun YongHyeon 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
221652ca7ee2SPyun YongHyeon 		    DC_TX_ALIGN;
221752ca7ee2SPyun YongHyeon 		sc->dc_pmode = DC_PMODE_MII;
221852ca7ee2SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
221952ca7ee2SPyun YongHyeon 		if (error != 0)
222052ca7ee2SPyun YongHyeon 			goto fail;
222152ca7ee2SPyun YongHyeon 		break;
222296f2e892SBill Paul 	default:
22231e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
22241e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
222596f2e892SBill Paul 		break;
222696f2e892SBill Paul 	}
222796f2e892SBill Paul 
222896f2e892SBill Paul 	/* Save the cache line size. */
222988d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
223088d739dcSBill Paul 		sc->dc_cachesize = 0;
223188d739dcSBill Paul 	else
22321e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
223396f2e892SBill Paul 
223496f2e892SBill Paul 	/* Reset the adapter. */
223596f2e892SBill Paul 	dc_reset(sc);
223696f2e892SBill Paul 
223796f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2238feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
223996f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
224096f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
224196f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
224296f2e892SBill Paul 	}
224396f2e892SBill Paul 
224496f2e892SBill Paul 	/*
224596f2e892SBill Paul 	 * Try to learn something about the supported media.
224696f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
224796f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
224896f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
224996f2e892SBill Paul 	 * Intel 21143.
225096f2e892SBill Paul 	 */
2251abe4e865SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
2252abe4e865SPyun YongHyeon 		error = dc_parse_21143_srom(sc);
2253abe4e865SPyun YongHyeon 		if (error != 0)
2254abe4e865SPyun YongHyeon 			goto fail;
2255abe4e865SPyun YongHyeon 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
225696f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
225796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
225896f2e892SBill Paul 		else
225996f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
226096f2e892SBill Paul 	} else if (!sc->dc_pmode)
226196f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
226296f2e892SBill Paul 
226396f2e892SBill Paul 	/*
226496f2e892SBill Paul 	 * Get station address from the EEPROM.
226596f2e892SBill Paul 	 */
226696f2e892SBill Paul 	switch(sc->dc_type) {
226796f2e892SBill Paul 	case DC_TYPE_98713:
226896f2e892SBill Paul 	case DC_TYPE_98713A:
226996f2e892SBill Paul 	case DC_TYPE_987x5:
227096f2e892SBill Paul 	case DC_TYPE_PNICII:
227196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
227296f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
227396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
227496f2e892SBill Paul 		break;
227596f2e892SBill Paul 	case DC_TYPE_PNIC:
227696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
227796f2e892SBill Paul 		break;
227896f2e892SBill Paul 	case DC_TYPE_DM9102:
2279ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2280ec6a7299SMaxime Henrion #ifdef __sparc64__
2281ec6a7299SMaxime Henrion 		/*
2282ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2283802cab03SMarius Strobl 		 * the EEPROM is all zero and we have to get it from the FCode.
2284ec6a7299SMaxime Henrion 		 */
2285802cab03SMarius Strobl 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
22868069c79dSRuslan Ermilov 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2287ec6a7299SMaxime Henrion #endif
2288ec6a7299SMaxime Henrion 		break;
228996f2e892SBill Paul 	case DC_TYPE_21143:
229096f2e892SBill Paul 	case DC_TYPE_ASIX:
229196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
229296f2e892SBill Paul 		break;
229396f2e892SBill Paul 	case DC_TYPE_AL981:
2294593a1aeaSMartin Blapp 	case DC_TYPE_AN983:
22952e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR0);
22962e3d4b79SPyun YongHyeon 		mac = (uint8_t *)&eaddr[0];
22972e3d4b79SPyun YongHyeon 		mac[0] = (reg >> 0) & 0xff;
22982e3d4b79SPyun YongHyeon 		mac[1] = (reg >> 8) & 0xff;
22992e3d4b79SPyun YongHyeon 		mac[2] = (reg >> 16) & 0xff;
23002e3d4b79SPyun YongHyeon 		mac[3] = (reg >> 24) & 0xff;
23012e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR1);
23022e3d4b79SPyun YongHyeon 		mac[4] = (reg >> 0) & 0xff;
23032e3d4b79SPyun YongHyeon 		mac[5] = (reg >> 8) & 0xff;
230496f2e892SBill Paul 		break;
23051af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
23060934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
23070934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
23081af8bec7SBill Paul 		break;
2309feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
23100934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2311e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2312e7b01d07SWarner Losh 		if (!mac) {
2313e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2314608654d4SNate Lawson 			error = ENXIO;
2315e7b01d07SWarner Losh 			goto fail;
2316e7b01d07SWarner Losh 		}
2317e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2318feb78939SJonathan Chen 		break;
231952ca7ee2SPyun YongHyeon 	case DC_TYPE_ULI_M5261:
232052ca7ee2SPyun YongHyeon 	case DC_TYPE_ULI_M5263:
232152ca7ee2SPyun YongHyeon 		srom = (uint16_t *)sc->dc_srom;
232252ca7ee2SPyun YongHyeon 		if (srom == NULL || *srom == 0xFFFF || *srom == 0) {
232352ca7ee2SPyun YongHyeon 			/*
232452ca7ee2SPyun YongHyeon 			 * No valid SROM present, read station address
232552ca7ee2SPyun YongHyeon 			 * from ID Table.
232652ca7ee2SPyun YongHyeon 			 */
232752ca7ee2SPyun YongHyeon 			device_printf(dev,
232852ca7ee2SPyun YongHyeon 			    "Reading station address from ID Table.\n");
232952ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_BUSCTL, 0x10000);
233052ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x01C0);
233152ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0000);
233252ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0010);
233352ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_10BTCTRL, 0x0000);
233452ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x0000);
233552ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x01B0);
233652ca7ee2SPyun YongHyeon 			mac = (uint8_t *)eaddr;
233752ca7ee2SPyun YongHyeon 			for (n = 0; n < ETHER_ADDR_LEN; n++)
233852ca7ee2SPyun YongHyeon 				mac[n] = (uint8_t)CSR_READ_4(sc, DC_10BTCTRL);
233952ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_SIARESET, 0x0000);
234052ca7ee2SPyun YongHyeon 			CSR_WRITE_4(sc, DC_BUSCTL, 0x0000);
234152ca7ee2SPyun YongHyeon 			DELAY(10);
234252ca7ee2SPyun YongHyeon 		} else
234352ca7ee2SPyun YongHyeon 			dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3,
234452ca7ee2SPyun YongHyeon 			    0);
234552ca7ee2SPyun YongHyeon 		break;
234696f2e892SBill Paul 	default:
234796f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
234896f2e892SBill Paul 		break;
234996f2e892SBill Paul 	}
235096f2e892SBill Paul 
235139d76ed6SPyun YongHyeon 	bcopy(eaddr, sc->dc_eaddr, sizeof(eaddr));
235239d76ed6SPyun YongHyeon 	/*
235339d76ed6SPyun YongHyeon 	 * If we still have invalid station address, see whether we can
235439d76ed6SPyun YongHyeon 	 * find station address for chip 0.  Some multi-port controllers
235539d76ed6SPyun YongHyeon 	 * just store station address for chip 0 if they have a shared
235639d76ed6SPyun YongHyeon 	 * SROM.
235739d76ed6SPyun YongHyeon 	 */
235839d76ed6SPyun YongHyeon 	if ((sc->dc_eaddr[0] == 0 && (sc->dc_eaddr[1] & ~0xffff) == 0) ||
235939d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[0] == 0xffffffff &&
236039d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[1] & 0xffff) == 0xffff)) {
2361b289c607SPyun YongHyeon 		error = dc_check_multiport(sc);
2362b289c607SPyun YongHyeon 		if (error == 0) {
236339d76ed6SPyun YongHyeon 			bcopy(sc->dc_eaddr, eaddr, sizeof(eaddr));
2364b289c607SPyun YongHyeon 			/* Extract media information. */
2365b289c607SPyun YongHyeon 			if (DC_IS_INTEL(sc) && sc->dc_srom != NULL) {
2366b289c607SPyun YongHyeon 				while (sc->dc_mi != NULL) {
2367b289c607SPyun YongHyeon 					m = sc->dc_mi->dc_next;
2368b289c607SPyun YongHyeon 					free(sc->dc_mi, M_DEVBUF);
2369b289c607SPyun YongHyeon 					sc->dc_mi = m;
2370b289c607SPyun YongHyeon 				}
2371b289c607SPyun YongHyeon 				error = dc_parse_21143_srom(sc);
2372b289c607SPyun YongHyeon 				if (error != 0)
2373b289c607SPyun YongHyeon 					goto fail;
2374b289c607SPyun YongHyeon 			}
2375b289c607SPyun YongHyeon 		} else if (error == ENOMEM)
2376b289c607SPyun YongHyeon 			goto fail;
2377b289c607SPyun YongHyeon 		else
2378b289c607SPyun YongHyeon 			error = 0;
237939d76ed6SPyun YongHyeon 	}
238039d76ed6SPyun YongHyeon 
23815f14ee23SPyun YongHyeon 	if ((error = dc_dma_alloc(sc)) != 0)
238256e5e7aeSMaxime Henrion 		goto fail;
238396f2e892SBill Paul 
2384fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2385fc74a9f9SBrooks Davis 	if (ifp == NULL) {
238622f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2387fc74a9f9SBrooks Davis 		error = ENOSPC;
2388fc74a9f9SBrooks Davis 		goto fail;
2389fc74a9f9SBrooks Davis 	}
239096f2e892SBill Paul 	ifp->if_softc = sc;
23919bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
23923d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
239396f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
239496f2e892SBill Paul 	ifp->if_start = dc_start;
239596f2e892SBill Paul 	ifp->if_init = dc_init;
2396cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2397cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2398cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
239996f2e892SBill Paul 
240096f2e892SBill Paul 	/*
24015c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
24025c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
24035c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
24045c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
24055c1cfac4SBill Paul 	 * driver instead.
240696f2e892SBill Paul 	 */
24078e5d93dbSMarius Strobl 	tmp = 0;
24085c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
24095c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
24105c1cfac4SBill Paul 		tmp = sc->dc_pmode;
24115c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
24125c1cfac4SBill Paul 	}
24135c1cfac4SBill Paul 
24146d431b17SWarner Losh 	/*
24156d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
24168e5d93dbSMarius Strobl 	 * to the MII.  This needs to be done before mii_attach so that
24176d431b17SWarner Losh 	 * we can actually see them.
24186d431b17SWarner Losh 	 */
24196d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
24206d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
24216d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24226d431b17SWarner Losh 		DELAY(10);
24236d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
24246d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24256d431b17SWarner Losh 		DELAY(10);
24266d431b17SWarner Losh 	}
24276d431b17SWarner Losh 
24288e5d93dbSMarius Strobl 	phy = MII_PHY_ANY;
24298e5d93dbSMarius Strobl 	/*
24308e5d93dbSMarius Strobl 	 * Note: both the AL981 and AN983 have internal PHYs, however the
24318e5d93dbSMarius Strobl 	 * AL981 provides direct access to the PHY registers while the AN983
24328e5d93dbSMarius Strobl 	 * uses a serial MII interface. The AN983's MII interface is also
24338e5d93dbSMarius Strobl 	 * buggy in that you can read from any MII address (0 to 31), but
24348e5d93dbSMarius Strobl 	 * only address 1 behaves normally. To deal with both cases, we
24358e5d93dbSMarius Strobl 	 * pretend that the PHY is at MII address 1.
24368e5d93dbSMarius Strobl 	 */
24378e5d93dbSMarius Strobl 	if (DC_IS_ADMTEK(sc))
24388e5d93dbSMarius Strobl 		phy = DC_ADMTEK_PHYADDR;
24398e5d93dbSMarius Strobl 
24408e5d93dbSMarius Strobl 	/*
24418e5d93dbSMarius Strobl 	 * Note: the ukphy probes of the RS7112 report a PHY at MII address
24428e5d93dbSMarius Strobl 	 * 0 (possibly HomePNA?) and 1 (ethernet) so we only respond to the
24438e5d93dbSMarius Strobl 	 * correct one.
24448e5d93dbSMarius Strobl 	 */
24458e5d93dbSMarius Strobl 	if (DC_IS_CONEXANT(sc))
24468e5d93dbSMarius Strobl 		phy = DC_CONEXANT_PHYADDR;
24478e5d93dbSMarius Strobl 
24488e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24498e5d93dbSMarius Strobl 	    dc_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
245096f2e892SBill Paul 
245196f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
24525c1cfac4SBill Paul 		sc->dc_pmode = tmp;
24535c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
245496f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2455042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
245678999dd1SBill Paul 		/*
245778999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
245878999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
245978999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
246078999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
246178999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
246278999dd1SBill Paul 		 */
24631e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
24641e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
246578999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
2466166e31d9SMarius Strobl 		error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
2467166e31d9SMarius Strobl 		    dc_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
2468166e31d9SMarius Strobl 		    MII_OFFSET_ANY, 0);
246996f2e892SBill Paul 	}
247096f2e892SBill Paul 
247196f2e892SBill Paul 	if (error) {
24728e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
247396f2e892SBill Paul 		goto fail;
247496f2e892SBill Paul 	}
247596f2e892SBill Paul 
2476028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2477028a8491SMartin Blapp 		/*
2478028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2479028a8491SMartin Blapp 		 */
2480028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2481028a8491SMartin Blapp 	}
2482028a8491SMartin Blapp 
248396f2e892SBill Paul 	/*
2484db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2485db40c1aeSDoug Ambrisko 	 */
2486db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
24879ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
248840929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2489e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2490e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2491e695984eSRuslan Ermilov #endif
2492db40c1aeSDoug Ambrisko 
2493c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2494b1d16143SMarius Strobl 	callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
249596f2e892SBill Paul 
2496608654d4SNate Lawson 	/*
2497608654d4SNate Lawson 	 * Call MI attach routine.
2498608654d4SNate Lawson 	 */
24998df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2500608654d4SNate Lawson 
250154f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2502c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2503ef544f63SPaolo Pisati 	    NULL, dc_intr, sc, &sc->dc_intrhand);
2504608654d4SNate Lawson 
2505608654d4SNate Lawson 	if (error) {
250622f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2507693f4477SNate Lawson 		ether_ifdetach(ifp);
250854f1f1d1SNate Lawson 		goto fail;
2509608654d4SNate Lawson 	}
2510510a809eSMike Smith 
251196f2e892SBill Paul fail:
251254f1f1d1SNate Lawson 	if (error)
251354f1f1d1SNate Lawson 		dc_detach(dev);
251496f2e892SBill Paul 	return (error);
251596f2e892SBill Paul }
251696f2e892SBill Paul 
2517693f4477SNate Lawson /*
2518693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2519693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2520693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2521693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2522693f4477SNate Lawson  * allocated.
2523693f4477SNate Lawson  */
2524e3d2833aSAlfred Perlstein static int
25250934f18aSMaxime Henrion dc_detach(device_t dev)
252696f2e892SBill Paul {
252796f2e892SBill Paul 	struct dc_softc *sc;
252896f2e892SBill Paul 	struct ifnet *ifp;
25295c1cfac4SBill Paul 	struct dc_mediainfo *m;
253096f2e892SBill Paul 
253196f2e892SBill Paul 	sc = device_get_softc(dev);
253259f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2533d1ce9105SBill Paul 
2534fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
253596f2e892SBill Paul 
253640929967SGleb Smirnoff #ifdef DEVICE_POLLING
2537166e31d9SMarius Strobl 	if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
253840929967SGleb Smirnoff 		ether_poll_deregister(ifp);
253940929967SGleb Smirnoff #endif
254040929967SGleb Smirnoff 
2541693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2542214073e5SWarner Losh 	if (device_is_attached(dev)) {
2543c8b27acaSJohn Baldwin 		DC_LOCK(sc);
254496f2e892SBill Paul 		dc_stop(sc);
2545c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2546c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
2547b1d16143SMarius Strobl 		callout_drain(&sc->dc_wdog_ch);
25489ef8b520SSam Leffler 		ether_ifdetach(ifp);
2549693f4477SNate Lawson 	}
2550693f4477SNate Lawson 	if (sc->dc_miibus)
255196f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
255254f1f1d1SNate Lawson 	bus_generic_detach(dev);
255396f2e892SBill Paul 
255454f1f1d1SNate Lawson 	if (sc->dc_intrhand)
255596f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
255654f1f1d1SNate Lawson 	if (sc->dc_irq)
255796f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
255854f1f1d1SNate Lawson 	if (sc->dc_res)
255996f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
256096f2e892SBill Paul 
2561166e31d9SMarius Strobl 	if (ifp != NULL)
25626a3033a8SWarner Losh 		if_free(ifp);
25636a3033a8SWarner Losh 
25645f14ee23SPyun YongHyeon 	dc_dma_free(sc);
256556e5e7aeSMaxime Henrion 
256696f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
256796f2e892SBill Paul 
25685c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
25695c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
25705c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
25715c1cfac4SBill Paul 		sc->dc_mi = m;
25725c1cfac4SBill Paul 	}
25737efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
25745c1cfac4SBill Paul 
2575d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
257696f2e892SBill Paul 
257796f2e892SBill Paul 	return (0);
257896f2e892SBill Paul }
257996f2e892SBill Paul 
258096f2e892SBill Paul /*
258196f2e892SBill Paul  * Initialize the transmit descriptors.
258296f2e892SBill Paul  */
2583e3d2833aSAlfred Perlstein static int
25840934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
258596f2e892SBill Paul {
258696f2e892SBill Paul 	struct dc_chain_data *cd;
258796f2e892SBill Paul 	struct dc_list_data *ld;
258801faf54bSLuigi Rizzo 	int i, nexti;
258996f2e892SBill Paul 
259096f2e892SBill Paul 	cd = &sc->dc_cdata;
25915f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
259296f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2593b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2594b3811c95SMaxime Henrion 			nexti = 0;
2595b3811c95SMaxime Henrion 		else
2596b3811c95SMaxime Henrion 			nexti = i + 1;
259752c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_status = 0;
259852c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_ctl = 0;
259952c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_data = 0;
2600af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
260196f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
260296f2e892SBill Paul 	}
260396f2e892SBill Paul 
260496f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
260506d23883SPyun YongHyeon 	cd->dc_tx_pkts = 0;
26065f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
260756e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
260896f2e892SBill Paul 	return (0);
260996f2e892SBill Paul }
261096f2e892SBill Paul 
261196f2e892SBill Paul /*
261296f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
261396f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
261496f2e892SBill Paul  * points back to the first.
261596f2e892SBill Paul  */
2616e3d2833aSAlfred Perlstein static int
26170934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
261896f2e892SBill Paul {
261996f2e892SBill Paul 	struct dc_chain_data *cd;
262096f2e892SBill Paul 	struct dc_list_data *ld;
262101faf54bSLuigi Rizzo 	int i, nexti;
262296f2e892SBill Paul 
262396f2e892SBill Paul 	cd = &sc->dc_cdata;
26245f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
262596f2e892SBill Paul 
262696f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
26275f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0)
262896f2e892SBill Paul 			return (ENOBUFS);
2629b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2630b3811c95SMaxime Henrion 			nexti = 0;
2631b3811c95SMaxime Henrion 		else
2632b3811c95SMaxime Henrion 			nexti = i + 1;
2633af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
263496f2e892SBill Paul 	}
263596f2e892SBill Paul 
263696f2e892SBill Paul 	cd->dc_rx_prod = 0;
26375f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
263856e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
263996f2e892SBill Paul 	return (0);
264096f2e892SBill Paul }
264196f2e892SBill Paul 
264296f2e892SBill Paul /*
264396f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
264496f2e892SBill Paul  */
2645e3d2833aSAlfred Perlstein static int
26465f14ee23SPyun YongHyeon dc_newbuf(struct dc_softc *sc, int i)
264796f2e892SBill Paul {
26485f14ee23SPyun YongHyeon 	struct mbuf *m;
26495f14ee23SPyun YongHyeon 	bus_dmamap_t map;
265082a67a70SMarius Strobl 	bus_dma_segment_t segs[1];
265182a67a70SMarius Strobl 	int error, nseg;
265296f2e892SBill Paul 
2653*c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
26545f14ee23SPyun YongHyeon 	if (m == NULL)
265596f2e892SBill Paul 		return (ENOBUFS);
26565f14ee23SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
26575f14ee23SPyun YongHyeon 	m_adj(m, sizeof(u_int64_t));
265896f2e892SBill Paul 
265996f2e892SBill Paul 	/*
266096f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
266196f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
266296f2e892SBill Paul 	 * 82c169 chips.
266396f2e892SBill Paul 	 */
266496f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
26655f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
266696f2e892SBill Paul 
26675f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_rx_mtag, sc->dc_sparemap,
26685f14ee23SPyun YongHyeon 	    m, segs, &nseg, 0);
266956e5e7aeSMaxime Henrion 	if (error) {
26705f14ee23SPyun YongHyeon 		m_freem(m);
267156e5e7aeSMaxime Henrion 		return (error);
267256e5e7aeSMaxime Henrion 	}
26735f14ee23SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: wrong number of segments (%d)", __func__,
26745f14ee23SPyun YongHyeon 	    nseg));
26755f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_rx_chain[i] != NULL)
26765f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i]);
267796f2e892SBill Paul 
26785f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_rx_map[i];
26795f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
26805f14ee23SPyun YongHyeon 	sc->dc_sparemap = map;
26815f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_chain[i] = m;
26825f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
268356e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
26845f14ee23SPyun YongHyeon 
26855f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
26865f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_data =
26875f14ee23SPyun YongHyeon 	    htole32(DC_ADDR_LO(segs[0].ds_addr));
26885f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
26895f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
269056e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
269196f2e892SBill Paul 	return (0);
269296f2e892SBill Paul }
269396f2e892SBill Paul 
269496f2e892SBill Paul /*
269596f2e892SBill Paul  * Grrrrr.
269696f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
269796f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
269896f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
269996f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
270096f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
270196f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
270296f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
270396f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
270496f2e892SBill Paul  *
270596f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
270696f2e892SBill Paul  * Here's what we know:
270796f2e892SBill Paul  *
270896f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
270996f2e892SBill Paul  *   descriptors uploaded.
271096f2e892SBill Paul  *
271196f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
271296f2e892SBill Paul  *   total data upload.
271396f2e892SBill Paul  *
271496f2e892SBill Paul  * - We know the size of the desired received frame because it will be
271596f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
271696f2e892SBill Paul  *
271796f2e892SBill Paul  * Here's what we do:
271896f2e892SBill Paul  *
271996f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
272096f2e892SBill Paul  *   This means that we know that the buffer contents should be all
272196f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
272296f2e892SBill Paul  *
272396f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
272496f2e892SBill Paul  *   ethernet CRC at the end.
272596f2e892SBill Paul  *
272696f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
272796f2e892SBill Paul  *
272896f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
272996f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
273096f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
273196f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
273296f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
273396f2e892SBill Paul  *   we won't be fooled.
273496f2e892SBill Paul  *
273596f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
273696f2e892SBill Paul  *   that value from the current pointer location. This brings us
273796f2e892SBill Paul  *   to the start of the actual received packet.
273896f2e892SBill Paul  *
273996f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
274096f2e892SBill Paul  *   frame length.
274196f2e892SBill Paul  *
274296f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
274396f2e892SBill Paul  * the time.
274496f2e892SBill Paul  */
274596f2e892SBill Paul 
274696f2e892SBill Paul #define	DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2747e3d2833aSAlfred Perlstein static void
27480934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
274996f2e892SBill Paul {
275096f2e892SBill Paul 	struct dc_desc *cur_rx;
275196f2e892SBill Paul 	struct dc_desc *c = NULL;
275296f2e892SBill Paul 	struct mbuf *m = NULL;
275396f2e892SBill Paul 	unsigned char *ptr;
275496f2e892SBill Paul 	int i, total_len;
2755ee320f98SPyun YongHyeon 	uint32_t rxstat = 0;
275696f2e892SBill Paul 
275796f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
27585f14ee23SPyun YongHyeon 	cur_rx = &sc->dc_ldata.dc_rx_list[idx];
275996f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
27601edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
276196f2e892SBill Paul 
276296f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
276396f2e892SBill Paul 	while (1) {
27645f14ee23SPyun YongHyeon 		c = &sc->dc_ldata.dc_rx_list[i];
2765af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
276696f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
276796f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
276896f2e892SBill Paul 		ptr += DC_RXLEN;
276996f2e892SBill Paul 		/* If this is the last buffer, break out. */
277096f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
277196f2e892SBill Paul 			break;
27725f14ee23SPyun YongHyeon 		dc_discard_rxbuf(sc, i);
277396f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
277496f2e892SBill Paul 	}
277596f2e892SBill Paul 
277696f2e892SBill Paul 	/* Find the length of the actual receive frame. */
277796f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
277896f2e892SBill Paul 
277996f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
278096f2e892SBill Paul 	while (*ptr == 0x00)
278196f2e892SBill Paul 		ptr--;
278296f2e892SBill Paul 
278396f2e892SBill Paul 	/* Round off. */
278496f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
278596f2e892SBill Paul 		ptr -= 1;
278696f2e892SBill Paul 
278796f2e892SBill Paul 	/* Now find the start of the frame. */
278896f2e892SBill Paul 	ptr -= total_len;
278996f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
279096f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
279196f2e892SBill Paul 
279296f2e892SBill Paul 	/*
279396f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
279496f2e892SBill Paul 	 * the status word to make it look like a successful
279596f2e892SBill Paul 	 * frame reception.
279696f2e892SBill Paul 	 */
279796f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2798af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
279996f2e892SBill Paul }
280096f2e892SBill Paul 
280196f2e892SBill Paul /*
280273bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
280373bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
280473bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
280573bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
280673bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
280773bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
280873bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
280973bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
281073bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
281173bf949cSBill Paul  */
2812e3d2833aSAlfred Perlstein static int
28130934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
281473bf949cSBill Paul {
281573bf949cSBill Paul 	struct dc_desc *cur_rx;
28160934f18aSMaxime Henrion 	int i, pos;
281773bf949cSBill Paul 
281873bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
281973bf949cSBill Paul 
282073bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
28215f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[pos];
2822af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
282373bf949cSBill Paul 			break;
282473bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
282573bf949cSBill Paul 	}
282673bf949cSBill Paul 
282773bf949cSBill Paul 	/* If the ring really is empty, then just return. */
282873bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
282973bf949cSBill Paul 		return (0);
283073bf949cSBill Paul 
283173bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
283273bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
283373bf949cSBill Paul 
283473bf949cSBill Paul 	return (EAGAIN);
283573bf949cSBill Paul }
283673bf949cSBill Paul 
28375f14ee23SPyun YongHyeon static void
28385f14ee23SPyun YongHyeon dc_discard_rxbuf(struct dc_softc *sc, int i)
28395f14ee23SPyun YongHyeon {
28405f14ee23SPyun YongHyeon 	struct mbuf *m;
28415f14ee23SPyun YongHyeon 
28425f14ee23SPyun YongHyeon 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
28435f14ee23SPyun YongHyeon 		m = sc->dc_cdata.dc_rx_chain[i];
28445f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
28455f14ee23SPyun YongHyeon 	}
28465f14ee23SPyun YongHyeon 
28475f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
28485f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
28495f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_PREREAD |
28505f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
28515f14ee23SPyun YongHyeon }
28525f14ee23SPyun YongHyeon 
285373bf949cSBill Paul /*
285496f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
285596f2e892SBill Paul  * the higher level protocols.
285696f2e892SBill Paul  */
28571abcdbd1SAttilio Rao static int
28580934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
285996f2e892SBill Paul {
28605f14ee23SPyun YongHyeon 	struct mbuf *m;
286196f2e892SBill Paul 	struct ifnet *ifp;
286296f2e892SBill Paul 	struct dc_desc *cur_rx;
28631abcdbd1SAttilio Rao 	int i, total_len, rx_npkts;
2864ee320f98SPyun YongHyeon 	uint32_t rxstat;
286596f2e892SBill Paul 
28665120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
28675120abbfSSam Leffler 
2868fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
28691abcdbd1SAttilio Rao 	rx_npkts = 0;
287096f2e892SBill Paul 
28715f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_POSTREAD |
28725f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
28735f14ee23SPyun YongHyeon 	for (i = sc->dc_cdata.dc_rx_prod;
28745f14ee23SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
28755f14ee23SPyun YongHyeon 	    DC_INC(i, DC_RX_LIST_CNT)) {
2876e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
287740929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2878e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2879e4fc250cSLuigi Rizzo 				break;
2880e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2881e4fc250cSLuigi Rizzo 		}
28820934f18aSMaxime Henrion #endif
28835f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[i];
2884af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
28855f14ee23SPyun YongHyeon 		if ((rxstat & DC_RXSTAT_OWN) != 0)
28865f14ee23SPyun YongHyeon 			break;
288796f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
28885f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
288956e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
289096f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
2891848a02fcSPyun YongHyeon 		rx_npkts++;
289296f2e892SBill Paul 
289396f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
289496f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
289596f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
289696f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
28975f14ee23SPyun YongHyeon 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0)
289896f2e892SBill Paul 					continue;
289996f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2900af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
290196f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
290296f2e892SBill Paul 			}
290396f2e892SBill Paul 		}
290496f2e892SBill Paul 
290596f2e892SBill Paul 		/*
290696f2e892SBill Paul 		 * If an error occurs, update stats, clear the
290796f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
290896f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2909db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
29100934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
291196f2e892SBill Paul 		 */
2912db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2913db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2914db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2915db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2916db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
291796f2e892SBill Paul 				ifp->if_ierrors++;
291896f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
291996f2e892SBill Paul 					ifp->if_collisions++;
29205f14ee23SPyun YongHyeon 				dc_discard_rxbuf(sc, i);
29215f14ee23SPyun YongHyeon 				if (rxstat & DC_RXSTAT_CRCERR)
292296f2e892SBill Paul 					continue;
29235f14ee23SPyun YongHyeon 				else {
29248f382a1fSPyun YongHyeon 					ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2925c8b27acaSJohn Baldwin 					dc_init_locked(sc);
29261abcdbd1SAttilio Rao 					return (rx_npkts);
292796f2e892SBill Paul 				}
292896f2e892SBill Paul 			}
2929db40c1aeSDoug Ambrisko 		}
293096f2e892SBill Paul 
293196f2e892SBill Paul 		/* No errors; receive the packet. */
293296f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2933432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
293401faf54bSLuigi Rizzo 		/*
2935432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
293601faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
293701faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
293801faf54bSLuigi Rizzo 		 * copy done in m_devget().
293901faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
294001faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
294101faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
294201faf54bSLuigi Rizzo 		 */
29435f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0) {
29445f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
29455f14ee23SPyun YongHyeon 			ifp->if_iqdrops++;
29465f14ee23SPyun YongHyeon 			continue;
29475f14ee23SPyun YongHyeon 		}
294801faf54bSLuigi Rizzo 		m->m_pkthdr.rcvif = ifp;
294901faf54bSLuigi Rizzo 		m->m_pkthdr.len = m->m_len = total_len;
29505f14ee23SPyun YongHyeon #else
295101faf54bSLuigi Rizzo 		{
29525f14ee23SPyun YongHyeon 			struct mbuf *m0;
29535f14ee23SPyun YongHyeon 
295401faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
295501faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
29565f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
295796f2e892SBill Paul 			if (m0 == NULL) {
29585f14ee23SPyun YongHyeon 				ifp->if_iqdrops++;
295996f2e892SBill Paul 				continue;
296096f2e892SBill Paul 			}
296196f2e892SBill Paul 			m = m0;
296201faf54bSLuigi Rizzo 		}
29635f14ee23SPyun YongHyeon #endif
296496f2e892SBill Paul 
296596f2e892SBill Paul 		ifp->if_ipackets++;
29665120abbfSSam Leffler 		DC_UNLOCK(sc);
29679ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
29685120abbfSSam Leffler 		DC_LOCK(sc);
296996f2e892SBill Paul 	}
297096f2e892SBill Paul 
297196f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
29721abcdbd1SAttilio Rao 	return (rx_npkts);
297396f2e892SBill Paul }
297496f2e892SBill Paul 
297596f2e892SBill Paul /*
297696f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
297796f2e892SBill Paul  * the list buffers.
297896f2e892SBill Paul  */
2979e3d2833aSAlfred Perlstein static void
29800934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
298196f2e892SBill Paul {
29825f14ee23SPyun YongHyeon 	struct dc_desc *cur_tx;
298396f2e892SBill Paul 	struct ifnet *ifp;
29845f14ee23SPyun YongHyeon 	int idx, setup;
2985ee320f98SPyun YongHyeon 	uint32_t ctl, txstat;
298696f2e892SBill Paul 
298706d23883SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt == 0)
298806d23883SPyun YongHyeon 		return;
298906d23883SPyun YongHyeon 
2990fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
299196f2e892SBill Paul 
299296f2e892SBill Paul 	/*
299396f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
299496f2e892SBill Paul 	 * frames that have been transmitted.
299596f2e892SBill Paul 	 */
2996cb94db27SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_POSTREAD |
29975f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
29985f14ee23SPyun YongHyeon 	setup = 0;
29995f14ee23SPyun YongHyeon 	for (idx = sc->dc_cdata.dc_tx_cons; idx != sc->dc_cdata.dc_tx_prod;
30005f14ee23SPyun YongHyeon 	    DC_INC(idx, DC_TX_LIST_CNT), sc->dc_cdata.dc_tx_cnt--) {
30015f14ee23SPyun YongHyeon 		cur_tx = &sc->dc_ldata.dc_tx_list[idx];
3002af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
3003af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
300496f2e892SBill Paul 
300596f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
300696f2e892SBill Paul 			break;
300796f2e892SBill Paul 
30085f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_chain[idx] == NULL)
30095f14ee23SPyun YongHyeon 			continue;
30105f14ee23SPyun YongHyeon 
3011af4358c7SMaxime Henrion 		if (ctl & DC_TXCTL_SETUP) {
30125f14ee23SPyun YongHyeon 			cur_tx->dc_ctl = htole32(ctl & ~DC_TXCTL_SETUP);
30135f14ee23SPyun YongHyeon 			setup++;
30145f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
30155f14ee23SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
301696f2e892SBill Paul 			/*
301796f2e892SBill Paul 			 * Yes, the PNIC is so brain damaged
301896f2e892SBill Paul 			 * that it will sometimes generate a TX
301996f2e892SBill Paul 			 * underrun error while DMAing the RX
302096f2e892SBill Paul 			 * filter setup frame. If we detect this,
302196f2e892SBill Paul 			 * we have to send the setup frame again,
302296f2e892SBill Paul 			 * or else the filter won't be programmed
302396f2e892SBill Paul 			 * correctly.
302496f2e892SBill Paul 			 */
302596f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
302696f2e892SBill Paul 				if (txstat & DC_TXSTAT_ERRSUM)
302796f2e892SBill Paul 					dc_setfilt(sc);
302896f2e892SBill Paul 			}
302996f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
303096f2e892SBill Paul 			continue;
303196f2e892SBill Paul 		}
303296f2e892SBill Paul 
303329a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
3034feb78939SJonathan Chen 			/*
3035feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
3036feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
303729a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
303829a2220aSBill Paul 			 * Who knows, but Conexant chips have the
303929a2220aSBill Paul 			 * same problem. Maybe they took lessons
304029a2220aSBill Paul 			 * from Xircom.
304129a2220aSBill Paul 			 */
3042feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
3043feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
3044feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
3045feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
3046feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
3047feb78939SJonathan Chen 		} else {
304896f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
304996f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
305096f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
305196f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
305296f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
3053feb78939SJonathan Chen 		}
305496f2e892SBill Paul 
305596f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
305696f2e892SBill Paul 			ifp->if_oerrors++;
305796f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
305896f2e892SBill Paul 				ifp->if_collisions++;
305996f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
306096f2e892SBill Paul 				ifp->if_collisions++;
306196f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
30628f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3063c8b27acaSJohn Baldwin 				dc_init_locked(sc);
306496f2e892SBill Paul 				return;
306596f2e892SBill Paul 			}
306652c43a47SPyun YongHyeon 		} else
306752c43a47SPyun YongHyeon 			ifp->if_opackets++;
306896f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
306996f2e892SBill Paul 
30705f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
307156e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
30725f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
307396f2e892SBill Paul 		m_freem(sc->dc_cdata.dc_tx_chain[idx]);
307496f2e892SBill Paul 		sc->dc_cdata.dc_tx_chain[idx] = NULL;
307596f2e892SBill Paul 	}
307696f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
307782a67a70SMarius Strobl 
30785f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
307913f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
30803e0e6726SMarius Strobl 		if (sc->dc_cdata.dc_tx_cnt == 0)
30813e0e6726SMarius Strobl 			sc->dc_wdog_timer = 0;
308296f2e892SBill Paul 	}
30835f14ee23SPyun YongHyeon 	if (setup > 0)
30845f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
30855f14ee23SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
30865f14ee23SPyun YongHyeon }
308796f2e892SBill Paul 
3088e3d2833aSAlfred Perlstein static void
30890934f18aSMaxime Henrion dc_tick(void *xsc)
309096f2e892SBill Paul {
309196f2e892SBill Paul 	struct dc_softc *sc;
309296f2e892SBill Paul 	struct mii_data *mii;
309396f2e892SBill Paul 	struct ifnet *ifp;
3094ee320f98SPyun YongHyeon 	uint32_t r;
309596f2e892SBill Paul 
309696f2e892SBill Paul 	sc = xsc;
3097c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3098fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
309996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
310096f2e892SBill Paul 
310106d23883SPyun YongHyeon 	/*
310206d23883SPyun YongHyeon 	 * Reclaim transmitted frames for controllers that do
310306d23883SPyun YongHyeon 	 * not generate TX completion interrupt for every frame.
310406d23883SPyun YongHyeon 	 */
310506d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR)
310606d23883SPyun YongHyeon 		dc_txeof(sc);
310706d23883SPyun YongHyeon 
310896f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
3109318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
3110318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
3111318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3112318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
311396f2e892SBill Paul 				sc->dc_link = 0;
3114318b02fdSBill Paul 				mii_mediachg(mii);
3115318b02fdSBill Paul 			}
3116318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3117318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
3118318b02fdSBill Paul 				sc->dc_link = 0;
3119318b02fdSBill Paul 				mii_mediachg(mii);
3120318b02fdSBill Paul 			}
3121d675147eSBill Paul 			if (sc->dc_link == 0)
312296f2e892SBill Paul 				mii_tick(mii);
312396f2e892SBill Paul 		} else {
3124d0d67284SMarius Strobl 			/*
3125d0d67284SMarius Strobl 			 * For NICs which never report DC_RXSTATE_WAIT, we
3126d0d67284SMarius Strobl 			 * have to bite the bullet...
3127d0d67284SMarius Strobl 			 */
3128d0d67284SMarius Strobl 			if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
3129d0d67284SMarius Strobl 			    DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
3130d314ebf5SPyun YongHyeon 			    sc->dc_cdata.dc_tx_cnt == 0)
313196f2e892SBill Paul 				mii_tick(mii);
3132259b8d84SMartin Blapp 		}
313396f2e892SBill Paul 	} else
313496f2e892SBill Paul 		mii_tick(mii);
313596f2e892SBill Paul 
313696f2e892SBill Paul 	/*
313796f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
313896f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
313996f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
314096f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
314196f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
314296f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
314396f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
314496f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
314596f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
314696f2e892SBill Paul 	 * a screeching halt for several seconds.
314796f2e892SBill Paul 	 *
314896f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
314996f2e892SBill Paul 	 * any packets until a link has been established. After the
315096f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
315196f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
315296f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
315396f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
315496f2e892SBill Paul 	 */
3155d314ebf5SPyun YongHyeon 	if (sc->dc_link != 0 && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3156c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
315796f2e892SBill Paul 
3158318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
3159b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3160318b02fdSBill Paul 	else
3161b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
316296f2e892SBill Paul }
316396f2e892SBill Paul 
3164d467c136SBill Paul /*
3165d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
3166d467c136SBill Paul  * or switch to store and forward mode if we have to.
3167d467c136SBill Paul  */
3168e3d2833aSAlfred Perlstein static void
31690934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
3170d467c136SBill Paul {
3171d9efae03SPyun YongHyeon 	uint32_t netcfg, isr;
3172d9efae03SPyun YongHyeon 	int i, reinit;
3173d467c136SBill Paul 
3174d9efae03SPyun YongHyeon 	reinit = 0;
3175d9efae03SPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
3176d9efae03SPyun YongHyeon 	device_printf(sc->dc_dev, "TX underrun -- ");
3177d9efae03SPyun YongHyeon 	if ((sc->dc_flags & DC_TX_STORENFWD) == 0) {
3178d9efae03SPyun YongHyeon 		if (sc->dc_txthresh + DC_TXTHRESH_INC > DC_TXTHRESH_MAX) {
3179d9efae03SPyun YongHyeon 			printf("using store and forward mode\n");
3180d9efae03SPyun YongHyeon 			netcfg |= DC_NETCFG_STORENFWD;
3181d9efae03SPyun YongHyeon 		} else {
3182d9efae03SPyun YongHyeon 			printf("increasing TX threshold\n");
3183d9efae03SPyun YongHyeon 			sc->dc_txthresh += DC_TXTHRESH_INC;
3184d9efae03SPyun YongHyeon 			netcfg &= ~DC_NETCFG_TX_THRESH;
3185d9efae03SPyun YongHyeon 			netcfg |= sc->dc_txthresh;
31868f382a1fSPyun YongHyeon 		}
3187d467c136SBill Paul 
3188d467c136SBill Paul 		if (DC_IS_INTEL(sc)) {
3189d467c136SBill Paul 			/*
3190d467c136SBill Paul 			 * The real 21143 requires that the transmitter be idle
3191d467c136SBill Paul 			 * in order to change the transmit threshold or store
3192d467c136SBill Paul 			 * and forward state.
3193d467c136SBill Paul 			 */
3194d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg & ~DC_NETCFG_TX_ON);
3195d467c136SBill Paul 
3196d467c136SBill Paul 			for (i = 0; i < DC_TIMEOUT; i++) {
3197d467c136SBill Paul 				isr = CSR_READ_4(sc, DC_ISR);
3198d467c136SBill Paul 				if (isr & DC_ISR_TX_IDLE)
3199d467c136SBill Paul 					break;
3200d467c136SBill Paul 				DELAY(10);
3201d467c136SBill Paul 			}
3202d467c136SBill Paul 			if (i == DC_TIMEOUT) {
32036b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
3204432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
3205432120f2SMarius Strobl 				    __func__);
3206d9efae03SPyun YongHyeon 				reinit++;
3207d9efae03SPyun YongHyeon 			}
3208d9efae03SPyun YongHyeon 		}
3209d9efae03SPyun YongHyeon 	} else {
3210d9efae03SPyun YongHyeon 		printf("resetting\n");
3211d9efae03SPyun YongHyeon 		reinit++;
3212d9efae03SPyun YongHyeon 	}
3213d9efae03SPyun YongHyeon 
3214d9efae03SPyun YongHyeon 	if (reinit == 0) {
3215d9efae03SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, netcfg);
3216d9efae03SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3217d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg | DC_NETCFG_TX_ON);
3218d9efae03SPyun YongHyeon 	} else {
32198f382a1fSPyun YongHyeon 		sc->dc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3220c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3221d467c136SBill Paul 	}
3222d467c136SBill Paul }
3223d467c136SBill Paul 
3224e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3225e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3226e4fc250cSLuigi Rizzo 
32271abcdbd1SAttilio Rao static int
3228e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3229e4fc250cSLuigi Rizzo {
3230e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
32311abcdbd1SAttilio Rao 	int rx_npkts = 0;
3232e4fc250cSLuigi Rizzo 
323340929967SGleb Smirnoff 	DC_LOCK(sc);
323440929967SGleb Smirnoff 
323540929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
323640929967SGleb Smirnoff 		DC_UNLOCK(sc);
32371abcdbd1SAttilio Rao 		return (rx_npkts);
3238e4fc250cSLuigi Rizzo 	}
323940929967SGleb Smirnoff 
3240e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
32411abcdbd1SAttilio Rao 	rx_npkts = dc_rxeof(sc);
3242e4fc250cSLuigi Rizzo 	dc_txeof(sc);
324313f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
324413f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3245c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3246e4fc250cSLuigi Rizzo 
3247e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3248ee320f98SPyun YongHyeon 		uint32_t	status;
3249e4fc250cSLuigi Rizzo 
3250e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3251e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3252e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3253e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
32545120abbfSSam Leffler 		if (!status) {
32555120abbfSSam Leffler 			DC_UNLOCK(sc);
32561abcdbd1SAttilio Rao 			return (rx_npkts);
32575120abbfSSam Leffler 		}
3258e4fc250cSLuigi Rizzo 		/* ack what we have */
3259e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3260e4fc250cSLuigi Rizzo 
3261e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3262ee320f98SPyun YongHyeon 			uint32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3263e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3264e4fc250cSLuigi Rizzo 
3265e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3266e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3267e4fc250cSLuigi Rizzo 		}
3268e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3269e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3270e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3271e4fc250cSLuigi Rizzo 
3272e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3273e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3274e4fc250cSLuigi Rizzo 
3275e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
32766b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
32778f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3278c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3279e4fc250cSLuigi Rizzo 		}
3280e4fc250cSLuigi Rizzo 	}
32815120abbfSSam Leffler 	DC_UNLOCK(sc);
32821abcdbd1SAttilio Rao 	return (rx_npkts);
3283e4fc250cSLuigi Rizzo }
3284e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3285e4fc250cSLuigi Rizzo 
3286e3d2833aSAlfred Perlstein static void
32870934f18aSMaxime Henrion dc_intr(void *arg)
328896f2e892SBill Paul {
328996f2e892SBill Paul 	struct dc_softc *sc;
329096f2e892SBill Paul 	struct ifnet *ifp;
3291ee320f98SPyun YongHyeon 	uint32_t r, status;
3292848a02fcSPyun YongHyeon 	int n;
329396f2e892SBill Paul 
329496f2e892SBill Paul 	sc = arg;
3295d2a1864bSWarner Losh 
32960934f18aSMaxime Henrion 	if (sc->suspended)
3297e8388e14SMitsuru IWASAKI 		return;
3298e8388e14SMitsuru IWASAKI 
3299d1ce9105SBill Paul 	DC_LOCK(sc);
3300a84b4e80SPyun YongHyeon 	status = CSR_READ_4(sc, DC_ISR);
3301a84b4e80SPyun YongHyeon 	if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0) {
3302a84b4e80SPyun YongHyeon 		DC_UNLOCK(sc);
3303a84b4e80SPyun YongHyeon 		return;
3304a84b4e80SPyun YongHyeon 	}
3305fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3306e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
330740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
330840929967SGleb Smirnoff 		DC_UNLOCK(sc);
330940929967SGleb Smirnoff 		return;
3310e4fc250cSLuigi Rizzo 	}
33110934f18aSMaxime Henrion #endif
331296f2e892SBill Paul 	/* Disable interrupts. */
331396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
331496f2e892SBill Paul 
3315a84b4e80SPyun YongHyeon 	for (n = 16; n > 0; n--) {
3316a84b4e80SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3317a84b4e80SPyun YongHyeon 			break;
3318a84b4e80SPyun YongHyeon 		/* Ack interrupts. */
331996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
332096f2e892SBill Paul 
332173bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
3322848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
332373bf949cSBill Paul 				while (dc_rx_resync(sc))
332473bf949cSBill Paul 					dc_rxeof(sc);
332573bf949cSBill Paul 			}
332673bf949cSBill Paul 		}
332796f2e892SBill Paul 
332896f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
332996f2e892SBill Paul 			dc_txeof(sc);
333096f2e892SBill Paul 
333196f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
333296f2e892SBill Paul 			dc_txeof(sc);
333396f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
333496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
333596f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
333696f2e892SBill Paul 			}
333796f2e892SBill Paul 		}
333896f2e892SBill Paul 
3339d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3340d467c136SBill Paul 			dc_tx_underrun(sc);
334196f2e892SBill Paul 
334296f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
334373bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
334426b40a65SPyun YongHyeon 			r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
334526b40a65SPyun YongHyeon 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3346848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
334773bf949cSBill Paul 				while (dc_rx_resync(sc))
334873bf949cSBill Paul 					dc_rxeof(sc);
334973bf949cSBill Paul 			}
335073bf949cSBill Paul 		}
335196f2e892SBill Paul 
3352a84b4e80SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3353a84b4e80SPyun YongHyeon 			dc_start_locked(ifp);
3354a84b4e80SPyun YongHyeon 
335596f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
33568f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3357c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3358a84b4e80SPyun YongHyeon 			DC_UNLOCK(sc);
3359a84b4e80SPyun YongHyeon 			return;
336096f2e892SBill Paul 		}
3361a84b4e80SPyun YongHyeon 		status = CSR_READ_4(sc, DC_ISR);
3362a84b4e80SPyun YongHyeon 		if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0)
3363a84b4e80SPyun YongHyeon 			break;
336496f2e892SBill Paul 	}
336596f2e892SBill Paul 
336696f2e892SBill Paul 	/* Re-enable interrupts. */
3367a84b4e80SPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
336896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
336996f2e892SBill Paul 
3370d1ce9105SBill Paul 	DC_UNLOCK(sc);
337196f2e892SBill Paul }
337296f2e892SBill Paul 
337396f2e892SBill Paul /*
337496f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
337596f2e892SBill Paul  * pointers to the fragment pointers.
337696f2e892SBill Paul  */
3377e3d2833aSAlfred Perlstein static int
3378a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
337996f2e892SBill Paul {
3380ebc284ccSMarius Strobl 	bus_dma_segment_t segs[DC_MAXFRAGS];
33815f14ee23SPyun YongHyeon 	bus_dmamap_t map;
3382ebc284ccSMarius Strobl 	struct dc_desc *f;
338396f2e892SBill Paul 	struct mbuf *m;
3384993a741aSMarius Strobl 	int cur, defragged, error, first, frag, i, idx, nseg;
3385cda97c50SMike Silbersack 
3386993a741aSMarius Strobl 	m = NULL;
3387993a741aSMarius Strobl 	defragged = 0;
3388993a741aSMarius Strobl 	if (sc->dc_flags & DC_TX_COALESCE &&
3389993a741aSMarius Strobl 	    ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3390*c6499eccSGleb Smirnoff 		m = m_defrag(*m_head, M_NOWAIT);
3391993a741aSMarius Strobl 		defragged = 1;
3392993a741aSMarius Strobl 	} else {
3393cda97c50SMike Silbersack 		/*
3394993a741aSMarius Strobl 		 * Count the number of frags in this chain to see if we
3395993a741aSMarius Strobl 		 * need to m_collapse.  Since the descriptor list is shared
3396993a741aSMarius Strobl 		 * by all packets, we'll m_collapse long chains so that they
3397cda97c50SMike Silbersack 		 * do not use up the entire list, even if they would fit.
3398cda97c50SMike Silbersack 		 */
3399993a741aSMarius Strobl 		i = 0;
3400a10c0e45SMike Silbersack 		for (m = *m_head; m != NULL; m = m->m_next)
3401993a741aSMarius Strobl 			i++;
3402993a741aSMarius Strobl 		if (i > DC_TX_LIST_CNT / 4 ||
3403993a741aSMarius Strobl 		    DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3404993a741aSMarius Strobl 		    DC_TX_LIST_RSVD) {
3405*c6499eccSGleb Smirnoff 			m = m_collapse(*m_head, M_NOWAIT, DC_MAXFRAGS);
3406993a741aSMarius Strobl 			defragged = 1;
3407993a741aSMarius Strobl 		}
3408993a741aSMarius Strobl 	}
3409993a741aSMarius Strobl 	if (defragged != 0) {
341082a67a70SMarius Strobl 		if (m == NULL) {
341182a67a70SMarius Strobl 			m_freem(*m_head);
341282a67a70SMarius Strobl 			*m_head = NULL;
3413cda97c50SMike Silbersack 			return (ENOBUFS);
341482a67a70SMarius Strobl 		}
3415a10c0e45SMike Silbersack 		*m_head = m;
3416cda97c50SMike Silbersack 	}
3417993a741aSMarius Strobl 
341856e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
34195f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3420ebc284ccSMarius Strobl 	    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3421ebc284ccSMarius Strobl 	if (error == EFBIG) {
3422*c6499eccSGleb Smirnoff 		if (defragged != 0 || (m = m_collapse(*m_head, M_NOWAIT,
3423993a741aSMarius Strobl 		    DC_MAXFRAGS)) == NULL) {
3424ebc284ccSMarius Strobl 			m_freem(*m_head);
342582a67a70SMarius Strobl 			*m_head = NULL;
3426993a741aSMarius Strobl 			return (defragged != 0 ? error : ENOBUFS);
342782a67a70SMarius Strobl 		}
3428ebc284ccSMarius Strobl 		*m_head = m;
34295f14ee23SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3430ebc284ccSMarius Strobl 		    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3431ebc284ccSMarius Strobl 		if (error != 0) {
3432ebc284ccSMarius Strobl 			m_freem(*m_head);
3433ebc284ccSMarius Strobl 			*m_head = NULL;
3434ebc284ccSMarius Strobl 			return (error);
343582a67a70SMarius Strobl 		}
3436ebc284ccSMarius Strobl 	} else if (error != 0)
3437ebc284ccSMarius Strobl 		return (error);
3438ebc284ccSMarius Strobl 	KASSERT(nseg <= DC_MAXFRAGS,
3439ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
3440ebc284ccSMarius Strobl 	if (nseg == 0) {
3441ebc284ccSMarius Strobl 		m_freem(*m_head);
3442ebc284ccSMarius Strobl 		*m_head = NULL;
3443ebc284ccSMarius Strobl 		return (EIO);
3444ebc284ccSMarius Strobl 	}
3445ebc284ccSMarius Strobl 
34465f14ee23SPyun YongHyeon 	/* Check descriptor overruns. */
34475f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt + nseg > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
34485f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
34495f14ee23SPyun YongHyeon 		return (ENOBUFS);
34505f14ee23SPyun YongHyeon 	}
34515f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
34525f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
34535f14ee23SPyun YongHyeon 
3454ebc284ccSMarius Strobl 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3455ebc284ccSMarius Strobl 	for (i = 0; i < nseg; i++) {
3456ebc284ccSMarius Strobl 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3457ebc284ccSMarius Strobl 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3458ebc284ccSMarius Strobl 		    (first != sc->dc_cdata.dc_tx_first)) {
34595f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_mtag,
3460ebc284ccSMarius Strobl 			    sc->dc_cdata.dc_tx_map[first]);
3461ebc284ccSMarius Strobl 			m_freem(*m_head);
3462ebc284ccSMarius Strobl 			*m_head = NULL;
3463ebc284ccSMarius Strobl 			return (ENOBUFS);
3464ebc284ccSMarius Strobl 		}
3465ebc284ccSMarius Strobl 
34665f14ee23SPyun YongHyeon 		f = &sc->dc_ldata.dc_tx_list[frag];
3467ebc284ccSMarius Strobl 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3468ebc284ccSMarius Strobl 		if (i == 0) {
3469ebc284ccSMarius Strobl 			f->dc_status = 0;
3470ebc284ccSMarius Strobl 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3471ebc284ccSMarius Strobl 		} else
3472ebc284ccSMarius Strobl 			f->dc_status = htole32(DC_TXSTAT_OWN);
34735f14ee23SPyun YongHyeon 		f->dc_data = htole32(DC_ADDR_LO(segs[i].ds_addr));
3474ebc284ccSMarius Strobl 		cur = frag;
3475ebc284ccSMarius Strobl 		DC_INC(frag, DC_TX_LIST_CNT);
3476ebc284ccSMarius Strobl 	}
3477ebc284ccSMarius Strobl 
3478ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_prod = frag;
3479ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_cnt += nseg;
3480ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_chain[cur] = *m_head;
34815f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3482ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
34835f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[first].dc_ctl |=
3484ebc284ccSMarius Strobl 		    htole32(DC_TXCTL_FINT);
3485ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
34865f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
348706d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR &&
348806d23883SPyun YongHyeon 	    ++sc->dc_cdata.dc_tx_pkts >= 8) {
348906d23883SPyun YongHyeon 		sc->dc_cdata.dc_tx_pkts = 0;
34905f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
349106d23883SPyun YongHyeon 	}
34925f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3493ebc284ccSMarius Strobl 
34945f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
34955f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
34965f14ee23SPyun YongHyeon 
34975f14ee23SPyun YongHyeon 	/*
34985f14ee23SPyun YongHyeon 	 * Swap the last and the first dmamaps to ensure the map for
34995f14ee23SPyun YongHyeon 	 * this transmission is placed at the last descriptor.
35005f14ee23SPyun YongHyeon 	 */
35015f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_tx_map[cur];
35025f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[cur] = sc->dc_cdata.dc_tx_map[first];
35035f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[first] = map;
35045f14ee23SPyun YongHyeon 
350596f2e892SBill Paul 	return (0);
350696f2e892SBill Paul }
350796f2e892SBill Paul 
3508e3d2833aSAlfred Perlstein static void
35090934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
351096f2e892SBill Paul {
351196f2e892SBill Paul 	struct dc_softc *sc;
3512c8b27acaSJohn Baldwin 
3513c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3514c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3515c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3516c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3517c8b27acaSJohn Baldwin }
3518c8b27acaSJohn Baldwin 
3519ebc284ccSMarius Strobl /*
3520ebc284ccSMarius Strobl  * Main transmit routine
3521ebc284ccSMarius Strobl  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3522ebc284ccSMarius Strobl  * regions directly in the transmit lists.  We also save a copy of the
3523ebc284ccSMarius Strobl  * pointers since the transmit list fragment pointers are physical
3524ebc284ccSMarius Strobl  * addresses.
3525ebc284ccSMarius Strobl  */
3526c8b27acaSJohn Baldwin static void
3527c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3528c8b27acaSJohn Baldwin {
3529c8b27acaSJohn Baldwin 	struct dc_softc *sc;
35305f14ee23SPyun YongHyeon 	struct mbuf *m_head;
35315f14ee23SPyun YongHyeon 	int queued;
353296f2e892SBill Paul 
353396f2e892SBill Paul 	sc = ifp->if_softc;
353496f2e892SBill Paul 
3535c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
353696f2e892SBill Paul 
353776d40c85SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
353876d40c85SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->dc_link == 0)
3539d1ce9105SBill Paul 		return;
354096f2e892SBill Paul 
35415f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
354296f2e892SBill Paul 
35435f14ee23SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
35445f14ee23SPyun YongHyeon 		/*
35455f14ee23SPyun YongHyeon 		 * If there's no way we can send any packets, return now.
35465f14ee23SPyun YongHyeon 		 */
35475f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
35485f14ee23SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
35495f14ee23SPyun YongHyeon 			break;
35505f14ee23SPyun YongHyeon 		}
3551cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
355296f2e892SBill Paul 		if (m_head == NULL)
355396f2e892SBill Paul 			break;
355496f2e892SBill Paul 
3555a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
355682a67a70SMarius Strobl 			if (m_head == NULL)
355782a67a70SMarius Strobl 				break;
3558cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
355913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
356096f2e892SBill Paul 			break;
356196f2e892SBill Paul 		}
356296f2e892SBill Paul 
3563cbaf877fSBrian Feldman 		queued++;
356496f2e892SBill Paul 		/*
356596f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
356696f2e892SBill Paul 		 * to him.
356796f2e892SBill Paul 		 */
35689ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
356996f2e892SBill Paul 	}
357096f2e892SBill Paul 
3571cbaf877fSBrian Feldman 	if (queued > 0) {
357296f2e892SBill Paul 		/* Transmit */
357396f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
357496f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
357596f2e892SBill Paul 
357696f2e892SBill Paul 		/*
357796f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
357896f2e892SBill Paul 		 */
3579b1d16143SMarius Strobl 		sc->dc_wdog_timer = 5;
3580cbaf877fSBrian Feldman 	}
358196f2e892SBill Paul }
358296f2e892SBill Paul 
3583e3d2833aSAlfred Perlstein static void
35840934f18aSMaxime Henrion dc_init(void *xsc)
358596f2e892SBill Paul {
358696f2e892SBill Paul 	struct dc_softc *sc = xsc;
3587c8b27acaSJohn Baldwin 
3588c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3589c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3590c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3591c8b27acaSJohn Baldwin }
3592c8b27acaSJohn Baldwin 
3593c8b27acaSJohn Baldwin static void
3594c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3595c8b27acaSJohn Baldwin {
3596fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
359796f2e892SBill Paul 	struct mii_data *mii;
3598d314ebf5SPyun YongHyeon 	struct ifmedia *ifm;
359996f2e892SBill Paul 
3600c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
360196f2e892SBill Paul 
36028f382a1fSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
36038f382a1fSPyun YongHyeon 		return;
36048f382a1fSPyun YongHyeon 
360596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
360696f2e892SBill Paul 
360796f2e892SBill Paul 	/*
360896f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
360996f2e892SBill Paul 	 */
361096f2e892SBill Paul 	dc_stop(sc);
361196f2e892SBill Paul 	dc_reset(sc);
3612d314ebf5SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
3613d314ebf5SPyun YongHyeon 		ifm = &mii->mii_media;
3614d314ebf5SPyun YongHyeon 		dc_apply_fixup(sc, ifm->ifm_media);
3615d314ebf5SPyun YongHyeon 	}
361696f2e892SBill Paul 
361796f2e892SBill Paul 	/*
361896f2e892SBill Paul 	 * Set cache alignment and burst length.
361996f2e892SBill Paul 	 */
362052ca7ee2SPyun YongHyeon 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc) || DC_IS_ULI(sc))
362196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
362296f2e892SBill Paul 	else
362396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3624935fe010SLuigi Rizzo 	/*
3625935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3626935fe010SLuigi Rizzo 	 */
3627935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3628935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
362996f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
363096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
363196f2e892SBill Paul 	} else {
363296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
363396f2e892SBill Paul 	}
363496f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
363596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
363696f2e892SBill Paul 	switch(sc->dc_cachesize) {
363796f2e892SBill Paul 	case 32:
363896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
363996f2e892SBill Paul 		break;
364096f2e892SBill Paul 	case 16:
364196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
364296f2e892SBill Paul 		break;
364396f2e892SBill Paul 	case 8:
364496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
364596f2e892SBill Paul 		break;
364696f2e892SBill Paul 	case 0:
364796f2e892SBill Paul 	default:
364896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
364996f2e892SBill Paul 		break;
365096f2e892SBill Paul 	}
365196f2e892SBill Paul 
365296f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
365396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365496f2e892SBill Paul 	else {
3655d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
365696f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365796f2e892SBill Paul 		} else {
365896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
365996f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
366096f2e892SBill Paul 		}
366196f2e892SBill Paul 	}
366296f2e892SBill Paul 
366396f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
366496f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
366596f2e892SBill Paul 
366696f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
366796f2e892SBill Paul 		/*
366896f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
366996f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
367096f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
367196f2e892SBill Paul 		 * document the meaning of these bits so there's no way
367296f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
367396f2e892SBill Paul 		 * number all its own; the rest all use a different one.
367496f2e892SBill Paul 		 */
367596f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
367696f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
367796f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
367896f2e892SBill Paul 		else
367996f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
368096f2e892SBill Paul 	}
368196f2e892SBill Paul 
3682feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3683feb78939SJonathan Chen 		/*
3684feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3685feb78939SJonathan Chen 		 * can talk to the MII.
3686feb78939SJonathan Chen 		 */
3687feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3688feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3689feb78939SJonathan Chen 		DELAY(10);
3690feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3691feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3692feb78939SJonathan Chen 		DELAY(10);
3693feb78939SJonathan Chen 	}
3694feb78939SJonathan Chen 
369596f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3696d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
369796f2e892SBill Paul 
369896f2e892SBill Paul 	/* Init circular RX list. */
369996f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
37006b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
370122f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
370296f2e892SBill Paul 		dc_stop(sc);
370396f2e892SBill Paul 		return;
370496f2e892SBill Paul 	}
370596f2e892SBill Paul 
370696f2e892SBill Paul 	/*
370756e5e7aeSMaxime Henrion 	 * Init TX descriptors.
370896f2e892SBill Paul 	 */
370996f2e892SBill Paul 	dc_list_tx_init(sc);
371096f2e892SBill Paul 
371196f2e892SBill Paul 	/*
371296f2e892SBill Paul 	 * Load the address of the RX list.
371396f2e892SBill Paul 	 */
371456e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
371556e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
371696f2e892SBill Paul 
371796f2e892SBill Paul 	/*
371896f2e892SBill Paul 	 * Enable interrupts.
371996f2e892SBill Paul 	 */
3720e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3721e4fc250cSLuigi Rizzo 	/*
3722e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3723e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3724e4fc250cSLuigi Rizzo 	 * after a reset.
3725e4fc250cSLuigi Rizzo 	 */
372640929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3727e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3728e4fc250cSLuigi Rizzo 	else
3729e4fc250cSLuigi Rizzo #endif
373096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
373196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
373296f2e892SBill Paul 
373352ca7ee2SPyun YongHyeon 	/* Initialize TX jabber and RX watchdog timer. */
373452ca7ee2SPyun YongHyeon 	if (DC_IS_ULI(sc))
373552ca7ee2SPyun YongHyeon 		CSR_WRITE_4(sc, DC_WATCHDOG, DC_WDOG_JABBERCLK |
373652ca7ee2SPyun YongHyeon 		    DC_WDOG_HOSTUNJAB);
373752ca7ee2SPyun YongHyeon 
373896f2e892SBill Paul 	/* Enable transmitter. */
373996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
374096f2e892SBill Paul 
374196f2e892SBill Paul 	/*
3742918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3743918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3744918434c8SBill Paul 	 * link and activity indications.
3745918434c8SBill Paul 	 */
374678999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3747918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3748918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
374978999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3750918434c8SBill Paul 	}
3751918434c8SBill Paul 
3752918434c8SBill Paul 	/*
375396f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
375496f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
375596f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
375696f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
375796f2e892SBill Paul 	 */
375896f2e892SBill Paul 	dc_setfilt(sc);
375996f2e892SBill Paul 
376096f2e892SBill Paul 	/* Enable receiver. */
376196f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
376296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
376396f2e892SBill Paul 
376413f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
376513f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
376696f2e892SBill Paul 
3767d7e9ac75SPyun YongHyeon 	dc_ifmedia_upd_locked(sc);
3768d314ebf5SPyun YongHyeon 
376926b40a65SPyun YongHyeon 	/* Clear missed frames and overflow counter. */
377026b40a65SPyun YongHyeon 	CSR_READ_4(sc, DC_FRAMESDISCARDED);
377126b40a65SPyun YongHyeon 
3772857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
377345521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3774857fd445SBill Paul 		sc->dc_link = 1;
3775857fd445SBill Paul 	else {
3776318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3777b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3778318b02fdSBill Paul 		else
3779b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3780857fd445SBill Paul 	}
3781b1d16143SMarius Strobl 
3782b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
3783b1d16143SMarius Strobl 	callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
378496f2e892SBill Paul }
378596f2e892SBill Paul 
378696f2e892SBill Paul /*
378796f2e892SBill Paul  * Set media options.
378896f2e892SBill Paul  */
3789e3d2833aSAlfred Perlstein static int
37900934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
379196f2e892SBill Paul {
379296f2e892SBill Paul 	struct dc_softc *sc;
3793d7e9ac75SPyun YongHyeon 	int error;
379496f2e892SBill Paul 
379596f2e892SBill Paul 	sc = ifp->if_softc;
3796c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3797d7e9ac75SPyun YongHyeon 	error = dc_ifmedia_upd_locked(sc);
3798d7e9ac75SPyun YongHyeon 	DC_UNLOCK(sc);
3799d7e9ac75SPyun YongHyeon 	return (error);
3800d7e9ac75SPyun YongHyeon }
3801f43d9309SBill Paul 
3802d7e9ac75SPyun YongHyeon static int
3803d7e9ac75SPyun YongHyeon dc_ifmedia_upd_locked(struct dc_softc *sc)
3804d7e9ac75SPyun YongHyeon {
3805d7e9ac75SPyun YongHyeon 	struct mii_data *mii;
3806d7e9ac75SPyun YongHyeon 	struct ifmedia *ifm;
3807d7e9ac75SPyun YongHyeon 	int error;
3808d7e9ac75SPyun YongHyeon 
3809d7e9ac75SPyun YongHyeon 	DC_LOCK_ASSERT(sc);
3810d7e9ac75SPyun YongHyeon 
3811d7e9ac75SPyun YongHyeon 	sc->dc_link = 0;
3812d7e9ac75SPyun YongHyeon 	mii = device_get_softc(sc->dc_miibus);
3813d7e9ac75SPyun YongHyeon 	error = mii_mediachg(mii);
3814d7e9ac75SPyun YongHyeon 	if (error == 0) {
3815d7e9ac75SPyun YongHyeon 		ifm = &mii->mii_media;
3816d314ebf5SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3817d314ebf5SPyun YongHyeon 			dc_setcfg(sc, ifm->ifm_media);
3818d314ebf5SPyun YongHyeon 		else if (DC_IS_DAVICOM(sc) &&
381945521525SPoul-Henning Kamp 		    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3820f43d9309SBill Paul 			dc_setcfg(sc, ifm->ifm_media);
3821d7e9ac75SPyun YongHyeon 	}
382296f2e892SBill Paul 
3823d7e9ac75SPyun YongHyeon 	return (error);
382496f2e892SBill Paul }
382596f2e892SBill Paul 
382696f2e892SBill Paul /*
382796f2e892SBill Paul  * Report current media status.
382896f2e892SBill Paul  */
3829e3d2833aSAlfred Perlstein static void
38300934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
383196f2e892SBill Paul {
383296f2e892SBill Paul 	struct dc_softc *sc;
383396f2e892SBill Paul 	struct mii_data *mii;
3834f43d9309SBill Paul 	struct ifmedia *ifm;
383596f2e892SBill Paul 
383696f2e892SBill Paul 	sc = ifp->if_softc;
383796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3838c8b27acaSJohn Baldwin 	DC_LOCK(sc);
383996f2e892SBill Paul 	mii_pollstat(mii);
3840f43d9309SBill Paul 	ifm = &mii->mii_media;
3841f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
384245521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3843f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3844f43d9309SBill Paul 			ifmr->ifm_status = 0;
3845432120f2SMarius Strobl 			DC_UNLOCK(sc);
3846f43d9309SBill Paul 			return;
3847f43d9309SBill Paul 		}
3848f43d9309SBill Paul 	}
384996f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
385096f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3851c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
385296f2e892SBill Paul }
385396f2e892SBill Paul 
3854e3d2833aSAlfred Perlstein static int
38550934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
385696f2e892SBill Paul {
385796f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
385896f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
385996f2e892SBill Paul 	struct mii_data *mii;
3860d1ce9105SBill Paul 	int error = 0;
386196f2e892SBill Paul 
386296f2e892SBill Paul 	switch (command) {
386396f2e892SBill Paul 	case SIOCSIFFLAGS:
3864c8b27acaSJohn Baldwin 		DC_LOCK(sc);
386596f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
38665d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
38675d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
38685d6dfbbbSLuigi Rizzo 
386913f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
38705d6dfbbbSLuigi Rizzo 				if (need_setfilt)
387196f2e892SBill Paul 					dc_setfilt(sc);
38725d6dfbbbSLuigi Rizzo 			} else {
38738f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3874c8b27acaSJohn Baldwin 				dc_init_locked(sc);
387596f2e892SBill Paul 			}
387696f2e892SBill Paul 		} else {
387713f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
387896f2e892SBill Paul 				dc_stop(sc);
387996f2e892SBill Paul 		}
388096f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3881c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
388296f2e892SBill Paul 		break;
388396f2e892SBill Paul 	case SIOCADDMULTI:
388496f2e892SBill Paul 	case SIOCDELMULTI:
3885c8b27acaSJohn Baldwin 		DC_LOCK(sc);
388624507bc1SPyun YongHyeon 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
388796f2e892SBill Paul 			dc_setfilt(sc);
3888c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
388996f2e892SBill Paul 		break;
389096f2e892SBill Paul 	case SIOCGIFMEDIA:
389196f2e892SBill Paul 	case SIOCSIFMEDIA:
389296f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
389396f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
389496f2e892SBill Paul 		break;
3895e695984eSRuslan Ermilov 	case SIOCSIFCAP:
389640929967SGleb Smirnoff #ifdef DEVICE_POLLING
389740929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
389840929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
389940929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
390040929967SGleb Smirnoff 			if (error)
390140929967SGleb Smirnoff 				return(error);
3902c8b27acaSJohn Baldwin 			DC_LOCK(sc);
390340929967SGleb Smirnoff 			/* Disable interrupts */
390440929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
390540929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3906c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
390740929967SGleb Smirnoff 			return (error);
390840929967SGleb Smirnoff 		}
390940929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
391040929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
391140929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
391240929967SGleb Smirnoff 			/* Enable interrupts. */
391340929967SGleb Smirnoff 			DC_LOCK(sc);
391440929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
391540929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
391640929967SGleb Smirnoff 			DC_UNLOCK(sc);
391740929967SGleb Smirnoff 			return (error);
391840929967SGleb Smirnoff 		}
391940929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3920e695984eSRuslan Ermilov 		break;
392196f2e892SBill Paul 	default:
39229ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
392396f2e892SBill Paul 		break;
392496f2e892SBill Paul 	}
392596f2e892SBill Paul 
392696f2e892SBill Paul 	return (error);
392796f2e892SBill Paul }
392896f2e892SBill Paul 
3929e3d2833aSAlfred Perlstein static void
3930b1d16143SMarius Strobl dc_watchdog(void *xsc)
393196f2e892SBill Paul {
3932b1d16143SMarius Strobl 	struct dc_softc *sc = xsc;
3933b1d16143SMarius Strobl 	struct ifnet *ifp;
393496f2e892SBill Paul 
3935b1d16143SMarius Strobl 	DC_LOCK_ASSERT(sc);
393696f2e892SBill Paul 
3937b1d16143SMarius Strobl 	if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3938b1d16143SMarius Strobl 		callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3939b1d16143SMarius Strobl 		return;
3940b1d16143SMarius Strobl 	}
3941d1ce9105SBill Paul 
3942b1d16143SMarius Strobl 	ifp = sc->dc_ifp;
394396f2e892SBill Paul 	ifp->if_oerrors++;
3944b1d16143SMarius Strobl 	device_printf(sc->dc_dev, "watchdog timeout\n");
394596f2e892SBill Paul 
39468f382a1fSPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3947c8b27acaSJohn Baldwin 	dc_init_locked(sc);
394896f2e892SBill Paul 
3949cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3950c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
395196f2e892SBill Paul }
395296f2e892SBill Paul 
395396f2e892SBill Paul /*
395496f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
395596f2e892SBill Paul  * RX and TX lists.
395696f2e892SBill Paul  */
3957e3d2833aSAlfred Perlstein static void
39580934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
395996f2e892SBill Paul {
396096f2e892SBill Paul 	struct ifnet *ifp;
3961b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3962b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3963b3811c95SMaxime Henrion 	int i;
39641da7683aSPyun YongHyeon 	uint32_t ctl, netcfg;
396596f2e892SBill Paul 
3966c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3967d1ce9105SBill Paul 
3968fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
39695f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
3970b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
397196f2e892SBill Paul 
3972b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
3973b1d16143SMarius Strobl 	callout_stop(&sc->dc_wdog_ch);
3974b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
39751da7683aSPyun YongHyeon 	sc->dc_link = 0;
397696f2e892SBill Paul 
397713f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
39783b3ec200SPeter Wemm 
39791da7683aSPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
39801da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39811da7683aSPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
39821da7683aSPyun YongHyeon 		   netcfg & ~(DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
398396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
39841da7683aSPyun YongHyeon 	/* Wait the completion of TX/RX SM. */
39851da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39861da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
39871da7683aSPyun YongHyeon 
398896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
398996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
399096f2e892SBill Paul 
399196f2e892SBill Paul 	/*
399296f2e892SBill Paul 	 * Free data in the RX lists.
399396f2e892SBill Paul 	 */
399496f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3995b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
39965f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_rx_mtag,
39975f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i], BUS_DMASYNC_POSTREAD);
39985f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_mtag,
39995f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i]);
400056e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
400156e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
400296f2e892SBill Paul 		}
400396f2e892SBill Paul 	}
40045f14ee23SPyun YongHyeon 	bzero(ld->dc_rx_list, DC_RX_LIST_SZ);
40055f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
40065f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
400796f2e892SBill Paul 
400896f2e892SBill Paul 	/*
400996f2e892SBill Paul 	 * Free the TX list buffers.
401096f2e892SBill Paul 	 */
401196f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
4012b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
4013af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
40145f14ee23SPyun YongHyeon 			if (ctl & DC_TXCTL_SETUP) {
40155f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
40165f14ee23SPyun YongHyeon 				    BUS_DMASYNC_POSTWRITE);
40175f14ee23SPyun YongHyeon 			} else {
40185f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_tx_mtag,
40195f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i], BUS_DMASYNC_POSTWRITE);
40205f14ee23SPyun YongHyeon 				bus_dmamap_unload(sc->dc_tx_mtag,
40215f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i]);
402256e5e7aeSMaxime Henrion 				m_freem(cd->dc_tx_chain[i]);
40235f14ee23SPyun YongHyeon 			}
4024b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
402596f2e892SBill Paul 		}
402696f2e892SBill Paul 	}
40275f14ee23SPyun YongHyeon 	bzero(ld->dc_tx_list, DC_TX_LIST_SZ);
40285f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
40295f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
403096f2e892SBill Paul }
403196f2e892SBill Paul 
403296f2e892SBill Paul /*
4033e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
4034e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
4035e8388e14SMitsuru IWASAKI  * resume.
4036e8388e14SMitsuru IWASAKI  */
4037e3d2833aSAlfred Perlstein static int
40380934f18aSMaxime Henrion dc_suspend(device_t dev)
4039e8388e14SMitsuru IWASAKI {
4040e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4041e8388e14SMitsuru IWASAKI 
4042e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4043c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4044e8388e14SMitsuru IWASAKI 	dc_stop(sc);
4045e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
4046c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4047e8388e14SMitsuru IWASAKI 
4048e8388e14SMitsuru IWASAKI 	return (0);
4049e8388e14SMitsuru IWASAKI }
4050e8388e14SMitsuru IWASAKI 
4051e8388e14SMitsuru IWASAKI /*
4052e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
4053e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
4054e8388e14SMitsuru IWASAKI  * appropriate.
4055e8388e14SMitsuru IWASAKI  */
4056e3d2833aSAlfred Perlstein static int
40570934f18aSMaxime Henrion dc_resume(device_t dev)
4058e8388e14SMitsuru IWASAKI {
4059e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4060e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
4061e8388e14SMitsuru IWASAKI 
4062e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4063fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
4064e8388e14SMitsuru IWASAKI 
4065e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
4066c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4067e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
4068c8b27acaSJohn Baldwin 		dc_init_locked(sc);
4069e8388e14SMitsuru IWASAKI 
4070e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
4071c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4072e8388e14SMitsuru IWASAKI 
4073e8388e14SMitsuru IWASAKI 	return (0);
4074e8388e14SMitsuru IWASAKI }
4075e8388e14SMitsuru IWASAKI 
4076e8388e14SMitsuru IWASAKI /*
407796f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
407896f2e892SBill Paul  * get confused by errant DMAs when rebooting.
407996f2e892SBill Paul  */
40806a087a87SPyun YongHyeon static int
40810934f18aSMaxime Henrion dc_shutdown(device_t dev)
408296f2e892SBill Paul {
408396f2e892SBill Paul 	struct dc_softc *sc;
408496f2e892SBill Paul 
408596f2e892SBill Paul 	sc = device_get_softc(dev);
408696f2e892SBill Paul 
4087c8b27acaSJohn Baldwin 	DC_LOCK(sc);
408896f2e892SBill Paul 	dc_stop(sc);
4089c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
40906a087a87SPyun YongHyeon 
40916a087a87SPyun YongHyeon 	return (0);
409296f2e892SBill Paul }
409339d76ed6SPyun YongHyeon 
409439d76ed6SPyun YongHyeon static int
409539d76ed6SPyun YongHyeon dc_check_multiport(struct dc_softc *sc)
409639d76ed6SPyun YongHyeon {
409739d76ed6SPyun YongHyeon 	struct dc_softc *dsc;
409839d76ed6SPyun YongHyeon 	devclass_t dc;
409939d76ed6SPyun YongHyeon 	device_t child;
410039d76ed6SPyun YongHyeon 	uint8_t *eaddr;
410139d76ed6SPyun YongHyeon 	int unit;
410239d76ed6SPyun YongHyeon 
410339d76ed6SPyun YongHyeon 	dc = devclass_find("dc");
410439d76ed6SPyun YongHyeon 	for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
410539d76ed6SPyun YongHyeon 		child = devclass_get_device(dc, unit);
410639d76ed6SPyun YongHyeon 		if (child == NULL)
410739d76ed6SPyun YongHyeon 			continue;
410839d76ed6SPyun YongHyeon 		if (child == sc->dc_dev)
410939d76ed6SPyun YongHyeon 			continue;
411039d76ed6SPyun YongHyeon 		if (device_get_parent(child) != device_get_parent(sc->dc_dev))
411139d76ed6SPyun YongHyeon 			continue;
411239d76ed6SPyun YongHyeon 		if (unit > device_get_unit(sc->dc_dev))
411339d76ed6SPyun YongHyeon 			continue;
4114b289c607SPyun YongHyeon 		if (device_is_attached(child) == 0)
4115b289c607SPyun YongHyeon 			continue;
411639d76ed6SPyun YongHyeon 		dsc = device_get_softc(child);
4117b289c607SPyun YongHyeon 		device_printf(sc->dc_dev,
4118b289c607SPyun YongHyeon 		    "Using station address of %s as base\n",
411939d76ed6SPyun YongHyeon 		    device_get_nameunit(child));
412039d76ed6SPyun YongHyeon 		bcopy(dsc->dc_eaddr, sc->dc_eaddr, ETHER_ADDR_LEN);
412139d76ed6SPyun YongHyeon 		eaddr = (uint8_t *)sc->dc_eaddr;
412239d76ed6SPyun YongHyeon 		eaddr[5]++;
4123b289c607SPyun YongHyeon 		/* Prepare SROM to parse again. */
4124b289c607SPyun YongHyeon 		if (DC_IS_INTEL(sc) && dsc->dc_srom != NULL &&
4125b289c607SPyun YongHyeon 		    sc->dc_romwidth != 0) {
4126b289c607SPyun YongHyeon 			free(sc->dc_srom, M_DEVBUF);
4127b289c607SPyun YongHyeon 			sc->dc_romwidth = dsc->dc_romwidth;
4128b289c607SPyun YongHyeon 			sc->dc_srom = malloc(DC_ROM_SIZE(sc->dc_romwidth),
4129b289c607SPyun YongHyeon 			    M_DEVBUF, M_NOWAIT);
4130b289c607SPyun YongHyeon 			if (sc->dc_srom == NULL) {
4131b289c607SPyun YongHyeon 				device_printf(sc->dc_dev,
4132b289c607SPyun YongHyeon 				    "Could not allocate SROM buffer\n");
4133b289c607SPyun YongHyeon 				return (ENOMEM);
4134b289c607SPyun YongHyeon 			}
4135b289c607SPyun YongHyeon 			bcopy(dsc->dc_srom, sc->dc_srom,
4136b289c607SPyun YongHyeon 			    DC_ROM_SIZE(sc->dc_romwidth));
4137b289c607SPyun YongHyeon 		}
413839d76ed6SPyun YongHyeon 		return (0);
413939d76ed6SPyun YongHyeon 	}
414039d76ed6SPyun YongHyeon 	return (ENOENT);
414139d76ed6SPyun YongHyeon }
4142