xref: /freebsd/sys/dev/dc/if_dc.c (revision f0796cd26c51af84618836d41f9e37a310323c6e)
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)
4696f2e892SBill Paul  * ADMtek AN985 (www.admtek.com.tw)
474c16d09eSWarner Losh  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek AN985
4888d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
499ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
50feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
511d5e5310SBill Paul  * Abocom FE2500
521af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
537eac366bSMartin Blapp  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
5496f2e892SBill Paul  *
5596f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5696f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5796f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5896f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
5996f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
6096f2e892SBill Paul  * instead of 512.
6196f2e892SBill Paul  *
6296f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
6396f2e892SBill Paul  * Electrical Engineering Department
6496f2e892SBill Paul  * Columbia University, New York City
6596f2e892SBill Paul  */
6696f2e892SBill Paul /*
6796f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6896f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
6996f2e892SBill Paul  * three kinds of media attachments:
7096f2e892SBill Paul  *
7196f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
7296f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7396f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7496f2e892SBill Paul  * o 10baseT port.
7596f2e892SBill Paul  * o AUI/BNC port.
7696f2e892SBill Paul  *
7796f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7896f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
7996f2e892SBill Paul  * autosensing configuration.
8096f2e892SBill Paul  *
8196f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
8296f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8396f2e892SBill Paul  * handled separately due to its different register offsets and the
8496f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8596f2e892SBill Paul  * here, but I'm not thrilled about it.
8696f2e892SBill Paul  *
8796f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8896f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
8996f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
9096f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
9196f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
9296f2e892SBill Paul  */
9396f2e892SBill Paul 
94f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
95f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
96f0796cd2SGleb Smirnoff #endif
97f0796cd2SGleb Smirnoff 
9896f2e892SBill Paul #include <sys/param.h>
99af4358c7SMaxime Henrion #include <sys/endian.h>
10096f2e892SBill Paul #include <sys/systm.h>
10196f2e892SBill Paul #include <sys/sockio.h>
10296f2e892SBill Paul #include <sys/mbuf.h>
10396f2e892SBill Paul #include <sys/malloc.h>
10496f2e892SBill Paul #include <sys/kernel.h>
105f11d01c3SPoul-Henning Kamp #include <sys/module.h>
10696f2e892SBill Paul #include <sys/socket.h>
10701faf54bSLuigi Rizzo #include <sys/sysctl.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>
12596f2e892SBill Paul #include <dev/mii/miivar.h>
12696f2e892SBill Paul 
12719b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
12819b7ffd1SWarner Losh #include <dev/pci/pcivar.h>
12996f2e892SBill Paul 
13096f2e892SBill Paul #define DC_USEIOSPACE
1315c1cfac4SBill Paul #ifdef __alpha__
1325c1cfac4SBill Paul #define SRM_MEDIA
1335c1cfac4SBill Paul #endif
13496f2e892SBill Paul 
13596f2e892SBill Paul #include <pci/if_dcreg.h>
13696f2e892SBill Paul 
137ec6a7299SMaxime Henrion #ifdef __sparc64__
138ec6a7299SMaxime Henrion #include <dev/ofw/openfirm.h>
139ec6a7299SMaxime Henrion #include <machine/ofw_machdep.h>
140ec6a7299SMaxime Henrion #endif
141ec6a7299SMaxime Henrion 
142f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, pci, 1, 1, 1);
143f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, ether, 1, 1, 1);
14495a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
14595a16455SPeter Wemm 
146919ccba7SWarner Losh /*
147919ccba7SWarner Losh  * "device miibus" is required in kernel config.  See GENERIC if you get
148919ccba7SWarner Losh  * errors here.
149919ccba7SWarner Losh  */
15096f2e892SBill Paul #include "miibus_if.h"
15196f2e892SBill Paul 
15296f2e892SBill Paul /*
15396f2e892SBill Paul  * Various supported device vendors/types and their names.
15496f2e892SBill Paul  */
15596f2e892SBill Paul static struct dc_type dc_devs[] = {
15696f2e892SBill Paul 	{ DC_VENDORID_DEC, DC_DEVICEID_21143,
15796f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
15838deb45fSTom Rhodes 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009,
15938deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
16096f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100,
16196f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
16296f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
16396f2e892SBill Paul 		"Davicom DM9102 10/100BaseTX" },
16488d739dcSBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
16588d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
16696f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AL981,
16796f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
16896f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AN985,
16996f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
170e351d778SMartin Blapp 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511,
171e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
172e351d778SMartin Blapp 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513,
173e351d778SMartin Blapp 		"ADMtek ADM9513 10/100BaseTX" },
1744c16d09eSWarner Losh 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_FA511,
1754c16d09eSWarner Losh 		"Netgear FA511 10/100BaseTX" },
17696f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
17796f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
17896f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
17996f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
18096f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
18196f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
18296f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
18396f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
18496f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
18596f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
18696f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
18796f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
18896f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
18996f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
19096f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
19179d11e09SBill Paul 		"Macronix 98715AEC-C 10/100BaseTX" },
19279d11e09SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
19396f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
194ead7cde9SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98727,
195ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
19696f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C115,
19796f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
19896f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
19996f2e892SBill Paul 		"82c168 PNIC 10/100BaseTX" },
20096f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
20196f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
2029ca710f6SJeroen Ruigrok van der Werven 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN1217,
2039ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
204fa167b8eSBill Paul 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN2242,
205fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
206feb78939SJonathan Chen 	{ DC_VENDORID_XIRCOM, DC_DEVICEID_X3201,
207feb78939SJonathan Chen 	  	"Xircom X3201 10/100BaseTX" },
2081d5e5310SBill Paul 	{ DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500,
2091d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
210773c505fSMIHIRA Sanpei Yoshiro 	{ DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX,
211773c505fSMIHIRA Sanpei Yoshiro 		"Abocom FE2500MX 10/100BaseTX" },
2121af8bec7SBill Paul 	{ DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112,
2131af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
214948c244dSWarner Losh 	{ DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX,
215948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
21697f91728SMIHIRA Sanpei Yoshiro 	{ DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T,
21797f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2187eac366bSMartin Blapp 	{ DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB,
2197eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
220e7b9ab3aSBill Paul 	{ DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120,
221e7b9ab3aSBill Paul 		"Microsoft MN-120 CardBus 10/100" },
222e7b9ab3aSBill Paul 	{ DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130,
223e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
224e7b9ab3aSBill Paul 	{ DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130_FAKE,
225e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
22696f2e892SBill Paul 	{ 0, 0, NULL }
22796f2e892SBill Paul };
22896f2e892SBill Paul 
229e51a25f8SAlfred Perlstein static int dc_probe(device_t);
230e51a25f8SAlfred Perlstein static int dc_attach(device_t);
231e51a25f8SAlfred Perlstein static int dc_detach(device_t);
232e8388e14SMitsuru IWASAKI static int dc_suspend(device_t);
233e8388e14SMitsuru IWASAKI static int dc_resume(device_t);
234e51a25f8SAlfred Perlstein static struct dc_type *dc_devtype(device_t);
23556e5e7aeSMaxime Henrion static int dc_newbuf(struct dc_softc *, int, int);
236a10c0e45SMike Silbersack static int dc_encap(struct dc_softc *, struct mbuf **);
237e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war(struct dc_softc *, int);
238e51a25f8SAlfred Perlstein static int dc_rx_resync(struct dc_softc *);
239e51a25f8SAlfred Perlstein static void dc_rxeof(struct dc_softc *);
240e51a25f8SAlfred Perlstein static void dc_txeof(struct dc_softc *);
241e51a25f8SAlfred Perlstein static void dc_tick(void *);
242e51a25f8SAlfred Perlstein static void dc_tx_underrun(struct dc_softc *);
243e51a25f8SAlfred Perlstein static void dc_intr(void *);
244e51a25f8SAlfred Perlstein static void dc_start(struct ifnet *);
245c8b27acaSJohn Baldwin static void dc_start_locked(struct ifnet *);
246e51a25f8SAlfred Perlstein static int dc_ioctl(struct ifnet *, u_long, caddr_t);
247e51a25f8SAlfred Perlstein static void dc_init(void *);
248c8b27acaSJohn Baldwin static void dc_init_locked(struct dc_softc *);
249e51a25f8SAlfred Perlstein static void dc_stop(struct dc_softc *);
250e51a25f8SAlfred Perlstein static void dc_watchdog(struct ifnet *);
251e51a25f8SAlfred Perlstein static void dc_shutdown(device_t);
252e51a25f8SAlfred Perlstein static int dc_ifmedia_upd(struct ifnet *);
253e51a25f8SAlfred Perlstein static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
25496f2e892SBill Paul 
255e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
256e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
257e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
258e51a25f8SAlfred Perlstein static void dc_eeprom_getword(struct dc_softc *, int, u_int16_t *);
259d24ae19dSWarner Losh static void dc_eeprom_getword_pnic(struct dc_softc *, int, u_int16_t *);
260d24ae19dSWarner Losh static void dc_eeprom_getword_xircom(struct dc_softc *, int, u_int16_t *);
2613097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
262e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
26396f2e892SBill Paul 
264e51a25f8SAlfred Perlstein static void dc_mii_writebit(struct dc_softc *, int);
265e51a25f8SAlfred Perlstein static int dc_mii_readbit(struct dc_softc *);
266e51a25f8SAlfred Perlstein static void dc_mii_sync(struct dc_softc *);
267e51a25f8SAlfred Perlstein static void dc_mii_send(struct dc_softc *, u_int32_t, int);
268e51a25f8SAlfred Perlstein static int dc_mii_readreg(struct dc_softc *, struct dc_mii_frame *);
269e51a25f8SAlfred Perlstein static int dc_mii_writereg(struct dc_softc *, struct dc_mii_frame *);
270e51a25f8SAlfred Perlstein static int dc_miibus_readreg(device_t, int, int);
271e51a25f8SAlfred Perlstein static int dc_miibus_writereg(device_t, int, int, int);
272e51a25f8SAlfred Perlstein static void dc_miibus_statchg(device_t);
273e51a25f8SAlfred Perlstein static void dc_miibus_mediainit(device_t);
27496f2e892SBill Paul 
275e51a25f8SAlfred Perlstein static void dc_setcfg(struct dc_softc *, int);
2763373489bSWarner Losh static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
2773373489bSWarner Losh static uint32_t dc_mchash_be(const uint8_t *);
278e51a25f8SAlfred Perlstein static void dc_setfilt_21143(struct dc_softc *);
279e51a25f8SAlfred Perlstein static void dc_setfilt_asix(struct dc_softc *);
280e51a25f8SAlfred Perlstein static void dc_setfilt_admtek(struct dc_softc *);
281e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
28296f2e892SBill Paul 
283e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
28496f2e892SBill Paul 
285e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
286e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
287e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
28896f2e892SBill Paul 
2893097aa70SWarner Losh static void dc_read_srom(struct dc_softc *, int);
290e51a25f8SAlfred Perlstein static void dc_parse_21143_srom(struct dc_softc *);
291e51a25f8SAlfred Perlstein static void dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
292e51a25f8SAlfred Perlstein static void dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
293e51a25f8SAlfred Perlstein static void dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
294e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
2955c1cfac4SBill Paul 
296d24ae19dSWarner Losh static void dc_dma_map_txbuf(void *, bus_dma_segment_t *, int, bus_size_t, int);
297d24ae19dSWarner Losh static void dc_dma_map_rxbuf(void *, bus_dma_segment_t *, int, bus_size_t, int);
29856e5e7aeSMaxime Henrion 
29996f2e892SBill Paul #ifdef DC_USEIOSPACE
30096f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
30196f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
30296f2e892SBill Paul #else
30396f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
30496f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
30596f2e892SBill Paul #endif
30696f2e892SBill Paul 
30796f2e892SBill Paul static device_method_t dc_methods[] = {
30896f2e892SBill Paul 	/* Device interface */
30996f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
31096f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
31196f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
312e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
313e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
31496f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
31596f2e892SBill Paul 
31696f2e892SBill Paul 	/* bus interface */
31796f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
31896f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
31996f2e892SBill Paul 
32096f2e892SBill Paul 	/* MII interface */
32196f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
32296f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
32396f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
324f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
32596f2e892SBill Paul 
32696f2e892SBill Paul 	{ 0, 0 }
32796f2e892SBill Paul };
32896f2e892SBill Paul 
32996f2e892SBill Paul static driver_t dc_driver = {
33096f2e892SBill Paul 	"dc",
33196f2e892SBill Paul 	dc_methods,
33296f2e892SBill Paul 	sizeof(struct dc_softc)
33396f2e892SBill Paul };
33496f2e892SBill Paul 
33596f2e892SBill Paul static devclass_t dc_devclass;
33601faf54bSLuigi Rizzo #ifdef __i386__
33701faf54bSLuigi Rizzo static int dc_quick = 1;
338b3811c95SMaxime Henrion SYSCTL_INT(_hw, OID_AUTO, dc_quick, CTLFLAG_RW, &dc_quick, 0,
33905992bb5SRuslan Ermilov     "do not m_devget() in dc driver");
34001faf54bSLuigi Rizzo #endif
34196f2e892SBill Paul 
342347934faSWarner Losh DRIVER_MODULE(dc, cardbus, dc_driver, dc_devclass, 0, 0);
343f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
34496f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
34596f2e892SBill Paul 
34696f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
34796f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
34896f2e892SBill Paul 
34996f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
35096f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
35196f2e892SBill Paul 
35296f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
35396f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
35496f2e892SBill Paul 
355e3d2833aSAlfred Perlstein static void
3560934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
35796f2e892SBill Paul {
35896f2e892SBill Paul 	int idx;
35996f2e892SBill Paul 
36096f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
36196f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
36296f2e892SBill Paul }
36396f2e892SBill Paul 
3642c876e15SPoul-Henning Kamp static void
3650934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3663097aa70SWarner Losh {
3673097aa70SWarner Losh 	int i;
3683097aa70SWarner Losh 
3693097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3703097aa70SWarner Losh 	dc_eeprom_idle(sc);
3713097aa70SWarner Losh 
3723097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3733097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3743097aa70SWarner Losh 	dc_delay(sc);
3753097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3763097aa70SWarner Losh 	dc_delay(sc);
3773097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3783097aa70SWarner Losh 	dc_delay(sc);
3793097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3803097aa70SWarner Losh 	dc_delay(sc);
3813097aa70SWarner Losh 
3823097aa70SWarner Losh 	for (i = 3; i--;) {
3833097aa70SWarner Losh 		if (6 & (1 << i))
3843097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3853097aa70SWarner Losh 		else
3863097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3873097aa70SWarner Losh 		dc_delay(sc);
3883097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3893097aa70SWarner Losh 		dc_delay(sc);
3903097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3913097aa70SWarner Losh 		dc_delay(sc);
3923097aa70SWarner Losh 	}
3933097aa70SWarner Losh 
3943097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
3953097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3963097aa70SWarner Losh 		dc_delay(sc);
3973097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
3983097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3993097aa70SWarner Losh 			dc_delay(sc);
4003097aa70SWarner Losh 			break;
4013097aa70SWarner Losh 		}
4023097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4033097aa70SWarner Losh 		dc_delay(sc);
4043097aa70SWarner Losh 	}
4053097aa70SWarner Losh 
4063097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4073097aa70SWarner Losh 	dc_eeprom_idle(sc);
4083097aa70SWarner Losh 
4093097aa70SWarner Losh 	if (i < 4 || i > 12)
4103097aa70SWarner Losh 		sc->dc_romwidth = 6;
4113097aa70SWarner Losh 	else
4123097aa70SWarner Losh 		sc->dc_romwidth = i;
4133097aa70SWarner Losh 
4143097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4153097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4163097aa70SWarner Losh 	dc_delay(sc);
4173097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4183097aa70SWarner Losh 	dc_delay(sc);
4193097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4203097aa70SWarner Losh 	dc_delay(sc);
4213097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4223097aa70SWarner Losh 	dc_delay(sc);
4233097aa70SWarner Losh 
4243097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4253097aa70SWarner Losh 	dc_eeprom_idle(sc);
4263097aa70SWarner Losh }
4273097aa70SWarner Losh 
428e3d2833aSAlfred Perlstein static void
4290934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
43096f2e892SBill Paul {
4310934f18aSMaxime Henrion 	int i;
43296f2e892SBill Paul 
43396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
43496f2e892SBill Paul 	dc_delay(sc);
43596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
43696f2e892SBill Paul 	dc_delay(sc);
43796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43896f2e892SBill Paul 	dc_delay(sc);
43996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
44096f2e892SBill Paul 	dc_delay(sc);
44196f2e892SBill Paul 
44296f2e892SBill Paul 	for (i = 0; i < 25; i++) {
44396f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44496f2e892SBill Paul 		dc_delay(sc);
44596f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44696f2e892SBill Paul 		dc_delay(sc);
44796f2e892SBill Paul 	}
44896f2e892SBill Paul 
44996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45096f2e892SBill Paul 	dc_delay(sc);
45196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
45296f2e892SBill Paul 	dc_delay(sc);
45396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
45496f2e892SBill Paul }
45596f2e892SBill Paul 
45696f2e892SBill Paul /*
45796f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
45896f2e892SBill Paul  */
459e3d2833aSAlfred Perlstein static void
4600934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
46196f2e892SBill Paul {
4620934f18aSMaxime Henrion 	int d, i;
46396f2e892SBill Paul 
4643097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4653097aa70SWarner Losh 	for (i = 3; i--; ) {
4663097aa70SWarner Losh 		if (d & (1 << i))
4673097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
46896f2e892SBill Paul 		else
4693097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4703097aa70SWarner Losh 		dc_delay(sc);
4713097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4723097aa70SWarner Losh 		dc_delay(sc);
4733097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4743097aa70SWarner Losh 		dc_delay(sc);
4753097aa70SWarner Losh 	}
47696f2e892SBill Paul 
47796f2e892SBill Paul 	/*
47896f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
47996f2e892SBill Paul 	 */
4803097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4813097aa70SWarner Losh 		if (addr & (1 << i)) {
48296f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
48396f2e892SBill Paul 		} else {
48496f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
48596f2e892SBill Paul 		}
48696f2e892SBill Paul 		dc_delay(sc);
48796f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
48896f2e892SBill Paul 		dc_delay(sc);
48996f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
49096f2e892SBill Paul 		dc_delay(sc);
49196f2e892SBill Paul 	}
49296f2e892SBill Paul }
49396f2e892SBill Paul 
49496f2e892SBill Paul /*
49596f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
49696f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
49796f2e892SBill Paul  * the EEPROM.
49896f2e892SBill Paul  */
499e3d2833aSAlfred Perlstein static void
5000934f18aSMaxime Henrion dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, u_int16_t *dest)
50196f2e892SBill Paul {
5020934f18aSMaxime Henrion 	int i;
50396f2e892SBill Paul 	u_int32_t r;
50496f2e892SBill Paul 
50596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
50696f2e892SBill Paul 
50796f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
50896f2e892SBill Paul 		DELAY(1);
50996f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
51096f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
51196f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
51296f2e892SBill Paul 			return;
51396f2e892SBill Paul 		}
51496f2e892SBill Paul 	}
51596f2e892SBill Paul }
51696f2e892SBill Paul 
51796f2e892SBill Paul /*
51896f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
519feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
520feb78939SJonathan Chen  * the EEPROM, too.
521feb78939SJonathan Chen  */
522e3d2833aSAlfred Perlstein static void
5230934f18aSMaxime Henrion dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, u_int16_t *dest)
524feb78939SJonathan Chen {
5250934f18aSMaxime Henrion 
526feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
527feb78939SJonathan Chen 
528feb78939SJonathan Chen 	addr *= 2;
529feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
530feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
531feb78939SJonathan Chen 	addr += 1;
532feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
533feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
534feb78939SJonathan Chen 
535feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
536feb78939SJonathan Chen }
537feb78939SJonathan Chen 
538feb78939SJonathan Chen /*
539feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
54096f2e892SBill Paul  */
541e3d2833aSAlfred Perlstein static void
5420934f18aSMaxime Henrion dc_eeprom_getword(struct dc_softc *sc, int addr, u_int16_t *dest)
54396f2e892SBill Paul {
5440934f18aSMaxime Henrion 	int i;
54596f2e892SBill Paul 	u_int16_t word = 0;
54696f2e892SBill Paul 
54796f2e892SBill Paul 	/* Force EEPROM to idle state. */
54896f2e892SBill Paul 	dc_eeprom_idle(sc);
54996f2e892SBill Paul 
55096f2e892SBill Paul 	/* Enter EEPROM access mode. */
55196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
55296f2e892SBill Paul 	dc_delay(sc);
55396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
55496f2e892SBill Paul 	dc_delay(sc);
55596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
55696f2e892SBill Paul 	dc_delay(sc);
55796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
55896f2e892SBill Paul 	dc_delay(sc);
55996f2e892SBill Paul 
56096f2e892SBill Paul 	/*
56196f2e892SBill Paul 	 * Send address of word we want to read.
56296f2e892SBill Paul 	 */
56396f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
56496f2e892SBill Paul 
56596f2e892SBill Paul 	/*
56696f2e892SBill Paul 	 * Start reading bits from EEPROM.
56796f2e892SBill Paul 	 */
56896f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
56996f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
57096f2e892SBill Paul 		dc_delay(sc);
57196f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
57296f2e892SBill Paul 			word |= i;
57396f2e892SBill Paul 		dc_delay(sc);
57496f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
57596f2e892SBill Paul 		dc_delay(sc);
57696f2e892SBill Paul 	}
57796f2e892SBill Paul 
57896f2e892SBill Paul 	/* Turn off EEPROM access mode. */
57996f2e892SBill Paul 	dc_eeprom_idle(sc);
58096f2e892SBill Paul 
58196f2e892SBill Paul 	*dest = word;
58296f2e892SBill Paul }
58396f2e892SBill Paul 
58496f2e892SBill Paul /*
58596f2e892SBill Paul  * Read a sequence of words from the EEPROM.
58696f2e892SBill Paul  */
587e3d2833aSAlfred Perlstein static void
5888c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
58996f2e892SBill Paul {
59096f2e892SBill Paul 	int i;
59196f2e892SBill Paul 	u_int16_t word = 0, *ptr;
59296f2e892SBill Paul 
59396f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
59496f2e892SBill Paul 		if (DC_IS_PNIC(sc))
59596f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
596feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
597feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
59896f2e892SBill Paul 		else
59996f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
60096f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
6018c7ff1f3SMaxime Henrion 		if (be)
6028c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
60396f2e892SBill Paul 		else
6048c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
60596f2e892SBill Paul 	}
60696f2e892SBill Paul }
60796f2e892SBill Paul 
60896f2e892SBill Paul /*
60996f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
61096f2e892SBill Paul  * Application Notes pp.19-21.
61196f2e892SBill Paul  */
61296f2e892SBill Paul /*
61396f2e892SBill Paul  * Write a bit to the MII bus.
61496f2e892SBill Paul  */
615e3d2833aSAlfred Perlstein static void
6160934f18aSMaxime Henrion dc_mii_writebit(struct dc_softc *sc, int bit)
61796f2e892SBill Paul {
6180934f18aSMaxime Henrion 
61996f2e892SBill Paul 	if (bit)
62096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
62196f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE | DC_SIO_MII_DATAOUT);
62296f2e892SBill Paul 	else
62396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
62496f2e892SBill Paul 
62596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
62696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
62796f2e892SBill Paul }
62896f2e892SBill Paul 
62996f2e892SBill Paul /*
63096f2e892SBill Paul  * Read a bit from the MII bus.
63196f2e892SBill Paul  */
632e3d2833aSAlfred Perlstein static int
6330934f18aSMaxime Henrion dc_mii_readbit(struct dc_softc *sc)
63496f2e892SBill Paul {
6350934f18aSMaxime Henrion 
63696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ | DC_SIO_MII_DIR);
63796f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
63896f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
64096f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
64196f2e892SBill Paul 		return (1);
64296f2e892SBill Paul 
64396f2e892SBill Paul 	return (0);
64496f2e892SBill Paul }
64596f2e892SBill Paul 
64696f2e892SBill Paul /*
64796f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
64896f2e892SBill Paul  */
649e3d2833aSAlfred Perlstein static void
6500934f18aSMaxime Henrion dc_mii_sync(struct dc_softc *sc)
65196f2e892SBill Paul {
6520934f18aSMaxime Henrion 	int i;
65396f2e892SBill Paul 
65496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
65596f2e892SBill Paul 
65696f2e892SBill Paul 	for (i = 0; i < 32; i++)
65796f2e892SBill Paul 		dc_mii_writebit(sc, 1);
65896f2e892SBill Paul }
65996f2e892SBill Paul 
66096f2e892SBill Paul /*
66196f2e892SBill Paul  * Clock a series of bits through the MII.
66296f2e892SBill Paul  */
663e3d2833aSAlfred Perlstein static void
6640934f18aSMaxime Henrion dc_mii_send(struct dc_softc *sc, u_int32_t bits, int cnt)
66596f2e892SBill Paul {
66696f2e892SBill Paul 	int i;
66796f2e892SBill Paul 
66896f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
66996f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
67096f2e892SBill Paul }
67196f2e892SBill Paul 
67296f2e892SBill Paul /*
67396f2e892SBill Paul  * Read an PHY register through the MII.
67496f2e892SBill Paul  */
675e3d2833aSAlfred Perlstein static int
6760934f18aSMaxime Henrion dc_mii_readreg(struct dc_softc *sc, struct dc_mii_frame *frame)
67796f2e892SBill Paul {
678d1ce9105SBill Paul 	int i, ack;
67996f2e892SBill Paul 
68096f2e892SBill Paul 	/*
68196f2e892SBill Paul 	 * Set up frame for RX.
68296f2e892SBill Paul 	 */
68396f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
68496f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
68596f2e892SBill Paul 	frame->mii_turnaround = 0;
68696f2e892SBill Paul 	frame->mii_data = 0;
68796f2e892SBill Paul 
68896f2e892SBill Paul 	/*
68996f2e892SBill Paul 	 * Sync the PHYs.
69096f2e892SBill Paul 	 */
69196f2e892SBill Paul 	dc_mii_sync(sc);
69296f2e892SBill Paul 
69396f2e892SBill Paul 	/*
69496f2e892SBill Paul 	 * Send command/address info.
69596f2e892SBill Paul 	 */
69696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
69796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
69896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
69996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
70096f2e892SBill Paul 
70196f2e892SBill Paul #ifdef notdef
70296f2e892SBill Paul 	/* Idle bit */
70396f2e892SBill Paul 	dc_mii_writebit(sc, 1);
70496f2e892SBill Paul 	dc_mii_writebit(sc, 0);
70596f2e892SBill Paul #endif
70696f2e892SBill Paul 
7070934f18aSMaxime Henrion 	/* Check for ack. */
70896f2e892SBill Paul 	ack = dc_mii_readbit(sc);
70996f2e892SBill Paul 
71096f2e892SBill Paul 	/*
71196f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
71296f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
71396f2e892SBill Paul 	 */
71496f2e892SBill Paul 	if (ack) {
7150934f18aSMaxime Henrion 		for (i = 0; i < 16; i++)
71696f2e892SBill Paul 			dc_mii_readbit(sc);
71796f2e892SBill Paul 		goto fail;
71896f2e892SBill Paul 	}
71996f2e892SBill Paul 
72096f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
72196f2e892SBill Paul 		if (!ack) {
72296f2e892SBill Paul 			if (dc_mii_readbit(sc))
72396f2e892SBill Paul 				frame->mii_data |= i;
72496f2e892SBill Paul 		}
72596f2e892SBill Paul 	}
72696f2e892SBill Paul 
72796f2e892SBill Paul fail:
72896f2e892SBill Paul 
72996f2e892SBill Paul 	dc_mii_writebit(sc, 0);
73096f2e892SBill Paul 	dc_mii_writebit(sc, 0);
73196f2e892SBill Paul 
73296f2e892SBill Paul 	if (ack)
73396f2e892SBill Paul 		return (1);
73496f2e892SBill Paul 	return (0);
73596f2e892SBill Paul }
73696f2e892SBill Paul 
73796f2e892SBill Paul /*
73896f2e892SBill Paul  * Write to a PHY register through the MII.
73996f2e892SBill Paul  */
740e3d2833aSAlfred Perlstein static int
7410934f18aSMaxime Henrion dc_mii_writereg(struct dc_softc *sc, struct dc_mii_frame *frame)
74296f2e892SBill Paul {
7430934f18aSMaxime Henrion 
74496f2e892SBill Paul 	/*
74596f2e892SBill Paul 	 * Set up frame for TX.
74696f2e892SBill Paul 	 */
74796f2e892SBill Paul 
74896f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
74996f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
75096f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
75196f2e892SBill Paul 
75296f2e892SBill Paul 	/*
75396f2e892SBill Paul 	 * Sync the PHYs.
75496f2e892SBill Paul 	 */
75596f2e892SBill Paul 	dc_mii_sync(sc);
75696f2e892SBill Paul 
75796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
75896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
75996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
76096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
76196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
76296f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
76396f2e892SBill Paul 
76496f2e892SBill Paul 	/* Idle bit. */
76596f2e892SBill Paul 	dc_mii_writebit(sc, 0);
76696f2e892SBill Paul 	dc_mii_writebit(sc, 0);
76796f2e892SBill Paul 
76896f2e892SBill Paul 	return (0);
76996f2e892SBill Paul }
77096f2e892SBill Paul 
771e3d2833aSAlfred Perlstein static int
7720934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
77396f2e892SBill Paul {
77496f2e892SBill Paul 	struct dc_mii_frame frame;
77596f2e892SBill Paul 	struct dc_softc	 *sc;
776c85c4667SBill Paul 	int i, rval, phy_reg = 0;
77796f2e892SBill Paul 
77896f2e892SBill Paul 	sc = device_get_softc(dev);
7790934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
78096f2e892SBill Paul 
78196f2e892SBill Paul 	/*
78296f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
78396f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
78496f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
78596f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
78696f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
78796f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
78896f2e892SBill Paul 	 * that the PHY is at MII address 1.
78996f2e892SBill Paul 	 */
79096f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
79196f2e892SBill Paul 		return (0);
79296f2e892SBill Paul 
7931af8bec7SBill Paul 	/*
7941af8bec7SBill Paul 	 * Note: the ukphy probes of the RS7112 report a PHY at
7951af8bec7SBill Paul 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
7961af8bec7SBill Paul 	 * so we only respond to correct one.
7971af8bec7SBill Paul 	 */
7981af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
7991af8bec7SBill Paul 		return (0);
8001af8bec7SBill Paul 
8015c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
80296f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
80396f2e892SBill Paul 			switch (reg) {
80496f2e892SBill Paul 			case MII_BMSR:
80596f2e892SBill Paul 			/*
80696f2e892SBill Paul 			 * Fake something to make the probe
80796f2e892SBill Paul 			 * code think there's a PHY here.
80896f2e892SBill Paul 			 */
80996f2e892SBill Paul 				return (BMSR_MEDIAMASK);
81096f2e892SBill Paul 				break;
81196f2e892SBill Paul 			case MII_PHYIDR1:
81296f2e892SBill Paul 				if (DC_IS_PNIC(sc))
81396f2e892SBill Paul 					return (DC_VENDORID_LO);
81496f2e892SBill Paul 				return (DC_VENDORID_DEC);
81596f2e892SBill Paul 				break;
81696f2e892SBill Paul 			case MII_PHYIDR2:
81796f2e892SBill Paul 				if (DC_IS_PNIC(sc))
81896f2e892SBill Paul 					return (DC_DEVICEID_82C168);
81996f2e892SBill Paul 				return (DC_DEVICEID_21143);
82096f2e892SBill Paul 				break;
82196f2e892SBill Paul 			default:
82296f2e892SBill Paul 				return (0);
82396f2e892SBill Paul 				break;
82496f2e892SBill Paul 			}
82596f2e892SBill Paul 		} else
82696f2e892SBill Paul 			return (0);
82796f2e892SBill Paul 	}
82896f2e892SBill Paul 
82996f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
83096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
83196f2e892SBill Paul 		    (phy << 23) | (reg << 18));
83296f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
83396f2e892SBill Paul 			DELAY(1);
83496f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
83596f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
83696f2e892SBill Paul 				rval &= 0xFFFF;
83796f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
83896f2e892SBill Paul 			}
83996f2e892SBill Paul 		}
84096f2e892SBill Paul 		return (0);
84196f2e892SBill Paul 	}
84296f2e892SBill Paul 
84396f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
84496f2e892SBill Paul 		switch (reg) {
84596f2e892SBill Paul 		case MII_BMCR:
84696f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
84796f2e892SBill Paul 			break;
84896f2e892SBill Paul 		case MII_BMSR:
84996f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
85096f2e892SBill Paul 			break;
85196f2e892SBill Paul 		case MII_PHYIDR1:
85296f2e892SBill Paul 			phy_reg = DC_AL_VENID;
85396f2e892SBill Paul 			break;
85496f2e892SBill Paul 		case MII_PHYIDR2:
85596f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
85696f2e892SBill Paul 			break;
85796f2e892SBill Paul 		case MII_ANAR:
85896f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
85996f2e892SBill Paul 			break;
86096f2e892SBill Paul 		case MII_ANLPAR:
86196f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
86296f2e892SBill Paul 			break;
86396f2e892SBill Paul 		case MII_ANER:
86496f2e892SBill Paul 			phy_reg = DC_AL_ANER;
86596f2e892SBill Paul 			break;
86696f2e892SBill Paul 		default:
86722f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
86822f6205dSJohn Baldwin 			    reg);
86996f2e892SBill Paul 			return (0);
87096f2e892SBill Paul 			break;
87196f2e892SBill Paul 		}
87296f2e892SBill Paul 
87396f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
87496f2e892SBill Paul 
87596f2e892SBill Paul 		if (rval == 0xFFFF)
87696f2e892SBill Paul 			return (0);
87796f2e892SBill Paul 		return (rval);
87896f2e892SBill Paul 	}
87996f2e892SBill Paul 
88096f2e892SBill Paul 	frame.mii_phyaddr = phy;
88196f2e892SBill Paul 	frame.mii_regaddr = reg;
882419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
883f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
884f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
885419146d9SBill Paul 	}
88696f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
887419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
888f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
88996f2e892SBill Paul 
89096f2e892SBill Paul 	return (frame.mii_data);
89196f2e892SBill Paul }
89296f2e892SBill Paul 
893e3d2833aSAlfred Perlstein static int
8940934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
89596f2e892SBill Paul {
89696f2e892SBill Paul 	struct dc_softc *sc;
89796f2e892SBill Paul 	struct dc_mii_frame frame;
898c85c4667SBill Paul 	int i, phy_reg = 0;
89996f2e892SBill Paul 
90096f2e892SBill Paul 	sc = device_get_softc(dev);
9010934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
90296f2e892SBill Paul 
90396f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
90496f2e892SBill Paul 		return (0);
90596f2e892SBill Paul 
9061af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
9071af8bec7SBill Paul 		return (0);
9081af8bec7SBill Paul 
90996f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
91096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
91196f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
91296f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
91396f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
91496f2e892SBill Paul 				break;
91596f2e892SBill Paul 		}
91696f2e892SBill Paul 		return (0);
91796f2e892SBill Paul 	}
91896f2e892SBill Paul 
91996f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
92096f2e892SBill Paul 		switch (reg) {
92196f2e892SBill Paul 		case MII_BMCR:
92296f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
92396f2e892SBill Paul 			break;
92496f2e892SBill Paul 		case MII_BMSR:
92596f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
92696f2e892SBill Paul 			break;
92796f2e892SBill Paul 		case MII_PHYIDR1:
92896f2e892SBill Paul 			phy_reg = DC_AL_VENID;
92996f2e892SBill Paul 			break;
93096f2e892SBill Paul 		case MII_PHYIDR2:
93196f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
93296f2e892SBill Paul 			break;
93396f2e892SBill Paul 		case MII_ANAR:
93496f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
93596f2e892SBill Paul 			break;
93696f2e892SBill Paul 		case MII_ANLPAR:
93796f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
93896f2e892SBill Paul 			break;
93996f2e892SBill Paul 		case MII_ANER:
94096f2e892SBill Paul 			phy_reg = DC_AL_ANER;
94196f2e892SBill Paul 			break;
94296f2e892SBill Paul 		default:
94322f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
94422f6205dSJohn Baldwin 			    reg);
94596f2e892SBill Paul 			return (0);
94696f2e892SBill Paul 			break;
94796f2e892SBill Paul 		}
94896f2e892SBill Paul 
94996f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
95096f2e892SBill Paul 		return (0);
95196f2e892SBill Paul 	}
95296f2e892SBill Paul 
95396f2e892SBill Paul 	frame.mii_phyaddr = phy;
95496f2e892SBill Paul 	frame.mii_regaddr = reg;
95596f2e892SBill Paul 	frame.mii_data = data;
95696f2e892SBill Paul 
957419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
958f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
959f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
960419146d9SBill Paul 	}
96196f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
962419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
963f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
96496f2e892SBill Paul 
96596f2e892SBill Paul 	return (0);
96696f2e892SBill Paul }
96796f2e892SBill Paul 
968e3d2833aSAlfred Perlstein static void
9690934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
97096f2e892SBill Paul {
97196f2e892SBill Paul 	struct dc_softc *sc;
97296f2e892SBill Paul 	struct mii_data *mii;
973f43d9309SBill Paul 	struct ifmedia *ifm;
97496f2e892SBill Paul 
97596f2e892SBill Paul 	sc = device_get_softc(dev);
97696f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
97796f2e892SBill Paul 		return;
9785c1cfac4SBill Paul 
97996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
980f43d9309SBill Paul 	ifm = &mii->mii_media;
981f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
98245521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
983f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
984f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
985f43d9309SBill Paul 	} else {
98696f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
98796f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
988f43d9309SBill Paul 	}
989f43d9309SBill Paul }
990f43d9309SBill Paul 
991f43d9309SBill Paul /*
992f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
993f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
994f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
995f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
996f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
997f43d9309SBill Paul  * with it itself. *sigh*
998f43d9309SBill Paul  */
999e3d2833aSAlfred Perlstein static void
10000934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
1001f43d9309SBill Paul {
1002f43d9309SBill Paul 	struct dc_softc *sc;
1003f43d9309SBill Paul 	struct mii_data *mii;
1004f43d9309SBill Paul 	struct ifmedia *ifm;
1005f43d9309SBill Paul 	int rev;
1006f43d9309SBill Paul 
1007f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
1008f43d9309SBill Paul 
1009f43d9309SBill Paul 	sc = device_get_softc(dev);
1010f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
1011f43d9309SBill Paul 	ifm = &mii->mii_media;
1012f43d9309SBill Paul 
1013f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
101445521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
101596f2e892SBill Paul }
101696f2e892SBill Paul 
101779d11e09SBill Paul #define DC_BITS_512	9
101879d11e09SBill Paul #define DC_BITS_128	7
101979d11e09SBill Paul #define DC_BITS_64	6
102096f2e892SBill Paul 
10213373489bSWarner Losh static uint32_t
10223373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
102396f2e892SBill Paul {
10243373489bSWarner Losh 	uint32_t crc;
102596f2e892SBill Paul 
102696f2e892SBill Paul 	/* Compute CRC for the address value. */
10270e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
102896f2e892SBill Paul 
102979d11e09SBill Paul 	/*
103079d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
103179d11e09SBill Paul 	 * chips is only 128 bits wide.
103279d11e09SBill Paul 	 */
103379d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
103479d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
103596f2e892SBill Paul 
103679d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
103779d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
103879d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
103979d11e09SBill Paul 
1040feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1041feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1042feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1043feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
10440934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
1045feb78939SJonathan Chen 		else
10460934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
10470934f18aSMaxime Henrion 			    (12 << 4));
1048feb78939SJonathan Chen 	}
1049feb78939SJonathan Chen 
105079d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
105196f2e892SBill Paul }
105296f2e892SBill Paul 
105396f2e892SBill Paul /*
105496f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
105596f2e892SBill Paul  */
10563373489bSWarner Losh static uint32_t
10573373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
105896f2e892SBill Paul {
10590e939c0cSChristian Weisgerber 	uint32_t crc;
106096f2e892SBill Paul 
106196f2e892SBill Paul 	/* Compute CRC for the address value. */
10620e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
106396f2e892SBill Paul 
10640934f18aSMaxime Henrion 	/* Return the filter bit position. */
106596f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
106696f2e892SBill Paul }
106796f2e892SBill Paul 
106896f2e892SBill Paul /*
106996f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
107096f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
107196f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
107296f2e892SBill Paul  *
107396f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
107496f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
107596f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
107696f2e892SBill Paul  * we need that too.
107796f2e892SBill Paul  */
10782c876e15SPoul-Henning Kamp static void
10790934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
108096f2e892SBill Paul {
108196f2e892SBill Paul 	struct dc_desc *sframe;
108296f2e892SBill Paul 	u_int32_t h, *sp;
108396f2e892SBill Paul 	struct ifmultiaddr *ifma;
108496f2e892SBill Paul 	struct ifnet *ifp;
108596f2e892SBill Paul 	int i;
108696f2e892SBill Paul 
1087fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
108896f2e892SBill Paul 
108996f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
109096f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
109196f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
109296f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
109356e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
10940934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
109596f2e892SBill Paul 
1096af4358c7SMaxime Henrion 	sframe->dc_data = htole32(sc->dc_saddr);
1097af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1098af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
109996f2e892SBill Paul 
110056e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
110196f2e892SBill Paul 
110296f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
110396f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
110496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
110596f2e892SBill Paul 	else
110696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
110796f2e892SBill Paul 
110896f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
110996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111096f2e892SBill Paul 	else
111196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111296f2e892SBill Paul 
111313b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
11146817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
111596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
111696f2e892SBill Paul 			continue;
1117aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
111896f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1119af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
112096f2e892SBill Paul 	}
112113b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
112296f2e892SBill Paul 
112396f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1124aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1125af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
112696f2e892SBill Paul 	}
112796f2e892SBill Paul 
112896f2e892SBill Paul 	/* Set our MAC address */
1129fc74a9f9SBrooks Davis 	sp[39] = DC_SP_MAC(((u_int16_t *)IFP2ENADDR(sc->dc_ifp))[0]);
1130fc74a9f9SBrooks Davis 	sp[40] = DC_SP_MAC(((u_int16_t *)IFP2ENADDR(sc->dc_ifp))[1]);
1131fc74a9f9SBrooks Davis 	sp[41] = DC_SP_MAC(((u_int16_t *)IFP2ENADDR(sc->dc_ifp))[2]);
113296f2e892SBill Paul 
1133af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
113496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
113596f2e892SBill Paul 
113696f2e892SBill Paul 	/*
113796f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
113896f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
113996f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
114096f2e892SBill Paul 	 * medicine.
114196f2e892SBill Paul 	 */
114296f2e892SBill Paul 	DELAY(10000);
114396f2e892SBill Paul 
114496f2e892SBill Paul 	ifp->if_timer = 5;
114596f2e892SBill Paul }
114696f2e892SBill Paul 
11472c876e15SPoul-Henning Kamp static void
11480934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
114996f2e892SBill Paul {
115096f2e892SBill Paul 	struct ifnet *ifp;
11510934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
115296f2e892SBill Paul 	int h = 0;
115396f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
115496f2e892SBill Paul 
1155fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
115696f2e892SBill Paul 
11570934f18aSMaxime Henrion 	/* Init our MAC address. */
1158fc74a9f9SBrooks Davis 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&IFP2ENADDR(sc->dc_ifp)[0]));
1159fc74a9f9SBrooks Davis 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&IFP2ENADDR(sc->dc_ifp)[4]));
116096f2e892SBill Paul 
116196f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
116296f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
116396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116496f2e892SBill Paul 	else
116596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116696f2e892SBill Paul 
116796f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
116896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
116996f2e892SBill Paul 	else
117096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
117196f2e892SBill Paul 
11720934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
117396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
117496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
117596f2e892SBill Paul 
117696f2e892SBill Paul 	/*
117796f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
117896f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
117996f2e892SBill Paul 	 */
118096f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
118196f2e892SBill Paul 		return;
118296f2e892SBill Paul 
11830934f18aSMaxime Henrion 	/* Now program new ones. */
118413b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
11856817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
118696f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
118796f2e892SBill Paul 			continue;
1188acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1189aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1190aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1191acc1bcccSMartin Blapp 		else
1192aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1193aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
119496f2e892SBill Paul 		if (h < 32)
119596f2e892SBill Paul 			hashes[0] |= (1 << h);
119696f2e892SBill Paul 		else
119796f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
119896f2e892SBill Paul 	}
119913b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
120096f2e892SBill Paul 
120196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
120296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
120396f2e892SBill Paul }
120496f2e892SBill Paul 
12052c876e15SPoul-Henning Kamp static void
12060934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
120796f2e892SBill Paul {
120896f2e892SBill Paul 	struct ifnet *ifp;
12090934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
121096f2e892SBill Paul 	int h = 0;
121196f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
121296f2e892SBill Paul 
1213fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
121496f2e892SBill Paul 
121596f2e892SBill Paul 	/* Init our MAC address */
121696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
121796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
1218fc74a9f9SBrooks Davis 	    *(u_int32_t *)(&IFP2ENADDR(sc->dc_ifp)[0]));
121996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
122096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
1221fc74a9f9SBrooks Davis 	    *(u_int32_t *)(&IFP2ENADDR(sc->dc_ifp)[4]));
122296f2e892SBill Paul 
122396f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
122496f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
122596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
122696f2e892SBill Paul 	else
122796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
122896f2e892SBill Paul 
122996f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
123096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
123196f2e892SBill Paul 	else
123296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
123396f2e892SBill Paul 
123496f2e892SBill Paul 	/*
123596f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
123696f2e892SBill Paul 	 * of broadcast frames.
123796f2e892SBill Paul 	 */
123896f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
123996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
124096f2e892SBill Paul 	else
124196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
124296f2e892SBill Paul 
124396f2e892SBill Paul 	/* first, zot all the existing hash bits */
124496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
124596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
124696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
124796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
124896f2e892SBill Paul 
124996f2e892SBill Paul 	/*
125096f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
125196f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
125296f2e892SBill Paul 	 */
125396f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
125496f2e892SBill Paul 		return;
125596f2e892SBill Paul 
125696f2e892SBill Paul 	/* now program new ones */
125713b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
12586817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
125996f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
126096f2e892SBill Paul 			continue;
1261aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
126296f2e892SBill Paul 		if (h < 32)
126396f2e892SBill Paul 			hashes[0] |= (1 << h);
126496f2e892SBill Paul 		else
126596f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
126696f2e892SBill Paul 	}
126713b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
126896f2e892SBill Paul 
126996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
127096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
127196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
127296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
127396f2e892SBill Paul }
127496f2e892SBill Paul 
12752c876e15SPoul-Henning Kamp static void
12760934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1277feb78939SJonathan Chen {
12780934f18aSMaxime Henrion 	struct ifnet *ifp;
12790934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1280feb78939SJonathan Chen 	struct dc_desc *sframe;
1281feb78939SJonathan Chen 	u_int32_t h, *sp;
1282feb78939SJonathan Chen 	int i;
1283feb78939SJonathan Chen 
1284fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1285feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1286feb78939SJonathan Chen 
1287feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1288feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1289feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1290feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
129156e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
12920934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1293feb78939SJonathan Chen 
1294af4358c7SMaxime Henrion 	sframe->dc_data = htole32(sc->dc_saddr);
1295af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1296af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1297feb78939SJonathan Chen 
129856e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1299feb78939SJonathan Chen 
1300feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1301feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1302feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1303feb78939SJonathan Chen 	else
1304feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1305feb78939SJonathan Chen 
1306feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1307feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1308feb78939SJonathan Chen 	else
1309feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1310feb78939SJonathan Chen 
131113b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
13126817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1313feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1314feb78939SJonathan Chen 			continue;
1315aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13161d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1317af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1318feb78939SJonathan Chen 	}
131913b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
1320feb78939SJonathan Chen 
1321feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1322aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1323af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1324feb78939SJonathan Chen 	}
1325feb78939SJonathan Chen 
1326feb78939SJonathan Chen 	/* Set our MAC address */
1327fc74a9f9SBrooks Davis 	sp[0] = DC_SP_MAC(((u_int16_t *)IFP2ENADDR(sc->dc_ifp))[0]);
1328fc74a9f9SBrooks Davis 	sp[1] = DC_SP_MAC(((u_int16_t *)IFP2ENADDR(sc->dc_ifp))[1]);
1329fc74a9f9SBrooks Davis 	sp[2] = DC_SP_MAC(((u_int16_t *)IFP2ENADDR(sc->dc_ifp))[2]);
1330feb78939SJonathan Chen 
1331feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1332feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
133313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1334af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
1335feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1336feb78939SJonathan Chen 
1337feb78939SJonathan Chen 	/*
13380934f18aSMaxime Henrion 	 * Wait some time...
1339feb78939SJonathan Chen 	 */
1340feb78939SJonathan Chen 	DELAY(1000);
1341feb78939SJonathan Chen 
1342feb78939SJonathan Chen 	ifp->if_timer = 5;
1343feb78939SJonathan Chen }
1344feb78939SJonathan Chen 
1345e3d2833aSAlfred Perlstein static void
13460934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
134796f2e892SBill Paul {
13480934f18aSMaxime Henrion 
134996f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13501af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
135196f2e892SBill Paul 		dc_setfilt_21143(sc);
135296f2e892SBill Paul 
135396f2e892SBill Paul 	if (DC_IS_ASIX(sc))
135496f2e892SBill Paul 		dc_setfilt_asix(sc);
135596f2e892SBill Paul 
135696f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
135796f2e892SBill Paul 		dc_setfilt_admtek(sc);
135896f2e892SBill Paul 
1359feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1360feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
136196f2e892SBill Paul }
136296f2e892SBill Paul 
136396f2e892SBill Paul /*
13640934f18aSMaxime Henrion  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13650934f18aSMaxime Henrion  * the netconfig register, we first have to put the transmit and/or
13660934f18aSMaxime Henrion  * receive logic in the idle state.
136796f2e892SBill Paul  */
1368e3d2833aSAlfred Perlstein static void
13690934f18aSMaxime Henrion dc_setcfg(struct dc_softc *sc, int media)
137096f2e892SBill Paul {
13710934f18aSMaxime Henrion 	int i, restart = 0, watchdogreg;
137296f2e892SBill Paul 	u_int32_t isr;
137396f2e892SBill Paul 
137496f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
137596f2e892SBill Paul 		return;
137696f2e892SBill Paul 
137796f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
137896f2e892SBill Paul 		restart = 1;
137996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
138096f2e892SBill Paul 
138196f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
138296f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
1383d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE &&
1384351267c1SMartin Blapp 			    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1385351267c1SMartin Blapp 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
138696f2e892SBill Paul 				break;
1387d467c136SBill Paul 			DELAY(10);
138896f2e892SBill Paul 		}
138996f2e892SBill Paul 
139096f2e892SBill Paul 		if (i == DC_TIMEOUT)
139122f6205dSJohn Baldwin 			if_printf(sc->dc_ifp,
139222f6205dSJohn Baldwin 			    "failed to force tx and rx to idle state\n");
139396f2e892SBill Paul 	}
139496f2e892SBill Paul 
139596f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1396042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1397042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
139896f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1399bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14000934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14018273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14028273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14038273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14044c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1405bf645417SBill Paul 			} else {
1406bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1407bf645417SBill Paul 			}
140896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
140996f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
141096f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
141196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141296f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
141388d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
141496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
141596f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1416e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1417e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
141896f2e892SBill Paul 		} else {
141996f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
142096f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
142196f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
142296f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
142396f2e892SBill Paul 			}
1424318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1425318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1426318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14275c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
14285c1cfac4SBill Paul 				dc_apply_fixup(sc,
14295c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14305c1cfac4SBill Paul 				    IFM_100_TX | IFM_FDX : IFM_100_TX);
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);
1454e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1455e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
145696f2e892SBill Paul 		} else {
145796f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
145896f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
145996f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
146096f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
146196f2e892SBill Paul 			}
146296f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1463318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146496f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14655c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14665c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14675c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14685c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14695c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14705c1cfac4SBill Paul 				else
14715c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14725c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14735c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14745c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14755c1cfac4SBill Paul 				dc_apply_fixup(sc,
14765c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14775c1cfac4SBill Paul 				    IFM_10_T | IFM_FDX : IFM_10_T);
14785c1cfac4SBill Paul 				DELAY(20000);
14795c1cfac4SBill Paul 			}
148096f2e892SBill Paul 		}
148196f2e892SBill Paul 	}
148296f2e892SBill Paul 
1483f43d9309SBill Paul 	/*
1484f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1485f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1486f43d9309SBill Paul 	 * on the external MII port.
1487f43d9309SBill Paul 	 */
1488f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
148945521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1490f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1491f43d9309SBill Paul 			sc->dc_link = 1;
1492f43d9309SBill Paul 		} else {
1493f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1494f43d9309SBill Paul 		}
1495f43d9309SBill Paul 	}
1496f43d9309SBill Paul 
149796f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
149896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149996f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
150096f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
150196f2e892SBill Paul 	} else {
150296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
150396f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
150496f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
150596f2e892SBill Paul 	}
150696f2e892SBill Paul 
150796f2e892SBill Paul 	if (restart)
150896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
150996f2e892SBill Paul }
151096f2e892SBill Paul 
1511e3d2833aSAlfred Perlstein static void
15120934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
151396f2e892SBill Paul {
15140934f18aSMaxime Henrion 	int i;
151596f2e892SBill Paul 
151696f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
151796f2e892SBill Paul 
151896f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
151996f2e892SBill Paul 		DELAY(10);
152096f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
152196f2e892SBill Paul 			break;
152296f2e892SBill Paul 	}
152396f2e892SBill Paul 
15241af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
15251d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
152696f2e892SBill Paul 		DELAY(10000);
152796f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152896f2e892SBill Paul 		i = 0;
152996f2e892SBill Paul 	}
153096f2e892SBill Paul 
153196f2e892SBill Paul 	if (i == DC_TIMEOUT)
153222f6205dSJohn Baldwin 		if_printf(sc->dc_ifp, "reset never completed!\n");
153396f2e892SBill Paul 
153496f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
153596f2e892SBill Paul 	DELAY(1000);
153696f2e892SBill Paul 
153796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
153896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
153996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
154096f2e892SBill Paul 
154191cc2adbSBill Paul 	/*
154291cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
154391cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
154491cc2adbSBill Paul 	 * into a state where it will never come out of reset
154591cc2adbSBill Paul 	 * until we reset the whole chip again.
154691cc2adbSBill Paul 	 */
15475c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
154891cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
15495c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
15505c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15515c1cfac4SBill Paul 	}
155296f2e892SBill Paul }
155396f2e892SBill Paul 
1554e3d2833aSAlfred Perlstein static struct dc_type *
15550934f18aSMaxime Henrion dc_devtype(device_t dev)
155696f2e892SBill Paul {
155796f2e892SBill Paul 	struct dc_type *t;
155896f2e892SBill Paul 	u_int32_t rev;
155996f2e892SBill Paul 
156096f2e892SBill Paul 	t = dc_devs;
156196f2e892SBill Paul 
156296f2e892SBill Paul 	while (t->dc_name != NULL) {
156396f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
156496f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
156596f2e892SBill Paul 			/* Check the PCI revision */
156696f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
156796f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
156896f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
156996f2e892SBill Paul 				t++;
157096f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
157196f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
157296f2e892SBill Paul 				t++;
157396f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
157479d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
157579d11e09SBill Paul 				t++;
157679d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
157796f2e892SBill Paul 			    rev >= DC_REVISION_98725)
157896f2e892SBill Paul 				t++;
157996f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
158096f2e892SBill Paul 			    rev >= DC_REVISION_88141)
158196f2e892SBill Paul 				t++;
158296f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
158396f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
158496f2e892SBill Paul 				t++;
158588d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
158688d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
158788d739dcSBill Paul 				t++;
1588e7b9ab3aSBill Paul 			/*
1589e7b9ab3aSBill Paul 			 * The Microsoft MN-130 has a device ID of 0x0002,
1590e7b9ab3aSBill Paul 			 * which happens to be the same as the PNIC 82c168.
1591e7b9ab3aSBill Paul 			 * To keep dc_attach() from getting confused, we
1592e7b9ab3aSBill Paul 			 * pretend its ID is something different.
1593e7b9ab3aSBill Paul 			 * XXX: ideally, dc_attach() should be checking
1594e7b9ab3aSBill Paul 			 * vendorid+deviceid together to avoid such
1595e7b9ab3aSBill Paul 			 * collisions.
1596e7b9ab3aSBill Paul 			 */
1597e7b9ab3aSBill Paul 			if (t->dc_vid == DC_VENDORID_MICROSOFT &&
1598e7b9ab3aSBill Paul 			    t->dc_did == DC_DEVICEID_MSMN130)
1599e7b9ab3aSBill Paul 				t++;
160096f2e892SBill Paul 			return (t);
160196f2e892SBill Paul 		}
160296f2e892SBill Paul 		t++;
160396f2e892SBill Paul 	}
160496f2e892SBill Paul 
160596f2e892SBill Paul 	return (NULL);
160696f2e892SBill Paul }
160796f2e892SBill Paul 
160896f2e892SBill Paul /*
160996f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
161096f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
161196f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
161296f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
161396f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
161496f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
161596f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
161696f2e892SBill Paul  */
1617e3d2833aSAlfred Perlstein static int
16180934f18aSMaxime Henrion dc_probe(device_t dev)
161996f2e892SBill Paul {
162096f2e892SBill Paul 	struct dc_type *t;
162196f2e892SBill Paul 
162296f2e892SBill Paul 	t = dc_devtype(dev);
162396f2e892SBill Paul 
162496f2e892SBill Paul 	if (t != NULL) {
162596f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1626d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
162796f2e892SBill Paul 	}
162896f2e892SBill Paul 
162996f2e892SBill Paul 	return (ENXIO);
163096f2e892SBill Paul }
163196f2e892SBill Paul 
1632e3d2833aSAlfred Perlstein static void
16330934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
16345c1cfac4SBill Paul {
16355c1cfac4SBill Paul 	struct dc_mediainfo *m;
16365c1cfac4SBill Paul 	u_int8_t *p;
16375c1cfac4SBill Paul 	int i;
16385d801891SBill Paul 	u_int32_t reg;
16395c1cfac4SBill Paul 
16405c1cfac4SBill Paul 	m = sc->dc_mi;
16415c1cfac4SBill Paul 
16425c1cfac4SBill Paul 	while (m != NULL) {
16435c1cfac4SBill Paul 		if (m->dc_media == media)
16445c1cfac4SBill Paul 			break;
16455c1cfac4SBill Paul 		m = m->dc_next;
16465c1cfac4SBill Paul 	}
16475c1cfac4SBill Paul 
16485c1cfac4SBill Paul 	if (m == NULL)
16495c1cfac4SBill Paul 		return;
16505c1cfac4SBill Paul 
16515c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16525c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16535c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16545c1cfac4SBill Paul 	}
16555c1cfac4SBill Paul 
16565c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16575c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16585c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16595c1cfac4SBill Paul 	}
16605c1cfac4SBill Paul }
16615c1cfac4SBill Paul 
1662e3d2833aSAlfred Perlstein static void
16630934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16645c1cfac4SBill Paul {
16655c1cfac4SBill Paul 	struct dc_mediainfo *m;
16665c1cfac4SBill Paul 
16670934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
166887f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
166987f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16705c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
167187f4fa15SMartin Blapp 		break;
167287f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16735c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
167487f4fa15SMartin Blapp 		break;
167587f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16765c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
167787f4fa15SMartin Blapp 		break;
167887f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16795c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
168087f4fa15SMartin Blapp 		break;
168187f4fa15SMartin Blapp 	default:
168287f4fa15SMartin Blapp 		break;
168387f4fa15SMartin Blapp 	}
16845c1cfac4SBill Paul 
168587f4fa15SMartin Blapp 	/*
168687f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
168787f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
168887f4fa15SMartin Blapp 	 * supply Media Specific Data.
168987f4fa15SMartin Blapp 	 */
169087f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16915c1cfac4SBill Paul 		m->dc_gp_len = 2;
169287f4fa15SMartin Blapp 		m->dc_gp_ptr =
169387f4fa15SMartin Blapp 		(u_int8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
169487f4fa15SMartin Blapp 	} else {
169587f4fa15SMartin Blapp 		m->dc_gp_len = 2;
169687f4fa15SMartin Blapp 		m->dc_gp_ptr =
169787f4fa15SMartin Blapp 		(u_int8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
169887f4fa15SMartin Blapp 	}
16995c1cfac4SBill Paul 
17005c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17015c1cfac4SBill Paul 	sc->dc_mi = m;
17025c1cfac4SBill Paul 
17035c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
17045c1cfac4SBill Paul }
17055c1cfac4SBill Paul 
1706e3d2833aSAlfred Perlstein static void
17070934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
17085c1cfac4SBill Paul {
17095c1cfac4SBill Paul 	struct dc_mediainfo *m;
17105c1cfac4SBill Paul 
17110934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17125c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
17135c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
17145c1cfac4SBill Paul 
17155c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
17165c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
17175c1cfac4SBill Paul 
17185c1cfac4SBill Paul 	m->dc_gp_len = 2;
17195c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
17205c1cfac4SBill Paul 
17215c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17225c1cfac4SBill Paul 	sc->dc_mi = m;
17235c1cfac4SBill Paul 
17245c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
17255c1cfac4SBill Paul }
17265c1cfac4SBill Paul 
1727e3d2833aSAlfred Perlstein static void
17280934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17295c1cfac4SBill Paul {
17305c1cfac4SBill Paul 	struct dc_mediainfo *m;
17310934f18aSMaxime Henrion 	u_int8_t *p;
17325c1cfac4SBill Paul 
17330934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17345c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17355c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17365c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17375c1cfac4SBill Paul 
17385c1cfac4SBill Paul 	p = (u_int8_t *)l;
17395c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17405c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17415c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17425c1cfac4SBill Paul 	m->dc_reset_len = *p;
17435c1cfac4SBill Paul 	p++;
17445c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17455c1cfac4SBill Paul 
17465c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17475c1cfac4SBill Paul 	sc->dc_mi = m;
17485c1cfac4SBill Paul }
17495c1cfac4SBill Paul 
17502c876e15SPoul-Henning Kamp static void
17510934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17523097aa70SWarner Losh {
17533097aa70SWarner Losh 	int size;
17543097aa70SWarner Losh 
17553097aa70SWarner Losh 	size = 2 << bits;
17563097aa70SWarner Losh 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
17573097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
17583097aa70SWarner Losh }
17593097aa70SWarner Losh 
1760e3d2833aSAlfred Perlstein static void
17610934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17625c1cfac4SBill Paul {
17635c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17645c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
17650934f18aSMaxime Henrion 	int have_mii, i, loff;
17665c1cfac4SBill Paul 	char *ptr;
17675c1cfac4SBill Paul 
1768f956e0b3SMartin Blapp 	have_mii = 0;
17695c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17705c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17715c1cfac4SBill Paul 
17725c1cfac4SBill Paul 	ptr = (char *)lhdr;
17735c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1774f956e0b3SMartin Blapp 	/*
1775f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1776f956e0b3SMartin Blapp 	 */
1777f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1778f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1779f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1780f956e0b3SMartin Blapp 		    have_mii++;
1781f956e0b3SMartin Blapp 
1782f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1783f956e0b3SMartin Blapp 		ptr++;
1784f956e0b3SMartin Blapp 	}
1785f956e0b3SMartin Blapp 
1786f956e0b3SMartin Blapp 	/*
1787f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1788f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1789f956e0b3SMartin Blapp 	 */
1790f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1791f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
17925c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17935c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17945c1cfac4SBill Paul 		switch (hdr->dc_type) {
17955c1cfac4SBill Paul 		case DC_EBLOCK_MII:
17965c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17975c1cfac4SBill Paul 			break;
17985c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1799f956e0b3SMartin Blapp 			if (! have_mii)
1800f956e0b3SMartin Blapp 				dc_decode_leaf_sia(sc,
1801f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
18025c1cfac4SBill Paul 			break;
18035c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1804f956e0b3SMartin Blapp 			if (! have_mii)
1805f956e0b3SMartin Blapp 				dc_decode_leaf_sym(sc,
1806f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
18075c1cfac4SBill Paul 			break;
18085c1cfac4SBill Paul 		default:
18095c1cfac4SBill Paul 			/* Don't care. Yet. */
18105c1cfac4SBill Paul 			break;
18115c1cfac4SBill Paul 		}
18125c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
18135c1cfac4SBill Paul 		ptr++;
18145c1cfac4SBill Paul 	}
18155c1cfac4SBill Paul }
18165c1cfac4SBill Paul 
181756e5e7aeSMaxime Henrion static void
181856e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
181956e5e7aeSMaxime Henrion {
182056e5e7aeSMaxime Henrion 	u_int32_t *paddr;
182156e5e7aeSMaxime Henrion 
182256e5e7aeSMaxime Henrion 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
182356e5e7aeSMaxime Henrion 	paddr = arg;
182456e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
182556e5e7aeSMaxime Henrion }
182656e5e7aeSMaxime Henrion 
182796f2e892SBill Paul /*
182896f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
182996f2e892SBill Paul  * setup and ethernet/BPF attach.
183096f2e892SBill Paul  */
1831e3d2833aSAlfred Perlstein static int
18320934f18aSMaxime Henrion dc_attach(device_t dev)
183396f2e892SBill Paul {
1834d1ce9105SBill Paul 	int tmp = 0;
183596f2e892SBill Paul 	u_char eaddr[ETHER_ADDR_LEN];
183696f2e892SBill Paul 	u_int32_t command;
183796f2e892SBill Paul 	struct dc_softc *sc;
183896f2e892SBill Paul 	struct ifnet *ifp;
183996f2e892SBill Paul 	u_int32_t revision;
184022f6205dSJohn Baldwin 	int error = 0, rid, mac_offset;
184156e5e7aeSMaxime Henrion 	int i;
1842e7b01d07SWarner Losh 	u_int8_t *mac;
184396f2e892SBill Paul 
184496f2e892SBill Paul 	sc = device_get_softc(dev);
184596f2e892SBill Paul 
18466008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1847c8b27acaSJohn Baldwin 	    MTX_DEF);
1848c3e7434fSWarner Losh 
184996f2e892SBill Paul 	/*
185096f2e892SBill Paul 	 * Map control/status registers.
185196f2e892SBill Paul 	 */
185207f65363SBill Paul 	pci_enable_busmaster(dev);
185396f2e892SBill Paul 
185496f2e892SBill Paul 	rid = DC_RID;
18555f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
185696f2e892SBill Paul 
185796f2e892SBill Paul 	if (sc->dc_res == NULL) {
185822f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
185996f2e892SBill Paul 		error = ENXIO;
1860608654d4SNate Lawson 		goto fail;
186196f2e892SBill Paul 	}
186296f2e892SBill Paul 
186396f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
186496f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
186596f2e892SBill Paul 
18660934f18aSMaxime Henrion 	/* Allocate interrupt. */
186754f1f1d1SNate Lawson 	rid = 0;
18685f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
186954f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
187054f1f1d1SNate Lawson 
187154f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
187222f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
187354f1f1d1SNate Lawson 		error = ENXIO;
187454f1f1d1SNate Lawson 		goto fail;
187554f1f1d1SNate Lawson 	}
187654f1f1d1SNate Lawson 
187796f2e892SBill Paul 	/* Need this info to decide on a chip type. */
187896f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
187996f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
188096f2e892SBill Paul 
18816d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
1882eecb3844SMartin Blapp 	if (sc->dc_info->dc_did != DC_DEVICEID_82C168 &&
1883eecb3844SMartin Blapp 	   sc->dc_info->dc_did != DC_DEVICEID_X3201)
1884eecb3844SMartin Blapp 		dc_eeprom_width(sc);
1885eecb3844SMartin Blapp 
188696f2e892SBill Paul 	switch (sc->dc_info->dc_did) {
188796f2e892SBill Paul 	case DC_DEVICEID_21143:
188896f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
188996f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1890042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
18915c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
18923097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
189396f2e892SBill Paul 		break;
189438deb45fSTom Rhodes 	case DC_DEVICEID_DM9009:
189596f2e892SBill Paul 	case DC_DEVICEID_DM9100:
189696f2e892SBill Paul 	case DC_DEVICEID_DM9102:
189796f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1898318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
1899318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
19007dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
19014a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
19020a46b1dcSBill Paul 		/* Increase the latency timer value. */
19030a46b1dcSBill Paul 		command = pci_read_config(dev, DC_PCI_CFLT, 4);
19040a46b1dcSBill Paul 		command &= 0xFFFF00FF;
19050a46b1dcSBill Paul 		command |= 0x00008000;
19060a46b1dcSBill Paul 		pci_write_config(dev, DC_PCI_CFLT, command, 4);
190796f2e892SBill Paul 		break;
190896f2e892SBill Paul 	case DC_DEVICEID_AL981:
190996f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
191096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
191196f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
191296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19133097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
191496f2e892SBill Paul 		break;
191596f2e892SBill Paul 	case DC_DEVICEID_AN985:
1916e351d778SMartin Blapp 	case DC_DEVICEID_ADM9511:
1917e351d778SMartin Blapp 	case DC_DEVICEID_ADM9513:
19184c16d09eSWarner Losh 	case DC_DEVICEID_FA511:
191941fced74SPeter Wemm 	case DC_DEVICEID_FE2500:
1920fa167b8eSBill Paul 	case DC_DEVICEID_EN2242:
1921948c244dSWarner Losh 	case DC_DEVICEID_HAWKING_PN672TX:
19227eac366bSMartin Blapp 	case DC_DEVICEID_3CSOHOB:
1923e7b9ab3aSBill Paul 	case DC_DEVICEID_MSMN120:
1924e7b9ab3aSBill Paul 	case DC_DEVICEID_MSMN130_FAKE: /* XXX avoid collision with PNIC*/
192596f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
1926acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
192796f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
192896f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
192996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
1930129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
193196f2e892SBill Paul 		break;
193296f2e892SBill Paul 	case DC_DEVICEID_98713:
193396f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
193496f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
193596f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
193696f2e892SBill Paul 		}
1937318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
193896f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1939318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1940318b02fdSBill Paul 		}
1941318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
194296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
194396f2e892SBill Paul 		break;
194496f2e892SBill Paul 	case DC_DEVICEID_987x5:
19459ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
194679d11e09SBill Paul 		/*
194779d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
194879d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
194979d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
195079d11e09SBill Paul 		 * get the right number of bits out of the
195179d11e09SBill Paul 		 * CRC routine.
195279d11e09SBill Paul 		 */
195379d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
195479d11e09SBill Paul 		    revision < DC_REVISION_98725)
195579d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
195696f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
195796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1958318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
195996f2e892SBill Paul 		break;
1960ead7cde9SBill Paul 	case DC_DEVICEID_98727:
1961ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1962ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1963ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
1964ead7cde9SBill Paul 		break;
196596f2e892SBill Paul 	case DC_DEVICEID_82C115:
196696f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
196779d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
1968318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
196996f2e892SBill Paul 		break;
197096f2e892SBill Paul 	case DC_DEVICEID_82C168:
197196f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
197291cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
197396f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
197496f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
197596f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
197696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
197796f2e892SBill Paul 		break;
197896f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
197996f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
198096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
198196f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
198296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
198396f2e892SBill Paul 		break;
1984feb78939SJonathan Chen 	case DC_DEVICEID_X3201:
1985feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
19862dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
19872dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
1988feb78939SJonathan Chen 		/*
1989feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
1990feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
19912dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
1992feb78939SJonathan Chen 		 */
19933097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
1994feb78939SJonathan Chen 		break;
19951af8bec7SBill Paul 	case DC_DEVICEID_RS7112:
19961af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
19971af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
19981af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19991af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
20003097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
20011af8bec7SBill Paul 		break;
200296f2e892SBill Paul 	default:
200322f6205dSJohn Baldwin 		device_printf(dev, "unknown device: %x\n", sc->dc_info->dc_did);
200496f2e892SBill Paul 		break;
200596f2e892SBill Paul 	}
200696f2e892SBill Paul 
200796f2e892SBill Paul 	/* Save the cache line size. */
200888d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
200988d739dcSBill Paul 		sc->dc_cachesize = 0;
201088d739dcSBill Paul 	else
201188d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
201288d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
201396f2e892SBill Paul 
201496f2e892SBill Paul 	/* Reset the adapter. */
201596f2e892SBill Paul 	dc_reset(sc);
201696f2e892SBill Paul 
201796f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2018feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
201996f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
202096f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
202196f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
202296f2e892SBill Paul 	}
202396f2e892SBill Paul 
202496f2e892SBill Paul 	/*
202596f2e892SBill Paul 	 * Try to learn something about the supported media.
202696f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
202796f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
202896f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
202996f2e892SBill Paul 	 * Intel 21143.
203096f2e892SBill Paul 	 */
20315c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
20325c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
20335c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
203496f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
203596f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
203696f2e892SBill Paul 		else
203796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
203896f2e892SBill Paul 	} else if (!sc->dc_pmode)
203996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
204096f2e892SBill Paul 
204196f2e892SBill Paul 	/*
204296f2e892SBill Paul 	 * Get station address from the EEPROM.
204396f2e892SBill Paul 	 */
204496f2e892SBill Paul 	switch(sc->dc_type) {
204596f2e892SBill Paul 	case DC_TYPE_98713:
204696f2e892SBill Paul 	case DC_TYPE_98713A:
204796f2e892SBill Paul 	case DC_TYPE_987x5:
204896f2e892SBill Paul 	case DC_TYPE_PNICII:
204996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
205096f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
205196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
205296f2e892SBill Paul 		break;
205396f2e892SBill Paul 	case DC_TYPE_PNIC:
205496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
205596f2e892SBill Paul 		break;
205696f2e892SBill Paul 	case DC_TYPE_DM9102:
2057ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2058ec6a7299SMaxime Henrion #ifdef __sparc64__
2059ec6a7299SMaxime Henrion 		/*
2060ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2061ec6a7299SMaxime Henrion 		 * the EEPROM is all zero and we have to get it from the fcode.
2062ec6a7299SMaxime Henrion 		 */
2063ec6a7299SMaxime Henrion 		for (i = 0; i < ETHER_ADDR_LEN; i++)
2064ec6a7299SMaxime Henrion 			if (eaddr[i] != 0x00)
2065ec6a7299SMaxime Henrion 				break;
2066b7b6c9e6SMarius Strobl 		if (i >= ETHER_ADDR_LEN)
2067ec6a7299SMaxime Henrion 			OF_getetheraddr(dev, eaddr);
2068ec6a7299SMaxime Henrion #endif
2069ec6a7299SMaxime Henrion 		break;
207096f2e892SBill Paul 	case DC_TYPE_21143:
207196f2e892SBill Paul 	case DC_TYPE_ASIX:
207296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
207396f2e892SBill Paul 		break;
207496f2e892SBill Paul 	case DC_TYPE_AL981:
207596f2e892SBill Paul 	case DC_TYPE_AN985:
2076129eaf79SMartin Blapp 		*(u_int32_t *)(&eaddr[0]) = CSR_READ_4(sc, DC_AL_PAR0);
2077129eaf79SMartin Blapp 		*(u_int16_t *)(&eaddr[4]) = CSR_READ_4(sc, DC_AL_PAR1);
207896f2e892SBill Paul 		break;
20791af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
20800934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
20810934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
20821af8bec7SBill Paul 		break;
2083feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
20840934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2085e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2086e7b01d07SWarner Losh 		if (!mac) {
2087e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2088608654d4SNate Lawson 			error = ENXIO;
2089e7b01d07SWarner Losh 			goto fail;
2090e7b01d07SWarner Losh 		}
2091e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2092feb78939SJonathan Chen 		break;
209396f2e892SBill Paul 	default:
209496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
209596f2e892SBill Paul 		break;
209696f2e892SBill Paul 	}
209796f2e892SBill Paul 
209856e5e7aeSMaxime Henrion 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
209956e5e7aeSMaxime Henrion 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
210056e5e7aeSMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct dc_list_data), 1,
210156e5e7aeSMaxime Henrion 	    sizeof(struct dc_list_data), 0, NULL, NULL, &sc->dc_ltag);
210256e5e7aeSMaxime Henrion 	if (error) {
210322f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
210456e5e7aeSMaxime Henrion 		error = ENXIO;
210556e5e7aeSMaxime Henrion 		goto fail;
210656e5e7aeSMaxime Henrion 	}
210756e5e7aeSMaxime Henrion 	error = bus_dmamem_alloc(sc->dc_ltag, (void **)&sc->dc_ldata,
2108aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->dc_lmap);
210956e5e7aeSMaxime Henrion 	if (error) {
211022f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate DMA safe memory\n");
211156e5e7aeSMaxime Henrion 		error = ENXIO;
211256e5e7aeSMaxime Henrion 		goto fail;
211356e5e7aeSMaxime Henrion 	}
211456e5e7aeSMaxime Henrion 	error = bus_dmamap_load(sc->dc_ltag, sc->dc_lmap, sc->dc_ldata,
211556e5e7aeSMaxime Henrion 	    sizeof(struct dc_list_data), dc_dma_map_addr, &sc->dc_laddr,
211656e5e7aeSMaxime Henrion 	    BUS_DMA_NOWAIT);
211756e5e7aeSMaxime Henrion 	if (error) {
211822f6205dSJohn Baldwin 		device_printf(dev, "cannot get address of the descriptors\n");
211956e5e7aeSMaxime Henrion 		error = ENXIO;
212056e5e7aeSMaxime Henrion 		goto fail;
212156e5e7aeSMaxime Henrion 	}
212296f2e892SBill Paul 
212356e5e7aeSMaxime Henrion 	/*
212456e5e7aeSMaxime Henrion 	 * Allocate a busdma tag and DMA safe memory for the multicast
212556e5e7aeSMaxime Henrion 	 * setup frame.
212656e5e7aeSMaxime Henrion 	 */
212756e5e7aeSMaxime Henrion 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
212856e5e7aeSMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1,
212956e5e7aeSMaxime Henrion 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 0, NULL, NULL, &sc->dc_stag);
213056e5e7aeSMaxime Henrion 	if (error) {
213122f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
213256e5e7aeSMaxime Henrion 		error = ENXIO;
213356e5e7aeSMaxime Henrion 		goto fail;
213456e5e7aeSMaxime Henrion 	}
213556e5e7aeSMaxime Henrion 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
213656e5e7aeSMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->dc_smap);
213756e5e7aeSMaxime Henrion 	if (error) {
213822f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate DMA safe memory\n");
213956e5e7aeSMaxime Henrion 		error = ENXIO;
214056e5e7aeSMaxime Henrion 		goto fail;
214156e5e7aeSMaxime Henrion 	}
214256e5e7aeSMaxime Henrion 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
214356e5e7aeSMaxime Henrion 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
214456e5e7aeSMaxime Henrion 	if (error) {
214522f6205dSJohn Baldwin 		device_printf(dev, "cannot get address of the descriptors\n");
214696f2e892SBill Paul 		error = ENXIO;
214796f2e892SBill Paul 		goto fail;
214896f2e892SBill Paul 	}
214996f2e892SBill Paul 
215056e5e7aeSMaxime Henrion 	/* Allocate a busdma tag for mbufs. */
2151c1b677aaSScott Long 	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
2152ab0d8702SScott Long 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, DC_TX_LIST_CNT, MCLBYTES,
2153c1b677aaSScott Long 	    0, NULL, NULL, &sc->dc_mtag);
215456e5e7aeSMaxime Henrion 	if (error) {
215522f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
215656e5e7aeSMaxime Henrion 		error = ENXIO;
215756e5e7aeSMaxime Henrion 		goto fail;
215856e5e7aeSMaxime Henrion 	}
215956e5e7aeSMaxime Henrion 
216056e5e7aeSMaxime Henrion 	/* Create the TX/RX busdma maps. */
216156e5e7aeSMaxime Henrion 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
216256e5e7aeSMaxime Henrion 		error = bus_dmamap_create(sc->dc_mtag, 0,
216356e5e7aeSMaxime Henrion 		    &sc->dc_cdata.dc_tx_map[i]);
216456e5e7aeSMaxime Henrion 		if (error) {
216522f6205dSJohn Baldwin 			device_printf(dev, "failed to init TX ring\n");
216656e5e7aeSMaxime Henrion 			error = ENXIO;
216756e5e7aeSMaxime Henrion 			goto fail;
216856e5e7aeSMaxime Henrion 		}
216956e5e7aeSMaxime Henrion 	}
217056e5e7aeSMaxime Henrion 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
217156e5e7aeSMaxime Henrion 		error = bus_dmamap_create(sc->dc_mtag, 0,
217256e5e7aeSMaxime Henrion 		    &sc->dc_cdata.dc_rx_map[i]);
217356e5e7aeSMaxime Henrion 		if (error) {
217422f6205dSJohn Baldwin 			device_printf(dev, "failed to init RX ring\n");
217556e5e7aeSMaxime Henrion 			error = ENXIO;
217656e5e7aeSMaxime Henrion 			goto fail;
217756e5e7aeSMaxime Henrion 		}
217856e5e7aeSMaxime Henrion 	}
217956e5e7aeSMaxime Henrion 	error = bus_dmamap_create(sc->dc_mtag, 0, &sc->dc_sparemap);
218056e5e7aeSMaxime Henrion 	if (error) {
218122f6205dSJohn Baldwin 		device_printf(dev, "failed to init RX ring\n");
218256e5e7aeSMaxime Henrion 		error = ENXIO;
218356e5e7aeSMaxime Henrion 		goto fail;
218456e5e7aeSMaxime Henrion 	}
218596f2e892SBill Paul 
2186fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2187fc74a9f9SBrooks Davis 	if (ifp == NULL) {
218822f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2189fc74a9f9SBrooks Davis 		error = ENOSPC;
2190fc74a9f9SBrooks Davis 		goto fail;
2191fc74a9f9SBrooks Davis 	}
219296f2e892SBill Paul 	ifp->if_softc = sc;
21939bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2194feb78939SJonathan Chen 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
219596f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
21963d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
219796f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
219896f2e892SBill Paul 	ifp->if_start = dc_start;
219996f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
220096f2e892SBill Paul 	ifp->if_init = dc_init;
220196f2e892SBill Paul 	ifp->if_baudrate = 10000000;
2202cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2203cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2204cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
220596f2e892SBill Paul 
220696f2e892SBill Paul 	/*
22075c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
22085c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
22095c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
22105c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
22115c1cfac4SBill Paul 	 * driver instead.
221296f2e892SBill Paul 	 */
22135c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
22145c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
22155c1cfac4SBill Paul 		tmp = sc->dc_pmode;
22165c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
22175c1cfac4SBill Paul 	}
22185c1cfac4SBill Paul 
22196d431b17SWarner Losh 	/*
22206d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
22216d431b17SWarner Losh 	 * to the MII.  This needs to be done before mii_phy_probe so that
22226d431b17SWarner Losh 	 * we can actually see them.
22236d431b17SWarner Losh 	 */
22246d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
22256d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
22266d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
22276d431b17SWarner Losh 		DELAY(10);
22286d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
22296d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
22306d431b17SWarner Losh 		DELAY(10);
22316d431b17SWarner Losh 	}
22326d431b17SWarner Losh 
223396f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
223496f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
223596f2e892SBill Paul 
223696f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
22375c1cfac4SBill Paul 		sc->dc_pmode = tmp;
22385c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
223996f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2240042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
224196f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
224296f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
224378999dd1SBill Paul 		/*
224478999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
224578999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
224678999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
224778999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
224878999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
224978999dd1SBill Paul 		 */
225078999dd1SBill Paul 		if (pci_read_config(dev, DC_PCI_CSID, 4) != 0x80281033)
225178999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
225296f2e892SBill Paul 		error = 0;
225396f2e892SBill Paul 	}
225496f2e892SBill Paul 
225596f2e892SBill Paul 	if (error) {
225622f6205dSJohn Baldwin 		device_printf(dev, "MII without any PHY!\n");
225796f2e892SBill Paul 		goto fail;
225896f2e892SBill Paul 	}
225996f2e892SBill Paul 
2260028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2261028a8491SMartin Blapp 		/*
2262028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2263028a8491SMartin Blapp 		 */
2264028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2265028a8491SMartin Blapp 	}
2266028a8491SMartin Blapp 
226796f2e892SBill Paul 	/*
2268db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2269db40c1aeSDoug Ambrisko 	 */
2270db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
22719ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
227240929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2273e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2274e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2275e695984eSRuslan Ermilov #endif
2276db40c1aeSDoug Ambrisko 
2277c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
227896f2e892SBill Paul 
22795c1cfac4SBill Paul #ifdef SRM_MEDIA
2280510a809eSMike Smith 	sc->dc_srm_media = 0;
2281510a809eSMike Smith 
2282510a809eSMike Smith 	/* Remember the SRM console media setting */
2283510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
2284510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
2285510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
2286510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
2287510a809eSMike Smith 		case 3:
2288510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
2289510a809eSMike Smith 			break;
2290510a809eSMike Smith 		case 4:
2291510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
2292510a809eSMike Smith 			break;
2293510a809eSMike Smith 		case 5:
2294510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
2295510a809eSMike Smith 			break;
2296510a809eSMike Smith 		case 6:
2297510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
2298510a809eSMike Smith 			break;
2299510a809eSMike Smith 		}
2300510a809eSMike Smith 		if (sc->dc_srm_media)
2301510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
2302510a809eSMike Smith 	}
2303510a809eSMike Smith #endif
2304510a809eSMike Smith 
2305608654d4SNate Lawson 	/*
2306608654d4SNate Lawson 	 * Call MI attach routine.
2307608654d4SNate Lawson 	 */
2308608654d4SNate Lawson 	ether_ifattach(ifp, eaddr);
2309608654d4SNate Lawson 
231054f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2311c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2312608654d4SNate Lawson 	    dc_intr, sc, &sc->dc_intrhand);
2313608654d4SNate Lawson 
2314608654d4SNate Lawson 	if (error) {
231522f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2316693f4477SNate Lawson 		ether_ifdetach(ifp);
231754f1f1d1SNate Lawson 		goto fail;
2318608654d4SNate Lawson 	}
2319510a809eSMike Smith 
232096f2e892SBill Paul fail:
232154f1f1d1SNate Lawson 	if (error)
232254f1f1d1SNate Lawson 		dc_detach(dev);
232396f2e892SBill Paul 	return (error);
232496f2e892SBill Paul }
232596f2e892SBill Paul 
2326693f4477SNate Lawson /*
2327693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2328693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2329693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2330693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2331693f4477SNate Lawson  * allocated.
2332693f4477SNate Lawson  */
2333e3d2833aSAlfred Perlstein static int
23340934f18aSMaxime Henrion dc_detach(device_t dev)
233596f2e892SBill Paul {
233696f2e892SBill Paul 	struct dc_softc *sc;
233796f2e892SBill Paul 	struct ifnet *ifp;
23385c1cfac4SBill Paul 	struct dc_mediainfo *m;
233956e5e7aeSMaxime Henrion 	int i;
234096f2e892SBill Paul 
234196f2e892SBill Paul 	sc = device_get_softc(dev);
234259f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2343d1ce9105SBill Paul 
2344fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
234596f2e892SBill Paul 
234640929967SGleb Smirnoff #ifdef DEVICE_POLLING
234740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
234840929967SGleb Smirnoff 		ether_poll_deregister(ifp);
234940929967SGleb Smirnoff #endif
235040929967SGleb Smirnoff 
2351693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2352214073e5SWarner Losh 	if (device_is_attached(dev)) {
2353c8b27acaSJohn Baldwin 		DC_LOCK(sc);
235496f2e892SBill Paul 		dc_stop(sc);
2355c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2356c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
23579ef8b520SSam Leffler 		ether_ifdetach(ifp);
2358693f4477SNate Lawson 	}
23593badaceeSRuslan Ermilov 	if (ifp)
23603badaceeSRuslan Ermilov 		if_free(ifp);
2361693f4477SNate Lawson 	if (sc->dc_miibus)
236296f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
236354f1f1d1SNate Lawson 	bus_generic_detach(dev);
236496f2e892SBill Paul 
236554f1f1d1SNate Lawson 	if (sc->dc_intrhand)
236696f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
236754f1f1d1SNate Lawson 	if (sc->dc_irq)
236896f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
236954f1f1d1SNate Lawson 	if (sc->dc_res)
237096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
237196f2e892SBill Paul 
237256e5e7aeSMaxime Henrion 	if (sc->dc_cdata.dc_sbuf != NULL)
237356e5e7aeSMaxime Henrion 		bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf, sc->dc_smap);
237456e5e7aeSMaxime Henrion 	if (sc->dc_ldata != NULL)
237556e5e7aeSMaxime Henrion 		bus_dmamem_free(sc->dc_ltag, sc->dc_ldata, sc->dc_lmap);
23764f867c2dSGiorgos Keramidas 	if (sc->dc_mtag) {
237756e5e7aeSMaxime Henrion 		for (i = 0; i < DC_TX_LIST_CNT; i++)
23784f867c2dSGiorgos Keramidas 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
23794f867c2dSGiorgos Keramidas 				bus_dmamap_destroy(sc->dc_mtag,
23804f867c2dSGiorgos Keramidas 				    sc->dc_cdata.dc_tx_map[i]);
238156e5e7aeSMaxime Henrion 		for (i = 0; i < DC_RX_LIST_CNT; i++)
23824f867c2dSGiorgos Keramidas 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
23834f867c2dSGiorgos Keramidas 				bus_dmamap_destroy(sc->dc_mtag,
23844f867c2dSGiorgos Keramidas 				    sc->dc_cdata.dc_rx_map[i]);
238556e5e7aeSMaxime Henrion 		bus_dmamap_destroy(sc->dc_mtag, sc->dc_sparemap);
23864f867c2dSGiorgos Keramidas 	}
238756e5e7aeSMaxime Henrion 	if (sc->dc_stag)
238856e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_stag);
238956e5e7aeSMaxime Henrion 	if (sc->dc_mtag)
239056e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_mtag);
239156e5e7aeSMaxime Henrion 	if (sc->dc_ltag)
239256e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_ltag);
239356e5e7aeSMaxime Henrion 
239496f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
239596f2e892SBill Paul 
23965c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
23975c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
23985c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
23995c1cfac4SBill Paul 		sc->dc_mi = m;
24005c1cfac4SBill Paul 	}
24017efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
24025c1cfac4SBill Paul 
2403d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
240496f2e892SBill Paul 
240596f2e892SBill Paul 	return (0);
240696f2e892SBill Paul }
240796f2e892SBill Paul 
240896f2e892SBill Paul /*
240996f2e892SBill Paul  * Initialize the transmit descriptors.
241096f2e892SBill Paul  */
2411e3d2833aSAlfred Perlstein static int
24120934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
241396f2e892SBill Paul {
241496f2e892SBill Paul 	struct dc_chain_data *cd;
241596f2e892SBill Paul 	struct dc_list_data *ld;
241601faf54bSLuigi Rizzo 	int i, nexti;
241796f2e892SBill Paul 
241896f2e892SBill Paul 	cd = &sc->dc_cdata;
241996f2e892SBill Paul 	ld = sc->dc_ldata;
242096f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2421b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2422b3811c95SMaxime Henrion 			nexti = 0;
2423b3811c95SMaxime Henrion 		else
2424b3811c95SMaxime Henrion 			nexti = i + 1;
2425af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
242696f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
242796f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
242896f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
242996f2e892SBill Paul 	}
243096f2e892SBill Paul 
243196f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
243256e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
243356e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
243496f2e892SBill Paul 	return (0);
243596f2e892SBill Paul }
243696f2e892SBill Paul 
243796f2e892SBill Paul 
243896f2e892SBill Paul /*
243996f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
244096f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
244196f2e892SBill Paul  * points back to the first.
244296f2e892SBill Paul  */
2443e3d2833aSAlfred Perlstein static int
24440934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
244596f2e892SBill Paul {
244696f2e892SBill Paul 	struct dc_chain_data *cd;
244796f2e892SBill Paul 	struct dc_list_data *ld;
244801faf54bSLuigi Rizzo 	int i, nexti;
244996f2e892SBill Paul 
245096f2e892SBill Paul 	cd = &sc->dc_cdata;
245196f2e892SBill Paul 	ld = sc->dc_ldata;
245296f2e892SBill Paul 
245396f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
245456e5e7aeSMaxime Henrion 		if (dc_newbuf(sc, i, 1) != 0)
245596f2e892SBill Paul 			return (ENOBUFS);
2456b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2457b3811c95SMaxime Henrion 			nexti = 0;
2458b3811c95SMaxime Henrion 		else
2459b3811c95SMaxime Henrion 			nexti = i + 1;
2460af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
246196f2e892SBill Paul 	}
246296f2e892SBill Paul 
246396f2e892SBill Paul 	cd->dc_rx_prod = 0;
246456e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
246556e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
246696f2e892SBill Paul 	return (0);
246796f2e892SBill Paul }
246896f2e892SBill Paul 
246956e5e7aeSMaxime Henrion static void
247056e5e7aeSMaxime Henrion dc_dma_map_rxbuf(arg, segs, nseg, mapsize, error)
247156e5e7aeSMaxime Henrion 	void *arg;
247256e5e7aeSMaxime Henrion 	bus_dma_segment_t *segs;
247356e5e7aeSMaxime Henrion 	int nseg;
247456e5e7aeSMaxime Henrion 	bus_size_t mapsize;
247556e5e7aeSMaxime Henrion 	int error;
247656e5e7aeSMaxime Henrion {
247756e5e7aeSMaxime Henrion 	struct dc_softc *sc;
247856e5e7aeSMaxime Henrion 	struct dc_desc *c;
247956e5e7aeSMaxime Henrion 
248056e5e7aeSMaxime Henrion 	sc = arg;
248156e5e7aeSMaxime Henrion 	c = &sc->dc_ldata->dc_rx_list[sc->dc_cdata.dc_rx_cur];
248256e5e7aeSMaxime Henrion 	if (error) {
248356e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_err = error;
248456e5e7aeSMaxime Henrion 		return;
248556e5e7aeSMaxime Henrion 	}
248656e5e7aeSMaxime Henrion 
248756e5e7aeSMaxime Henrion 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
248856e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_rx_err = 0;
2489af4358c7SMaxime Henrion 	c->dc_data = htole32(segs->ds_addr);
249056e5e7aeSMaxime Henrion }
249156e5e7aeSMaxime Henrion 
249296f2e892SBill Paul /*
249396f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
249496f2e892SBill Paul  */
2495e3d2833aSAlfred Perlstein static int
249656e5e7aeSMaxime Henrion dc_newbuf(struct dc_softc *sc, int i, int alloc)
249796f2e892SBill Paul {
249856e5e7aeSMaxime Henrion 	struct mbuf *m_new;
249956e5e7aeSMaxime Henrion 	bus_dmamap_t tmp;
250056e5e7aeSMaxime Henrion 	int error;
250196f2e892SBill Paul 
250256e5e7aeSMaxime Henrion 	if (alloc) {
250356e5e7aeSMaxime Henrion 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
250440129585SLuigi Rizzo 		if (m_new == NULL)
250596f2e892SBill Paul 			return (ENOBUFS);
250696f2e892SBill Paul 	} else {
250756e5e7aeSMaxime Henrion 		m_new = sc->dc_cdata.dc_rx_chain[i];
250896f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
250996f2e892SBill Paul 	}
251056e5e7aeSMaxime Henrion 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
251196f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
251296f2e892SBill Paul 
251396f2e892SBill Paul 	/*
251496f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
251596f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
251696f2e892SBill Paul 	 * 82c169 chips.
251796f2e892SBill Paul 	 */
251896f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
25190934f18aSMaxime Henrion 		bzero(mtod(m_new, char *), m_new->m_len);
252096f2e892SBill Paul 
252156e5e7aeSMaxime Henrion 	/* No need to remap the mbuf if we're reusing it. */
252256e5e7aeSMaxime Henrion 	if (alloc) {
252356e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_cur = i;
252456e5e7aeSMaxime Henrion 		error = bus_dmamap_load_mbuf(sc->dc_mtag, sc->dc_sparemap,
252556e5e7aeSMaxime Henrion 		    m_new, dc_dma_map_rxbuf, sc, 0);
252656e5e7aeSMaxime Henrion 		if (error) {
252756e5e7aeSMaxime Henrion 			m_freem(m_new);
252856e5e7aeSMaxime Henrion 			return (error);
252956e5e7aeSMaxime Henrion 		}
253056e5e7aeSMaxime Henrion 		if (sc->dc_cdata.dc_rx_err != 0) {
253156e5e7aeSMaxime Henrion 			m_freem(m_new);
253256e5e7aeSMaxime Henrion 			return (sc->dc_cdata.dc_rx_err);
253356e5e7aeSMaxime Henrion 		}
253456e5e7aeSMaxime Henrion 		bus_dmamap_unload(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i]);
253556e5e7aeSMaxime Henrion 		tmp = sc->dc_cdata.dc_rx_map[i];
253656e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
253756e5e7aeSMaxime Henrion 		sc->dc_sparemap = tmp;
253896f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = m_new;
253956e5e7aeSMaxime Henrion 	}
254096f2e892SBill Paul 
2541af4358c7SMaxime Henrion 	sc->dc_ldata->dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
2542af4358c7SMaxime Henrion 	sc->dc_ldata->dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
254356e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
254456e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
254556e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
254656e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
254796f2e892SBill Paul 	return (0);
254896f2e892SBill Paul }
254996f2e892SBill Paul 
255096f2e892SBill Paul /*
255196f2e892SBill Paul  * Grrrrr.
255296f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
255396f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
255496f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
255596f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
255696f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
255796f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
255896f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
255996f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
256096f2e892SBill Paul  *
256196f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
256296f2e892SBill Paul  * Here's what we know:
256396f2e892SBill Paul  *
256496f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
256596f2e892SBill Paul  *   descriptors uploaded.
256696f2e892SBill Paul  *
256796f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
256896f2e892SBill Paul  *   total data upload.
256996f2e892SBill Paul  *
257096f2e892SBill Paul  * - We know the size of the desired received frame because it will be
257196f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
257296f2e892SBill Paul  *
257396f2e892SBill Paul  * Here's what we do:
257496f2e892SBill Paul  *
257596f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
257696f2e892SBill Paul  *   This means that we know that the buffer contents should be all
257796f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
257896f2e892SBill Paul  *
257996f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
258096f2e892SBill Paul  *   ethernet CRC at the end.
258196f2e892SBill Paul  *
258296f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
258396f2e892SBill Paul  *
258496f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
258596f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
258696f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
258796f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
258896f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
258996f2e892SBill Paul  *   we won't be fooled.
259096f2e892SBill Paul  *
259196f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
259296f2e892SBill Paul  *   that value from the current pointer location. This brings us
259396f2e892SBill Paul  *   to the start of the actual received packet.
259496f2e892SBill Paul  *
259596f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
259696f2e892SBill Paul  *   frame length.
259796f2e892SBill Paul  *
259896f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
259996f2e892SBill Paul  * the time.
260096f2e892SBill Paul  */
260196f2e892SBill Paul 
260296f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2603e3d2833aSAlfred Perlstein static void
26040934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
260596f2e892SBill Paul {
260696f2e892SBill Paul 	struct dc_desc *cur_rx;
260796f2e892SBill Paul 	struct dc_desc *c = NULL;
260896f2e892SBill Paul 	struct mbuf *m = NULL;
260996f2e892SBill Paul 	unsigned char *ptr;
261096f2e892SBill Paul 	int i, total_len;
261196f2e892SBill Paul 	u_int32_t rxstat = 0;
261296f2e892SBill Paul 
261396f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
261496f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
261596f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
26161edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
261796f2e892SBill Paul 
261896f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
261996f2e892SBill Paul 	while (1) {
262096f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
2621af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
262296f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
262396f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
262496f2e892SBill Paul 		ptr += DC_RXLEN;
262596f2e892SBill Paul 		/* If this is the last buffer, break out. */
262696f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
262796f2e892SBill Paul 			break;
262856e5e7aeSMaxime Henrion 		dc_newbuf(sc, i, 0);
262996f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
263096f2e892SBill Paul 	}
263196f2e892SBill Paul 
263296f2e892SBill Paul 	/* Find the length of the actual receive frame. */
263396f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
263496f2e892SBill Paul 
263596f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
263696f2e892SBill Paul 	while (*ptr == 0x00)
263796f2e892SBill Paul 		ptr--;
263896f2e892SBill Paul 
263996f2e892SBill Paul 	/* Round off. */
264096f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
264196f2e892SBill Paul 		ptr -= 1;
264296f2e892SBill Paul 
264396f2e892SBill Paul 	/* Now find the start of the frame. */
264496f2e892SBill Paul 	ptr -= total_len;
264596f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
264696f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
264796f2e892SBill Paul 
264896f2e892SBill Paul 	/*
264996f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
265096f2e892SBill Paul 	 * the status word to make it look like a successful
265196f2e892SBill Paul 	 * frame reception.
265296f2e892SBill Paul 	 */
265356e5e7aeSMaxime Henrion 	dc_newbuf(sc, i, 0);
265496f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2655af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
265696f2e892SBill Paul }
265796f2e892SBill Paul 
265896f2e892SBill Paul /*
265973bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
266073bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
266173bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
266273bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
266373bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
266473bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
266573bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
266673bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
266773bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
266873bf949cSBill Paul  */
2669e3d2833aSAlfred Perlstein static int
26700934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
267173bf949cSBill Paul {
267273bf949cSBill Paul 	struct dc_desc *cur_rx;
26730934f18aSMaxime Henrion 	int i, pos;
267473bf949cSBill Paul 
267573bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
267673bf949cSBill Paul 
267773bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
267873bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
2679af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
268073bf949cSBill Paul 			break;
268173bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
268273bf949cSBill Paul 	}
268373bf949cSBill Paul 
268473bf949cSBill Paul 	/* If the ring really is empty, then just return. */
268573bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
268673bf949cSBill Paul 		return (0);
268773bf949cSBill Paul 
268873bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
268973bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
269073bf949cSBill Paul 
269173bf949cSBill Paul 	return (EAGAIN);
269273bf949cSBill Paul }
269373bf949cSBill Paul 
269473bf949cSBill Paul /*
269596f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
269696f2e892SBill Paul  * the higher level protocols.
269796f2e892SBill Paul  */
2698e3d2833aSAlfred Perlstein static void
26990934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
270096f2e892SBill Paul {
270196f2e892SBill Paul 	struct mbuf *m;
270296f2e892SBill Paul 	struct ifnet *ifp;
270396f2e892SBill Paul 	struct dc_desc *cur_rx;
270496f2e892SBill Paul 	int i, total_len = 0;
270596f2e892SBill Paul 	u_int32_t rxstat;
270696f2e892SBill Paul 
27075120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
27085120abbfSSam Leffler 
2709fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
271096f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
271196f2e892SBill Paul 
271256e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
2713af4358c7SMaxime Henrion 	while (!(le32toh(sc->dc_ldata->dc_rx_list[i].dc_status) &
2714af4358c7SMaxime Henrion 	    DC_RXSTAT_OWN)) {
2715e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
271640929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2717e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2718e4fc250cSLuigi Rizzo 				break;
2719e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2720e4fc250cSLuigi Rizzo 		}
27210934f18aSMaxime Henrion #endif
272296f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
2723af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
272496f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
272556e5e7aeSMaxime Henrion 		bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
272656e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
272796f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
272896f2e892SBill Paul 
272996f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
273096f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
273196f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
273296f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
273396f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
273496f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
273596f2e892SBill Paul 					continue;
273696f2e892SBill Paul 				}
273796f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2738af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
273996f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
274096f2e892SBill Paul 			}
274196f2e892SBill Paul 		}
274296f2e892SBill Paul 
274396f2e892SBill Paul 		/*
274496f2e892SBill Paul 		 * If an error occurs, update stats, clear the
274596f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
274696f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2747db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
27480934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
274996f2e892SBill Paul 		 */
2750db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2751db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2752db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2753db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2754db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
275596f2e892SBill Paul 				ifp->if_ierrors++;
275696f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
275796f2e892SBill Paul 					ifp->if_collisions++;
275856e5e7aeSMaxime Henrion 				dc_newbuf(sc, i, 0);
275996f2e892SBill Paul 				if (rxstat & DC_RXSTAT_CRCERR) {
276096f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
276196f2e892SBill Paul 					continue;
276296f2e892SBill Paul 				} else {
2763c8b27acaSJohn Baldwin 					dc_init_locked(sc);
276496f2e892SBill Paul 					return;
276596f2e892SBill Paul 				}
276696f2e892SBill Paul 			}
2767db40c1aeSDoug Ambrisko 		}
276896f2e892SBill Paul 
276996f2e892SBill Paul 		/* No errors; receive the packet. */
277096f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
277101faf54bSLuigi Rizzo #ifdef __i386__
277201faf54bSLuigi Rizzo 		/*
277301faf54bSLuigi Rizzo 		 * On the x86 we do not have alignment problems, so try to
277401faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
277501faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
277601faf54bSLuigi Rizzo 		 * copy done in m_devget().
277701faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
277801faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
277901faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
278001faf54bSLuigi Rizzo 		 */
278156e5e7aeSMaxime Henrion 		if (dc_quick && dc_newbuf(sc, i, 1) == 0) {
278201faf54bSLuigi Rizzo 			m->m_pkthdr.rcvif = ifp;
278301faf54bSLuigi Rizzo 			m->m_pkthdr.len = m->m_len = total_len;
278401faf54bSLuigi Rizzo 			DC_INC(i, DC_RX_LIST_CNT);
278501faf54bSLuigi Rizzo 		} else
278601faf54bSLuigi Rizzo #endif
278701faf54bSLuigi Rizzo 		{
278801faf54bSLuigi Rizzo 			struct mbuf *m0;
278996f2e892SBill Paul 
279001faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
279101faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
279256e5e7aeSMaxime Henrion 			dc_newbuf(sc, i, 0);
279396f2e892SBill Paul 			DC_INC(i, DC_RX_LIST_CNT);
279496f2e892SBill Paul 			if (m0 == NULL) {
279596f2e892SBill Paul 				ifp->if_ierrors++;
279696f2e892SBill Paul 				continue;
279796f2e892SBill Paul 			}
279896f2e892SBill Paul 			m = m0;
279901faf54bSLuigi Rizzo 		}
280096f2e892SBill Paul 
280196f2e892SBill Paul 		ifp->if_ipackets++;
28025120abbfSSam Leffler 		DC_UNLOCK(sc);
28039ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
28045120abbfSSam Leffler 		DC_LOCK(sc);
280596f2e892SBill Paul 	}
280696f2e892SBill Paul 
280796f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
280896f2e892SBill Paul }
280996f2e892SBill Paul 
281096f2e892SBill Paul /*
281196f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
281296f2e892SBill Paul  * the list buffers.
281396f2e892SBill Paul  */
281496f2e892SBill Paul 
2815e3d2833aSAlfred Perlstein static void
28160934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
281796f2e892SBill Paul {
281896f2e892SBill Paul 	struct dc_desc *cur_tx = NULL;
281996f2e892SBill Paul 	struct ifnet *ifp;
282096f2e892SBill Paul 	int idx;
2821af4358c7SMaxime Henrion 	u_int32_t ctl, txstat;
282296f2e892SBill Paul 
2823fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
282496f2e892SBill Paul 
282596f2e892SBill Paul 	/*
282696f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
282796f2e892SBill Paul 	 * frames that have been transmitted.
282896f2e892SBill Paul 	 */
282956e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
283096f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
283196f2e892SBill Paul 	while (idx != sc->dc_cdata.dc_tx_prod) {
283296f2e892SBill Paul 
283396f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
2834af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
2835af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
283696f2e892SBill Paul 
283796f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
283896f2e892SBill Paul 			break;
283996f2e892SBill Paul 
28404ff4a9beSDon Lewis 		if (!(ctl & DC_TXCTL_LASTFRAG) || ctl & DC_TXCTL_SETUP) {
2841af4358c7SMaxime Henrion 			if (ctl & DC_TXCTL_SETUP) {
284296f2e892SBill Paul 				/*
284396f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
284496f2e892SBill Paul 				 * that it will sometimes generate a TX
284596f2e892SBill Paul 				 * underrun error while DMAing the RX
284696f2e892SBill Paul 				 * filter setup frame. If we detect this,
284796f2e892SBill Paul 				 * we have to send the setup frame again,
284896f2e892SBill Paul 				 * or else the filter won't be programmed
284996f2e892SBill Paul 				 * correctly.
285096f2e892SBill Paul 				 */
285196f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
285296f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
285396f2e892SBill Paul 						dc_setfilt(sc);
285496f2e892SBill Paul 				}
285596f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
285696f2e892SBill Paul 			}
2857bcb9ef4fSLuigi Rizzo 			sc->dc_cdata.dc_tx_cnt--;
285896f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
285996f2e892SBill Paul 			continue;
286096f2e892SBill Paul 		}
286196f2e892SBill Paul 
286229a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2863feb78939SJonathan Chen 			/*
2864feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2865feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
286629a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
286729a2220aSBill Paul 			 * Who knows, but Conexant chips have the
286829a2220aSBill Paul 			 * same problem. Maybe they took lessons
286929a2220aSBill Paul 			 * from Xircom.
287029a2220aSBill Paul 			 */
2871feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2872feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2873feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2874feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
2875feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2876feb78939SJonathan Chen 		} else {
287796f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
287896f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
287996f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
288096f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
288196f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2882feb78939SJonathan Chen 		}
288396f2e892SBill Paul 
288496f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
288596f2e892SBill Paul 			ifp->if_oerrors++;
288696f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
288796f2e892SBill Paul 				ifp->if_collisions++;
288896f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
288996f2e892SBill Paul 				ifp->if_collisions++;
289096f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
2891c8b27acaSJohn Baldwin 				dc_init_locked(sc);
289296f2e892SBill Paul 				return;
289396f2e892SBill Paul 			}
289496f2e892SBill Paul 		}
289596f2e892SBill Paul 
289696f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
289796f2e892SBill Paul 
289896f2e892SBill Paul 		ifp->if_opackets++;
289996f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
290056e5e7aeSMaxime Henrion 			bus_dmamap_sync(sc->dc_mtag,
290156e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[idx],
290256e5e7aeSMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
290356e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag,
290456e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[idx]);
290596f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
290696f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
290796f2e892SBill Paul 		}
290896f2e892SBill Paul 
290996f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
291096f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
291196f2e892SBill Paul 	}
291296f2e892SBill Paul 
2913bcb9ef4fSLuigi Rizzo 	if (idx != sc->dc_cdata.dc_tx_cons) {
29140934f18aSMaxime Henrion 	    	/* Some buffers have been freed. */
291596f2e892SBill Paul 		sc->dc_cdata.dc_tx_cons = idx;
291613f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2917bcb9ef4fSLuigi Rizzo 	}
2918bcb9ef4fSLuigi Rizzo 	ifp->if_timer = (sc->dc_cdata.dc_tx_cnt == 0) ? 0 : 5;
291996f2e892SBill Paul }
292096f2e892SBill Paul 
2921e3d2833aSAlfred Perlstein static void
29220934f18aSMaxime Henrion dc_tick(void *xsc)
292396f2e892SBill Paul {
292496f2e892SBill Paul 	struct dc_softc *sc;
292596f2e892SBill Paul 	struct mii_data *mii;
292696f2e892SBill Paul 	struct ifnet *ifp;
292796f2e892SBill Paul 	u_int32_t r;
292896f2e892SBill Paul 
292996f2e892SBill Paul 	sc = xsc;
2930c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
2931fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
293296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
293396f2e892SBill Paul 
293496f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2935318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2936318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2937318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2938318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
293996f2e892SBill Paul 				sc->dc_link = 0;
2940318b02fdSBill Paul 				mii_mediachg(mii);
2941318b02fdSBill Paul 			}
2942318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2943318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2944318b02fdSBill Paul 				sc->dc_link = 0;
2945318b02fdSBill Paul 				mii_mediachg(mii);
2946318b02fdSBill Paul 			}
2947d675147eSBill Paul 			if (sc->dc_link == 0)
294896f2e892SBill Paul 				mii_tick(mii);
294996f2e892SBill Paul 		} else {
2950318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
295196f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2952259b8d84SMartin Blapp 			    sc->dc_cdata.dc_tx_cnt == 0) {
295396f2e892SBill Paul 				mii_tick(mii);
2954042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2955042c8f6eSBill Paul 					sc->dc_link = 0;
295696f2e892SBill Paul 			}
2957259b8d84SMartin Blapp 		}
295896f2e892SBill Paul 	} else
295996f2e892SBill Paul 		mii_tick(mii);
296096f2e892SBill Paul 
296196f2e892SBill Paul 	/*
296296f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
296396f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
296496f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
296596f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
296696f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
296796f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
296896f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
296996f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
297096f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
297196f2e892SBill Paul 	 * a screeching halt for several seconds.
297296f2e892SBill Paul 	 *
297396f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
297496f2e892SBill Paul 	 * any packets until a link has been established. After the
297596f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
297696f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
297796f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
297896f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
297996f2e892SBill Paul 	 */
2980cd62a9cbSJonathan Lemon 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
298196f2e892SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
298296f2e892SBill Paul 		sc->dc_link++;
2983cbaf877fSBrian Feldman 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2984c8b27acaSJohn Baldwin 			dc_start_locked(ifp);
298596f2e892SBill Paul 	}
298696f2e892SBill Paul 
2987318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2988b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2989318b02fdSBill Paul 	else
2990b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
299196f2e892SBill Paul }
299296f2e892SBill Paul 
2993d467c136SBill Paul /*
2994d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
2995d467c136SBill Paul  * or switch to store and forward mode if we have to.
2996d467c136SBill Paul  */
2997e3d2833aSAlfred Perlstein static void
29980934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
2999d467c136SBill Paul {
3000d467c136SBill Paul 	u_int32_t isr;
3001d467c136SBill Paul 	int i;
3002d467c136SBill Paul 
3003d467c136SBill Paul 	if (DC_IS_DAVICOM(sc))
3004c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3005d467c136SBill Paul 
3006d467c136SBill Paul 	if (DC_IS_INTEL(sc)) {
3007d467c136SBill Paul 		/*
3008d467c136SBill Paul 		 * The real 21143 requires that the transmitter be idle
3009d467c136SBill Paul 		 * in order to change the transmit threshold or store
3010d467c136SBill Paul 		 * and forward state.
3011d467c136SBill Paul 		 */
3012d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
3013d467c136SBill Paul 
3014d467c136SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
3015d467c136SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
3016d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE)
3017d467c136SBill Paul 				break;
3018d467c136SBill Paul 			DELAY(10);
3019d467c136SBill Paul 		}
3020d467c136SBill Paul 		if (i == DC_TIMEOUT) {
302122f6205dSJohn Baldwin 			if_printf(sc->dc_ifp,
302222f6205dSJohn Baldwin 			    "failed to force tx to idle state\n");
3023c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3024d467c136SBill Paul 		}
3025d467c136SBill Paul 	}
3026d467c136SBill Paul 
302722f6205dSJohn Baldwin 	if_printf(sc->dc_ifp, "TX underrun -- ");
3028d467c136SBill Paul 	sc->dc_txthresh += DC_TXTHRESH_INC;
3029d467c136SBill Paul 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
3030d467c136SBill Paul 		printf("using store and forward mode\n");
3031d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
3032d467c136SBill Paul 	} else {
3033d467c136SBill Paul 		printf("increasing TX threshold\n");
3034d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3035d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
3036d467c136SBill Paul 	}
3037d467c136SBill Paul 
3038d467c136SBill Paul 	if (DC_IS_INTEL(sc))
3039d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
3040d467c136SBill Paul }
3041d467c136SBill Paul 
3042e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3043e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3044e4fc250cSLuigi Rizzo 
3045e4fc250cSLuigi Rizzo static void
3046e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3047e4fc250cSLuigi Rizzo {
3048e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
3049e4fc250cSLuigi Rizzo 
305040929967SGleb Smirnoff 	DC_LOCK(sc);
305140929967SGleb Smirnoff 
305240929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
305340929967SGleb Smirnoff 		DC_UNLOCK(sc);
3054e4fc250cSLuigi Rizzo 		return;
3055e4fc250cSLuigi Rizzo 	}
305640929967SGleb Smirnoff 
3057e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
3058e4fc250cSLuigi Rizzo 	dc_rxeof(sc);
3059e4fc250cSLuigi Rizzo 	dc_txeof(sc);
306013f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
306113f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3062c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3063e4fc250cSLuigi Rizzo 
3064e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3065e4fc250cSLuigi Rizzo 		u_int32_t	status;
3066e4fc250cSLuigi Rizzo 
3067e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3068e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3069e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3070e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
30715120abbfSSam Leffler 		if (!status) {
30725120abbfSSam Leffler 			DC_UNLOCK(sc);
3073e4fc250cSLuigi Rizzo 			return;
30745120abbfSSam Leffler 		}
3075e4fc250cSLuigi Rizzo 		/* ack what we have */
3076e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3077e4fc250cSLuigi Rizzo 
3078e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3079e4fc250cSLuigi Rizzo 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3080e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3081e4fc250cSLuigi Rizzo 
3082e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3083e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3084e4fc250cSLuigi Rizzo 		}
3085e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3086e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3087e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3088e4fc250cSLuigi Rizzo 
3089e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3090e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3091e4fc250cSLuigi Rizzo 
3092e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
309322f6205dSJohn Baldwin 			if_printf(ifp, "dc_poll: bus error\n");
3094e4fc250cSLuigi Rizzo 			dc_reset(sc);
3095c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3096e4fc250cSLuigi Rizzo 		}
3097e4fc250cSLuigi Rizzo 	}
30985120abbfSSam Leffler 	DC_UNLOCK(sc);
3099e4fc250cSLuigi Rizzo }
3100e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3101e4fc250cSLuigi Rizzo 
3102e3d2833aSAlfred Perlstein static void
31030934f18aSMaxime Henrion dc_intr(void *arg)
310496f2e892SBill Paul {
310596f2e892SBill Paul 	struct dc_softc *sc;
310696f2e892SBill Paul 	struct ifnet *ifp;
310796f2e892SBill Paul 	u_int32_t status;
310896f2e892SBill Paul 
310996f2e892SBill Paul 	sc = arg;
3110d2a1864bSWarner Losh 
31110934f18aSMaxime Henrion 	if (sc->suspended)
3112e8388e14SMitsuru IWASAKI 		return;
3113e8388e14SMitsuru IWASAKI 
3114d2a1864bSWarner Losh 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
3115d2a1864bSWarner Losh 		return;
3116d2a1864bSWarner Losh 
3117d1ce9105SBill Paul 	DC_LOCK(sc);
3118fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3119e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
312040929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
312140929967SGleb Smirnoff 		DC_UNLOCK(sc);
312240929967SGleb Smirnoff 		return;
3123e4fc250cSLuigi Rizzo 	}
31240934f18aSMaxime Henrion #endif
312596f2e892SBill Paul 
3126d88a358cSLuigi Rizzo 	/* Suppress unwanted interrupts */
312796f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
312896f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
312996f2e892SBill Paul 			dc_stop(sc);
3130d1ce9105SBill Paul 		DC_UNLOCK(sc);
313196f2e892SBill Paul 		return;
313296f2e892SBill Paul 	}
313396f2e892SBill Paul 
313496f2e892SBill Paul 	/* Disable interrupts. */
313596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
313696f2e892SBill Paul 
3137feb78939SJonathan Chen 	while (((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS)
3138feb78939SJonathan Chen 	      && status != 0xFFFFFFFF) {
313996f2e892SBill Paul 
314096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
314196f2e892SBill Paul 
314273bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
314373bf949cSBill Paul 			int		curpkts;
314473bf949cSBill Paul 			curpkts = ifp->if_ipackets;
314596f2e892SBill Paul 			dc_rxeof(sc);
314673bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
314773bf949cSBill Paul 				while (dc_rx_resync(sc))
314873bf949cSBill Paul 					dc_rxeof(sc);
314973bf949cSBill Paul 			}
315073bf949cSBill Paul 		}
315196f2e892SBill Paul 
315296f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
315396f2e892SBill Paul 			dc_txeof(sc);
315496f2e892SBill Paul 
315596f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
315696f2e892SBill Paul 			dc_txeof(sc);
315796f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
315896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
315996f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
316096f2e892SBill Paul 			}
316196f2e892SBill Paul 		}
316296f2e892SBill Paul 
3163d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3164d467c136SBill Paul 			dc_tx_underrun(sc);
316596f2e892SBill Paul 
316696f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
316773bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
316873bf949cSBill Paul 			int		curpkts;
316973bf949cSBill Paul 			curpkts = ifp->if_ipackets;
317096f2e892SBill Paul 			dc_rxeof(sc);
317173bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
317273bf949cSBill Paul 				while (dc_rx_resync(sc))
317373bf949cSBill Paul 					dc_rxeof(sc);
317473bf949cSBill Paul 			}
317573bf949cSBill Paul 		}
317696f2e892SBill Paul 
317796f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
317896f2e892SBill Paul 			dc_reset(sc);
3179c8b27acaSJohn Baldwin 			dc_init_locked(sc);
318096f2e892SBill Paul 		}
318196f2e892SBill Paul 	}
318296f2e892SBill Paul 
318396f2e892SBill Paul 	/* Re-enable interrupts. */
318496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
318596f2e892SBill Paul 
3186cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3187c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
318896f2e892SBill Paul 
3189d1ce9105SBill Paul 	DC_UNLOCK(sc);
319096f2e892SBill Paul }
319196f2e892SBill Paul 
319256e5e7aeSMaxime Henrion static void
319356e5e7aeSMaxime Henrion dc_dma_map_txbuf(arg, segs, nseg, mapsize, error)
319456e5e7aeSMaxime Henrion 	void *arg;
319556e5e7aeSMaxime Henrion 	bus_dma_segment_t *segs;
319656e5e7aeSMaxime Henrion 	int nseg;
319756e5e7aeSMaxime Henrion 	bus_size_t mapsize;
319856e5e7aeSMaxime Henrion 	int error;
319956e5e7aeSMaxime Henrion {
320056e5e7aeSMaxime Henrion 	struct dc_softc *sc;
320156e5e7aeSMaxime Henrion 	struct dc_desc *f;
320256e5e7aeSMaxime Henrion 	int cur, first, frag, i;
320356e5e7aeSMaxime Henrion 
320456e5e7aeSMaxime Henrion 	sc = arg;
320556e5e7aeSMaxime Henrion 	if (error) {
320656e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_tx_err = error;
320756e5e7aeSMaxime Henrion 		return;
320856e5e7aeSMaxime Henrion 	}
320956e5e7aeSMaxime Henrion 
321056e5e7aeSMaxime Henrion 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
321156e5e7aeSMaxime Henrion 	for (i = 0; i < nseg; i++) {
321256e5e7aeSMaxime Henrion 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
321356e5e7aeSMaxime Henrion 		    (frag == (DC_TX_LIST_CNT - 1)) &&
321456e5e7aeSMaxime Henrion 		    (first != sc->dc_cdata.dc_tx_first)) {
321556e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag,
321656e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[first]);
321756e5e7aeSMaxime Henrion 			sc->dc_cdata.dc_tx_err = ENOBUFS;
321856e5e7aeSMaxime Henrion 			return;
321956e5e7aeSMaxime Henrion 		}
322056e5e7aeSMaxime Henrion 
322156e5e7aeSMaxime Henrion 		f = &sc->dc_ldata->dc_tx_list[frag];
3222af4358c7SMaxime Henrion 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
322356e5e7aeSMaxime Henrion 		if (i == 0) {
322456e5e7aeSMaxime Henrion 			f->dc_status = 0;
3225af4358c7SMaxime Henrion 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
322656e5e7aeSMaxime Henrion 		} else
3227af4358c7SMaxime Henrion 			f->dc_status = htole32(DC_TXSTAT_OWN);
3228af4358c7SMaxime Henrion 		f->dc_data = htole32(segs[i].ds_addr);
322956e5e7aeSMaxime Henrion 		cur = frag;
323056e5e7aeSMaxime Henrion 		DC_INC(frag, DC_TX_LIST_CNT);
323156e5e7aeSMaxime Henrion 	}
323256e5e7aeSMaxime Henrion 
323356e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_err = 0;
323456e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_prod = frag;
323556e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_cnt += nseg;
3236af4358c7SMaxime Henrion 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
32374ff4a9beSDon Lewis 	sc->dc_cdata.dc_tx_chain[cur] = sc->dc_cdata.dc_tx_mapping;
323856e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
3239af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[first].dc_ctl |=
3240af4358c7SMaxime Henrion 		    htole32(DC_TXCTL_FINT);
324156e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
3242af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
324356e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
3244af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3245af4358c7SMaxime Henrion 	sc->dc_ldata->dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
324656e5e7aeSMaxime Henrion }
324756e5e7aeSMaxime Henrion 
324896f2e892SBill Paul /*
324996f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
325096f2e892SBill Paul  * pointers to the fragment pointers.
325196f2e892SBill Paul  */
3252e3d2833aSAlfred Perlstein static int
3253a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
325496f2e892SBill Paul {
325596f2e892SBill Paul 	struct mbuf *m;
325656e5e7aeSMaxime Henrion 	int error, idx, chainlen = 0;
3257cda97c50SMike Silbersack 
3258cda97c50SMike Silbersack 	/*
3259cda97c50SMike Silbersack 	 * If there's no way we can send any packets, return now.
3260cda97c50SMike Silbersack 	 */
3261cda97c50SMike Silbersack 	if (DC_TX_LIST_CNT - sc->dc_cdata.dc_tx_cnt < 6)
3262cda97c50SMike Silbersack 		return (ENOBUFS);
3263cda97c50SMike Silbersack 
3264cda97c50SMike Silbersack 	/*
3265cda97c50SMike Silbersack 	 * Count the number of frags in this chain to see if
3266cda97c50SMike Silbersack 	 * we need to m_defrag.  Since the descriptor list is shared
3267cda97c50SMike Silbersack 	 * by all packets, we'll m_defrag long chains so that they
3268cda97c50SMike Silbersack 	 * do not use up the entire list, even if they would fit.
3269cda97c50SMike Silbersack 	 */
3270a10c0e45SMike Silbersack 	for (m = *m_head; m != NULL; m = m->m_next)
3271cda97c50SMike Silbersack 		chainlen++;
3272cda97c50SMike Silbersack 
3273cda97c50SMike Silbersack 	if ((chainlen > DC_TX_LIST_CNT / 4) ||
3274cda97c50SMike Silbersack 	    ((DC_TX_LIST_CNT - (chainlen + sc->dc_cdata.dc_tx_cnt)) < 6)) {
3275a10c0e45SMike Silbersack 		m = m_defrag(*m_head, M_DONTWAIT);
3276cda97c50SMike Silbersack 		if (m == NULL)
3277cda97c50SMike Silbersack 			return (ENOBUFS);
3278a10c0e45SMike Silbersack 		*m_head = m;
3279cda97c50SMike Silbersack 	}
328096f2e892SBill Paul 
328196f2e892SBill Paul 	/*
328296f2e892SBill Paul 	 * Start packing the mbufs in this chain into
328396f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
328496f2e892SBill Paul 	 * of fragments or hit the end of the mbuf chain.
328596f2e892SBill Paul 	 */
328656e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
32874ff4a9beSDon Lewis 	sc->dc_cdata.dc_tx_mapping = *m_head;
328856e5e7aeSMaxime Henrion 	error = bus_dmamap_load_mbuf(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
3289a10c0e45SMike Silbersack 	    *m_head, dc_dma_map_txbuf, sc, 0);
329056e5e7aeSMaxime Henrion 	if (error)
329156e5e7aeSMaxime Henrion 		return (error);
329256e5e7aeSMaxime Henrion 	if (sc->dc_cdata.dc_tx_err != 0)
329356e5e7aeSMaxime Henrion 		return (sc->dc_cdata.dc_tx_err);
329456e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
329556e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE);
329656e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
329756e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
329896f2e892SBill Paul 	return (0);
329996f2e892SBill Paul }
330096f2e892SBill Paul 
330196f2e892SBill Paul /*
330296f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
330396f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
330496f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
330596f2e892SBill Paul  * physical addresses.
330696f2e892SBill Paul  */
330796f2e892SBill Paul 
3308e3d2833aSAlfred Perlstein static void
33090934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
331096f2e892SBill Paul {
331196f2e892SBill Paul 	struct dc_softc *sc;
3312c8b27acaSJohn Baldwin 
3313c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3314c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3315c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3316c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3317c8b27acaSJohn Baldwin }
3318c8b27acaSJohn Baldwin 
3319c8b27acaSJohn Baldwin static void
3320c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3321c8b27acaSJohn Baldwin {
3322c8b27acaSJohn Baldwin 	struct dc_softc *sc;
3323cda97c50SMike Silbersack 	struct mbuf *m_head = NULL, *m;
3324cbaf877fSBrian Feldman 	unsigned int queued = 0;
332596f2e892SBill Paul 	int idx;
332696f2e892SBill Paul 
332796f2e892SBill Paul 	sc = ifp->if_softc;
332896f2e892SBill Paul 
3329c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
333096f2e892SBill Paul 
3331c8b27acaSJohn Baldwin 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10)
333296f2e892SBill Paul 		return;
3333d1ce9105SBill Paul 
3334c8b27acaSJohn Baldwin 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
3335d1ce9105SBill Paul 		return;
333696f2e892SBill Paul 
333756e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
333896f2e892SBill Paul 
333996f2e892SBill Paul 	while (sc->dc_cdata.dc_tx_chain[idx] == NULL) {
3340cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
334196f2e892SBill Paul 		if (m_head == NULL)
334296f2e892SBill Paul 			break;
334396f2e892SBill Paul 
33442dfc960aSLuigi Rizzo 		if (sc->dc_flags & DC_TX_COALESCE &&
33452dfc960aSLuigi Rizzo 		    (m_head->m_next != NULL ||
33462dfc960aSLuigi Rizzo 		     sc->dc_flags & DC_TX_ALIGN)) {
3347cda97c50SMike Silbersack 			m = m_defrag(m_head, M_DONTWAIT);
3348cda97c50SMike Silbersack 			if (m == NULL) {
3349cbaf877fSBrian Feldman 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
335013f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3351fda39fd0SBill Paul 				break;
3352cda97c50SMike Silbersack 			} else {
3353cda97c50SMike Silbersack 				m_head = m;
3354fda39fd0SBill Paul 			}
3355fda39fd0SBill Paul 		}
3356fda39fd0SBill Paul 
3357a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
3358cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
335913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
336096f2e892SBill Paul 			break;
336196f2e892SBill Paul 		}
336256e5e7aeSMaxime Henrion 		idx = sc->dc_cdata.dc_tx_prod;
336396f2e892SBill Paul 
3364cbaf877fSBrian Feldman 		queued++;
336596f2e892SBill Paul 		/*
336696f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
336796f2e892SBill Paul 		 * to him.
336896f2e892SBill Paul 		 */
33699ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
33705c1cfac4SBill Paul 
33715c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
337213f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
33735c1cfac4SBill Paul 			break;
33745c1cfac4SBill Paul 		}
337596f2e892SBill Paul 	}
337696f2e892SBill Paul 
3377cbaf877fSBrian Feldman 	if (queued > 0) {
337896f2e892SBill Paul 		/* Transmit */
337996f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
338096f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
338196f2e892SBill Paul 
338296f2e892SBill Paul 		/*
338396f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
338496f2e892SBill Paul 		 */
338596f2e892SBill Paul 		ifp->if_timer = 5;
3386cbaf877fSBrian Feldman 	}
338796f2e892SBill Paul }
338896f2e892SBill Paul 
3389e3d2833aSAlfred Perlstein static void
33900934f18aSMaxime Henrion dc_init(void *xsc)
339196f2e892SBill Paul {
339296f2e892SBill Paul 	struct dc_softc *sc = xsc;
3393c8b27acaSJohn Baldwin 
3394c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3395c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3396c8b27acaSJohn Baldwin #ifdef SRM_MEDIA
3397c8b27acaSJohn Baldwin 	if(sc->dc_srm_media) {
3398c8b27acaSJohn Baldwin 		struct ifreq ifr;
3399c8b27acaSJohn Baldwin 		struct mii_data *mii;
3400c8b27acaSJohn Baldwin 
3401c8b27acaSJohn Baldwin 		ifr.ifr_media = sc->dc_srm_media;
3402c8b27acaSJohn Baldwin 		sc->dc_srm_media = 0;
3403c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
3404c8b27acaSJohn Baldwin 		mii = device_get_softc(sc->dc_miibus);
3405c8b27acaSJohn Baldwin 		ifmedia_ioctl(sc->dc_ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
3406c8b27acaSJohn Baldwin 	} else
3407c8b27acaSJohn Baldwin #endif
3408c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
3409c8b27acaSJohn Baldwin }
3410c8b27acaSJohn Baldwin 
3411c8b27acaSJohn Baldwin static void
3412c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3413c8b27acaSJohn Baldwin {
3414fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
341596f2e892SBill Paul 	struct mii_data *mii;
341696f2e892SBill Paul 
3417c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
341896f2e892SBill Paul 
341996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
342096f2e892SBill Paul 
342196f2e892SBill Paul 	/*
342296f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
342396f2e892SBill Paul 	 */
342496f2e892SBill Paul 	dc_stop(sc);
342596f2e892SBill Paul 	dc_reset(sc);
342696f2e892SBill Paul 
342796f2e892SBill Paul 	/*
342896f2e892SBill Paul 	 * Set cache alignment and burst length.
342996f2e892SBill Paul 	 */
343088d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
343196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
343296f2e892SBill Paul 	else
343396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3434935fe010SLuigi Rizzo 	/*
3435935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3436935fe010SLuigi Rizzo 	 */
3437935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3438935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
343996f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
344096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
344196f2e892SBill Paul 	} else {
344296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
344396f2e892SBill Paul 	}
344496f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
344596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
344696f2e892SBill Paul 	switch(sc->dc_cachesize) {
344796f2e892SBill Paul 	case 32:
344896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
344996f2e892SBill Paul 		break;
345096f2e892SBill Paul 	case 16:
345196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
345296f2e892SBill Paul 		break;
345396f2e892SBill Paul 	case 8:
345496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
345596f2e892SBill Paul 		break;
345696f2e892SBill Paul 	case 0:
345796f2e892SBill Paul 	default:
345896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
345996f2e892SBill Paul 		break;
346096f2e892SBill Paul 	}
346196f2e892SBill Paul 
346296f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
346396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
346496f2e892SBill Paul 	else {
3465d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
346696f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
346796f2e892SBill Paul 		} else {
346896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
346996f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
347096f2e892SBill Paul 		}
347196f2e892SBill Paul 	}
347296f2e892SBill Paul 
347396f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
347496f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
347596f2e892SBill Paul 
347696f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
347796f2e892SBill Paul 		/*
347896f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
347996f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
348096f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
348196f2e892SBill Paul 		 * document the meaning of these bits so there's no way
348296f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
348396f2e892SBill Paul 		 * number all its own; the rest all use a different one.
348496f2e892SBill Paul 		 */
348596f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
348696f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
348796f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
348896f2e892SBill Paul 		else
348996f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
349096f2e892SBill Paul 	}
349196f2e892SBill Paul 
3492feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3493feb78939SJonathan Chen 		/*
3494feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3495feb78939SJonathan Chen 		 * can talk to the MII.
3496feb78939SJonathan Chen 		 */
3497feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3498feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3499feb78939SJonathan Chen 		DELAY(10);
3500feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3501feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3502feb78939SJonathan Chen 		DELAY(10);
3503feb78939SJonathan Chen 	}
3504feb78939SJonathan Chen 
350596f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3506d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
350796f2e892SBill Paul 
350896f2e892SBill Paul 	/* Init circular RX list. */
350996f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
351022f6205dSJohn Baldwin 		if_printf(ifp,
351122f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
351296f2e892SBill Paul 		dc_stop(sc);
351396f2e892SBill Paul 		return;
351496f2e892SBill Paul 	}
351596f2e892SBill Paul 
351696f2e892SBill Paul 	/*
351756e5e7aeSMaxime Henrion 	 * Init TX descriptors.
351896f2e892SBill Paul 	 */
351996f2e892SBill Paul 	dc_list_tx_init(sc);
352096f2e892SBill Paul 
352196f2e892SBill Paul 	/*
352296f2e892SBill Paul 	 * Load the address of the RX list.
352396f2e892SBill Paul 	 */
352456e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
352556e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
352696f2e892SBill Paul 
352796f2e892SBill Paul 	/*
352896f2e892SBill Paul 	 * Enable interrupts.
352996f2e892SBill Paul 	 */
3530e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3531e4fc250cSLuigi Rizzo 	/*
3532e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3533e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3534e4fc250cSLuigi Rizzo 	 * after a reset.
3535e4fc250cSLuigi Rizzo 	 */
353640929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3537e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3538e4fc250cSLuigi Rizzo 	else
3539e4fc250cSLuigi Rizzo #endif
354096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
354196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
354296f2e892SBill Paul 
354396f2e892SBill Paul 	/* Enable transmitter. */
354496f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
354596f2e892SBill Paul 
354696f2e892SBill Paul 	/*
3547918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3548918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3549918434c8SBill Paul 	 * link and activity indications.
3550918434c8SBill Paul 	 */
355178999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3552918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3553918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
355478999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3555918434c8SBill Paul 	}
3556918434c8SBill Paul 
3557918434c8SBill Paul 	/*
355896f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
355996f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
356096f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
356196f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
356296f2e892SBill Paul 	 */
356396f2e892SBill Paul 	dc_setfilt(sc);
356496f2e892SBill Paul 
356596f2e892SBill Paul 	/* Enable receiver. */
356696f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
356796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
356896f2e892SBill Paul 
356996f2e892SBill Paul 	mii_mediachg(mii);
357096f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
357196f2e892SBill Paul 
357213f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
357313f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
357496f2e892SBill Paul 
3575857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
357645521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3577857fd445SBill Paul 		sc->dc_link = 1;
3578857fd445SBill Paul 	else {
3579318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3580b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3581318b02fdSBill Paul 		else
3582b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3583857fd445SBill Paul 	}
358496f2e892SBill Paul }
358596f2e892SBill Paul 
358696f2e892SBill Paul /*
358796f2e892SBill Paul  * Set media options.
358896f2e892SBill Paul  */
3589e3d2833aSAlfred Perlstein static int
35900934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
359196f2e892SBill Paul {
359296f2e892SBill Paul 	struct dc_softc *sc;
359396f2e892SBill Paul 	struct mii_data *mii;
3594f43d9309SBill Paul 	struct ifmedia *ifm;
359596f2e892SBill Paul 
359696f2e892SBill Paul 	sc = ifp->if_softc;
359796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3598c8b27acaSJohn Baldwin 	DC_LOCK(sc);
359996f2e892SBill Paul 	mii_mediachg(mii);
3600f43d9309SBill Paul 	ifm = &mii->mii_media;
3601f43d9309SBill Paul 
3602f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
360345521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3604f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3605f43d9309SBill Paul 	else
360696f2e892SBill Paul 		sc->dc_link = 0;
3607c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
360896f2e892SBill Paul 
360996f2e892SBill Paul 	return (0);
361096f2e892SBill Paul }
361196f2e892SBill Paul 
361296f2e892SBill Paul /*
361396f2e892SBill Paul  * Report current media status.
361496f2e892SBill Paul  */
3615e3d2833aSAlfred Perlstein static void
36160934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
361796f2e892SBill Paul {
361896f2e892SBill Paul 	struct dc_softc *sc;
361996f2e892SBill Paul 	struct mii_data *mii;
3620f43d9309SBill Paul 	struct ifmedia *ifm;
362196f2e892SBill Paul 
362296f2e892SBill Paul 	sc = ifp->if_softc;
362396f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3624c8b27acaSJohn Baldwin 	DC_LOCK(sc);
362596f2e892SBill Paul 	mii_pollstat(mii);
3626f43d9309SBill Paul 	ifm = &mii->mii_media;
3627f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
362845521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3629f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3630f43d9309SBill Paul 			ifmr->ifm_status = 0;
3631f43d9309SBill Paul 			return;
3632f43d9309SBill Paul 		}
3633f43d9309SBill Paul 	}
363496f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
363596f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3636c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
363796f2e892SBill Paul }
363896f2e892SBill Paul 
3639e3d2833aSAlfred Perlstein static int
36400934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
364196f2e892SBill Paul {
364296f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
364396f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
364496f2e892SBill Paul 	struct mii_data *mii;
3645d1ce9105SBill Paul 	int error = 0;
364696f2e892SBill Paul 
364796f2e892SBill Paul 	switch (command) {
364896f2e892SBill Paul 	case SIOCSIFFLAGS:
3649c8b27acaSJohn Baldwin 		DC_LOCK(sc);
365096f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
36515d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
36525d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
36535d6dfbbbSLuigi Rizzo 
365413f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
36555d6dfbbbSLuigi Rizzo 				if (need_setfilt)
365696f2e892SBill Paul 					dc_setfilt(sc);
36575d6dfbbbSLuigi Rizzo 			} else {
365896f2e892SBill Paul 				sc->dc_txthresh = 0;
3659c8b27acaSJohn Baldwin 				dc_init_locked(sc);
366096f2e892SBill Paul 			}
366196f2e892SBill Paul 		} else {
366213f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
366396f2e892SBill Paul 				dc_stop(sc);
366496f2e892SBill Paul 		}
366596f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3666c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
366796f2e892SBill Paul 		error = 0;
366896f2e892SBill Paul 		break;
366996f2e892SBill Paul 	case SIOCADDMULTI:
367096f2e892SBill Paul 	case SIOCDELMULTI:
3671c8b27acaSJohn Baldwin 		DC_LOCK(sc);
367296f2e892SBill Paul 		dc_setfilt(sc);
3673c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
367496f2e892SBill Paul 		error = 0;
367596f2e892SBill Paul 		break;
367696f2e892SBill Paul 	case SIOCGIFMEDIA:
367796f2e892SBill Paul 	case SIOCSIFMEDIA:
367896f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
367996f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
36805c1cfac4SBill Paul #ifdef SRM_MEDIA
3681c8b27acaSJohn Baldwin 		DC_LOCK(sc);
3682510a809eSMike Smith 		if (sc->dc_srm_media)
3683510a809eSMike Smith 			sc->dc_srm_media = 0;
3684c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
3685510a809eSMike Smith #endif
368696f2e892SBill Paul 		break;
3687e695984eSRuslan Ermilov 	case SIOCSIFCAP:
368840929967SGleb Smirnoff #ifdef DEVICE_POLLING
368940929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
369040929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
369140929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
369240929967SGleb Smirnoff 			if (error)
369340929967SGleb Smirnoff 				return(error);
3694c8b27acaSJohn Baldwin 			DC_LOCK(sc);
369540929967SGleb Smirnoff 			/* Disable interrupts */
369640929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
369740929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3698c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
369940929967SGleb Smirnoff 			return (error);
370040929967SGleb Smirnoff 
370140929967SGleb Smirnoff 		}
370240929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
370340929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
370440929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
370540929967SGleb Smirnoff 			/* Enable interrupts. */
370640929967SGleb Smirnoff 			DC_LOCK(sc);
370740929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
370840929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
370940929967SGleb Smirnoff 			DC_UNLOCK(sc);
371040929967SGleb Smirnoff 			return (error);
371140929967SGleb Smirnoff 		}
371240929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3713e695984eSRuslan Ermilov 		break;
371496f2e892SBill Paul 	default:
37159ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
371696f2e892SBill Paul 		break;
371796f2e892SBill Paul 	}
371896f2e892SBill Paul 
371996f2e892SBill Paul 	return (error);
372096f2e892SBill Paul }
372196f2e892SBill Paul 
3722e3d2833aSAlfred Perlstein static void
37230934f18aSMaxime Henrion dc_watchdog(struct ifnet *ifp)
372496f2e892SBill Paul {
372596f2e892SBill Paul 	struct dc_softc *sc;
372696f2e892SBill Paul 
372796f2e892SBill Paul 	sc = ifp->if_softc;
372896f2e892SBill Paul 
3729d1ce9105SBill Paul 	DC_LOCK(sc);
3730d1ce9105SBill Paul 
373196f2e892SBill Paul 	ifp->if_oerrors++;
373222f6205dSJohn Baldwin 	if_printf(ifp, "watchdog timeout\n");
373396f2e892SBill Paul 
373496f2e892SBill Paul 	dc_stop(sc);
373596f2e892SBill Paul 	dc_reset(sc);
3736c8b27acaSJohn Baldwin 	dc_init_locked(sc);
373796f2e892SBill Paul 
3738cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3739c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
374096f2e892SBill Paul 
3741d1ce9105SBill Paul 	DC_UNLOCK(sc);
374296f2e892SBill Paul }
374396f2e892SBill Paul 
374496f2e892SBill Paul /*
374596f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
374696f2e892SBill Paul  * RX and TX lists.
374796f2e892SBill Paul  */
3748e3d2833aSAlfred Perlstein static void
37490934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
375096f2e892SBill Paul {
375196f2e892SBill Paul 	struct ifnet *ifp;
3752b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3753b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3754b3811c95SMaxime Henrion 	int i;
3755af4358c7SMaxime Henrion 	u_int32_t ctl;
375696f2e892SBill Paul 
3757c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3758d1ce9105SBill Paul 
3759fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
376096f2e892SBill Paul 	ifp->if_timer = 0;
3761b3811c95SMaxime Henrion 	ld = sc->dc_ldata;
3762b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
376396f2e892SBill Paul 
3764b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
376596f2e892SBill Paul 
376613f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
37673b3ec200SPeter Wemm 
376896f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
376996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
377096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
377196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
377296f2e892SBill Paul 	sc->dc_link = 0;
377396f2e892SBill Paul 
377496f2e892SBill Paul 	/*
377596f2e892SBill Paul 	 * Free data in the RX lists.
377696f2e892SBill Paul 	 */
377796f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3778b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
377956e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
378056e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
378196f2e892SBill Paul 		}
378296f2e892SBill Paul 	}
3783b3811c95SMaxime Henrion 	bzero(&ld->dc_rx_list, sizeof(ld->dc_rx_list));
378496f2e892SBill Paul 
378596f2e892SBill Paul 	/*
378696f2e892SBill Paul 	 * Free the TX list buffers.
378796f2e892SBill Paul 	 */
378896f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
3789b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
3790af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
3791af4358c7SMaxime Henrion 			if ((ctl & DC_TXCTL_SETUP) ||
37924ff4a9beSDon Lewis 			    !(ctl & DC_TXCTL_LASTFRAG)) {
3793b3811c95SMaxime Henrion 				cd->dc_tx_chain[i] = NULL;
379496f2e892SBill Paul 				continue;
379596f2e892SBill Paul 			}
379656e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag, cd->dc_tx_map[i]);
379756e5e7aeSMaxime Henrion 			m_freem(cd->dc_tx_chain[i]);
3798b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
379996f2e892SBill Paul 		}
380096f2e892SBill Paul 	}
3801b3811c95SMaxime Henrion 	bzero(&ld->dc_tx_list, sizeof(ld->dc_tx_list));
380296f2e892SBill Paul }
380396f2e892SBill Paul 
380496f2e892SBill Paul /*
3805e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
3806e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
3807e8388e14SMitsuru IWASAKI  * resume.
3808e8388e14SMitsuru IWASAKI  */
3809e3d2833aSAlfred Perlstein static int
38100934f18aSMaxime Henrion dc_suspend(device_t dev)
3811e8388e14SMitsuru IWASAKI {
3812e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
3813e8388e14SMitsuru IWASAKI 
3814e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3815c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3816e8388e14SMitsuru IWASAKI 	dc_stop(sc);
3817e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
3818c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3819e8388e14SMitsuru IWASAKI 
3820e8388e14SMitsuru IWASAKI 	return (0);
3821e8388e14SMitsuru IWASAKI }
3822e8388e14SMitsuru IWASAKI 
3823e8388e14SMitsuru IWASAKI /*
3824e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
3825e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
3826e8388e14SMitsuru IWASAKI  * appropriate.
3827e8388e14SMitsuru IWASAKI  */
3828e3d2833aSAlfred Perlstein static int
38290934f18aSMaxime Henrion dc_resume(device_t dev)
3830e8388e14SMitsuru IWASAKI {
3831e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
3832e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
3833e8388e14SMitsuru IWASAKI 
3834e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3835fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3836e8388e14SMitsuru IWASAKI 
3837e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
3838c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3839e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
3840c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3841e8388e14SMitsuru IWASAKI 
3842e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
3843c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3844e8388e14SMitsuru IWASAKI 
3845e8388e14SMitsuru IWASAKI 	return (0);
3846e8388e14SMitsuru IWASAKI }
3847e8388e14SMitsuru IWASAKI 
3848e8388e14SMitsuru IWASAKI /*
384996f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
385096f2e892SBill Paul  * get confused by errant DMAs when rebooting.
385196f2e892SBill Paul  */
3852e3d2833aSAlfred Perlstein static void
38530934f18aSMaxime Henrion dc_shutdown(device_t dev)
385496f2e892SBill Paul {
385596f2e892SBill Paul 	struct dc_softc *sc;
385696f2e892SBill Paul 
385796f2e892SBill Paul 	sc = device_get_softc(dev);
385896f2e892SBill Paul 
3859c8b27acaSJohn Baldwin 	DC_LOCK(sc);
386096f2e892SBill Paul 	dc_stop(sc);
3861c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
386296f2e892SBill Paul }
3863