xref: /freebsd/sys/dev/dc/if_dc.c (revision 8847579c57d6aff2b3371c707dce7a2cee8389aa)
1 /*-
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by Bill Paul.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 /*
37  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
38  * series chips and several workalikes including the following:
39  *
40  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
41  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
42  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
43  * ASIX Electronics AX88140A (www.asix.com.tw)
44  * ASIX Electronics AX88141 (www.asix.com.tw)
45  * ADMtek AL981 (www.admtek.com.tw)
46  * ADMtek AN985 (www.admtek.com.tw)
47  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek AN985
48  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
49  * Accton EN1217 (www.accton.com)
50  * Xircom X3201 (www.xircom.com)
51  * Abocom FE2500
52  * Conexant LANfinity (www.conexant.com)
53  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
54  *
55  * Datasheets for the 21143 are available at developer.intel.com.
56  * Datasheets for the clone parts can be found at their respective sites.
57  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
58  * The PNIC II is essentially a Macronix 98715A chip; the only difference
59  * worth noting is that its multicast hash table is only 128 bits wide
60  * instead of 512.
61  *
62  * Written by Bill Paul <wpaul@ee.columbia.edu>
63  * Electrical Engineering Department
64  * Columbia University, New York City
65  */
66 /*
67  * The Intel 21143 is the successor to the DEC 21140. It is basically
68  * the same as the 21140 but with a few new features. The 21143 supports
69  * three kinds of media attachments:
70  *
71  * o MII port, for 10Mbps and 100Mbps support and NWAY
72  *   autonegotiation provided by an external PHY.
73  * o SYM port, for symbol mode 100Mbps support.
74  * o 10baseT port.
75  * o AUI/BNC port.
76  *
77  * The 100Mbps SYM port and 10baseT port can be used together in
78  * combination with the internal NWAY support to create a 10/100
79  * autosensing configuration.
80  *
81  * Note that not all tulip workalikes are handled in this driver: we only
82  * deal with those which are relatively well behaved. The Winbond is
83  * handled separately due to its different register offsets and the
84  * special handling needed for its various bugs. The PNIC is handled
85  * here, but I'm not thrilled about it.
86  *
87  * All of the workalike chips use some form of MII transceiver support
88  * with the exception of the Macronix chips, which also have a SYM port.
89  * The ASIX AX88140A is also documented to have a SYM port, but all
90  * the cards I've seen use an MII transceiver, probably because the
91  * AX88140A doesn't support internal NWAY.
92  */
93 
94 #ifdef HAVE_KERNEL_OPTION_HEADERS
95 #include "opt_device_polling.h"
96 #endif
97 
98 #include <sys/param.h>
99 #include <sys/endian.h>
100 #include <sys/systm.h>
101 #include <sys/sockio.h>
102 #include <sys/mbuf.h>
103 #include <sys/malloc.h>
104 #include <sys/kernel.h>
105 #include <sys/module.h>
106 #include <sys/socket.h>
107 #include <sys/sysctl.h>
108 
109 #include <net/if.h>
110 #include <net/if_arp.h>
111 #include <net/ethernet.h>
112 #include <net/if_dl.h>
113 #include <net/if_media.h>
114 #include <net/if_types.h>
115 #include <net/if_vlan_var.h>
116 
117 #include <net/bpf.h>
118 
119 #include <machine/bus.h>
120 #include <machine/resource.h>
121 #include <sys/bus.h>
122 #include <sys/rman.h>
123 
124 #include <dev/mii/mii.h>
125 #include <dev/mii/miivar.h>
126 
127 #include <dev/pci/pcireg.h>
128 #include <dev/pci/pcivar.h>
129 
130 #define DC_USEIOSPACE
131 
132 #include <dev/dc/if_dcreg.h>
133 
134 #ifdef __sparc64__
135 #include <dev/ofw/openfirm.h>
136 #include <machine/ofw_machdep.h>
137 #endif
138 
139 MODULE_DEPEND(dc, pci, 1, 1, 1);
140 MODULE_DEPEND(dc, ether, 1, 1, 1);
141 MODULE_DEPEND(dc, miibus, 1, 1, 1);
142 
143 /*
144  * "device miibus" is required in kernel config.  See GENERIC if you get
145  * errors here.
146  */
147 #include "miibus_if.h"
148 
149 /*
150  * Various supported device vendors/types and their names.
151  */
152 static struct dc_type dc_devs[] = {
153 	{ DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143), 0,
154 		"Intel 21143 10/100BaseTX" },
155 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009), 0,
156 		"Davicom DM9009 10/100BaseTX" },
157 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100), 0,
158 		"Davicom DM9100 10/100BaseTX" },
159 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), DC_REVISION_DM9102A,
160 		"Davicom DM9102A 10/100BaseTX" },
161 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), 0,
162 		"Davicom DM9102 10/100BaseTX" },
163 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981), 0,
164 		"ADMtek AL981 10/100BaseTX" },
165 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
166 		"ADMtek AN985 10/100BaseTX" },
167 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
168 		"ADMtek ADM9511 10/100BaseTX" },
169 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
170 		"ADMtek ADM9513 10/100BaseTX" },
171 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_FA511), 0,
172 		"Netgear FA511 10/100BaseTX" },
173 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), DC_REVISION_88141,
174 		"ASIX AX88141 10/100BaseTX" },
175 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), 0,
176 		"ASIX AX88140A 10/100BaseTX" },
177 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), DC_REVISION_98713A,
178 		"Macronix 98713A 10/100BaseTX" },
179 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), 0,
180 		"Macronix 98713 10/100BaseTX" },
181 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), DC_REVISION_98713A,
182 		"Compex RL100-TX 10/100BaseTX" },
183 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), 0,
184 		"Compex RL100-TX 10/100BaseTX" },
185 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98725,
186 		"Macronix 98725 10/100BaseTX" },
187 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98715AEC_C,
188 		"Macronix 98715AEC-C 10/100BaseTX" },
189 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), 0,
190 		"Macronix 98715/98715A 10/100BaseTX" },
191 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727), 0,
192 		"Macronix 98727/98732 10/100BaseTX" },
193 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115), 0,
194 		"LC82C115 PNIC II 10/100BaseTX" },
195 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), DC_REVISION_82C169,
196 		"82c169 PNIC 10/100BaseTX" },
197 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), 0,
198 		"82c168 PNIC 10/100BaseTX" },
199 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217), 0,
200 		"Accton EN1217 10/100BaseTX" },
201 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242), 0,
202 		"Accton EN2242 MiniPCI 10/100BaseTX" },
203 	{ DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201), 0,
204 	  	"Xircom X3201 10/100BaseTX" },
205 	{ DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD), 0,
206 		"Neteasy DRP-32TXD Cardbus 10/100" },
207 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500), 0,
208 		"Abocom FE2500 10/100BaseTX" },
209 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX), 0,
210 		"Abocom FE2500MX 10/100BaseTX" },
211 	{ DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112), 0,
212 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
213 	{ DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX), 0,
214 		"Hawking CB102 CardBus 10/100" },
215 	{ DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T), 0,
216 		"PlaneX FNW-3602-T CardBus 10/100" },
217 	{ DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB), 0,
218 		"3Com OfficeConnect 10/100B" },
219 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120), 0,
220 		"Microsoft MN-120 CardBus 10/100" },
221 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130), 0,
222 		"Microsoft MN-130 10/100" },
223 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08), 0,
224 		"Linksys PCMPC200 CardBus 10/100" },
225 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09), 0,
226 		"Linksys PCMPC200 CardBus 10/100" },
227 	{ 0, 0, NULL }
228 };
229 
230 static int dc_probe(device_t);
231 static int dc_attach(device_t);
232 static int dc_detach(device_t);
233 static int dc_suspend(device_t);
234 static int dc_resume(device_t);
235 static struct dc_type *dc_devtype(device_t);
236 static int dc_newbuf(struct dc_softc *, int, int);
237 static int dc_encap(struct dc_softc *, struct mbuf **);
238 static void dc_pnic_rx_bug_war(struct dc_softc *, int);
239 static int dc_rx_resync(struct dc_softc *);
240 static void dc_rxeof(struct dc_softc *);
241 static void dc_txeof(struct dc_softc *);
242 static void dc_tick(void *);
243 static void dc_tx_underrun(struct dc_softc *);
244 static void dc_intr(void *);
245 static void dc_start(struct ifnet *);
246 static void dc_start_locked(struct ifnet *);
247 static int dc_ioctl(struct ifnet *, u_long, caddr_t);
248 static void dc_init(void *);
249 static void dc_init_locked(struct dc_softc *);
250 static void dc_stop(struct dc_softc *);
251 static void dc_watchdog(struct ifnet *);
252 static void dc_shutdown(device_t);
253 static int dc_ifmedia_upd(struct ifnet *);
254 static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
255 
256 static void dc_delay(struct dc_softc *);
257 static void dc_eeprom_idle(struct dc_softc *);
258 static void dc_eeprom_putbyte(struct dc_softc *, int);
259 static void dc_eeprom_getword(struct dc_softc *, int, u_int16_t *);
260 static void dc_eeprom_getword_pnic(struct dc_softc *, int, u_int16_t *);
261 static void dc_eeprom_getword_xircom(struct dc_softc *, int, u_int16_t *);
262 static void dc_eeprom_width(struct dc_softc *);
263 static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
264 
265 static void dc_mii_writebit(struct dc_softc *, int);
266 static int dc_mii_readbit(struct dc_softc *);
267 static void dc_mii_sync(struct dc_softc *);
268 static void dc_mii_send(struct dc_softc *, u_int32_t, int);
269 static int dc_mii_readreg(struct dc_softc *, struct dc_mii_frame *);
270 static int dc_mii_writereg(struct dc_softc *, struct dc_mii_frame *);
271 static int dc_miibus_readreg(device_t, int, int);
272 static int dc_miibus_writereg(device_t, int, int, int);
273 static void dc_miibus_statchg(device_t);
274 static void dc_miibus_mediainit(device_t);
275 
276 static void dc_setcfg(struct dc_softc *, int);
277 static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
278 static uint32_t dc_mchash_be(const uint8_t *);
279 static void dc_setfilt_21143(struct dc_softc *);
280 static void dc_setfilt_asix(struct dc_softc *);
281 static void dc_setfilt_admtek(struct dc_softc *);
282 static void dc_setfilt_xircom(struct dc_softc *);
283 
284 static void dc_setfilt(struct dc_softc *);
285 
286 static void dc_reset(struct dc_softc *);
287 static int dc_list_rx_init(struct dc_softc *);
288 static int dc_list_tx_init(struct dc_softc *);
289 
290 static void dc_read_srom(struct dc_softc *, int);
291 static void dc_parse_21143_srom(struct dc_softc *);
292 static void dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
293 static void dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
294 static void dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
295 static void dc_apply_fixup(struct dc_softc *, int);
296 
297 static void dc_dma_map_txbuf(void *, bus_dma_segment_t *, int, bus_size_t, int);
298 static void dc_dma_map_rxbuf(void *, bus_dma_segment_t *, int, bus_size_t, int);
299 
300 #ifdef DC_USEIOSPACE
301 #define DC_RES			SYS_RES_IOPORT
302 #define DC_RID			DC_PCI_CFBIO
303 #else
304 #define DC_RES			SYS_RES_MEMORY
305 #define DC_RID			DC_PCI_CFBMA
306 #endif
307 
308 static device_method_t dc_methods[] = {
309 	/* Device interface */
310 	DEVMETHOD(device_probe,		dc_probe),
311 	DEVMETHOD(device_attach,	dc_attach),
312 	DEVMETHOD(device_detach,	dc_detach),
313 	DEVMETHOD(device_suspend,	dc_suspend),
314 	DEVMETHOD(device_resume,	dc_resume),
315 	DEVMETHOD(device_shutdown,	dc_shutdown),
316 
317 	/* bus interface */
318 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
319 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
320 
321 	/* MII interface */
322 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
323 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
324 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
325 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
326 
327 	{ 0, 0 }
328 };
329 
330 static driver_t dc_driver = {
331 	"dc",
332 	dc_methods,
333 	sizeof(struct dc_softc)
334 };
335 
336 static devclass_t dc_devclass;
337 #ifdef __i386__
338 static int dc_quick = 1;
339 SYSCTL_INT(_hw, OID_AUTO, dc_quick, CTLFLAG_RW, &dc_quick, 0,
340     "do not m_devget() in dc driver");
341 #endif
342 
343 DRIVER_MODULE(dc, cardbus, dc_driver, dc_devclass, 0, 0);
344 DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
345 DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
346 
347 #define DC_SETBIT(sc, reg, x)				\
348 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
349 
350 #define DC_CLRBIT(sc, reg, x)				\
351 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
352 
353 #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
354 #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
355 
356 static void
357 dc_delay(struct dc_softc *sc)
358 {
359 	int idx;
360 
361 	for (idx = (300 / 33) + 1; idx > 0; idx--)
362 		CSR_READ_4(sc, DC_BUSCTL);
363 }
364 
365 static void
366 dc_eeprom_width(struct dc_softc *sc)
367 {
368 	int i;
369 
370 	/* Force EEPROM to idle state. */
371 	dc_eeprom_idle(sc);
372 
373 	/* Enter EEPROM access mode. */
374 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
375 	dc_delay(sc);
376 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
377 	dc_delay(sc);
378 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
379 	dc_delay(sc);
380 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
381 	dc_delay(sc);
382 
383 	for (i = 3; i--;) {
384 		if (6 & (1 << i))
385 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
386 		else
387 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
388 		dc_delay(sc);
389 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
390 		dc_delay(sc);
391 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
392 		dc_delay(sc);
393 	}
394 
395 	for (i = 1; i <= 12; i++) {
396 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
397 		dc_delay(sc);
398 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
399 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
400 			dc_delay(sc);
401 			break;
402 		}
403 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
404 		dc_delay(sc);
405 	}
406 
407 	/* Turn off EEPROM access mode. */
408 	dc_eeprom_idle(sc);
409 
410 	if (i < 4 || i > 12)
411 		sc->dc_romwidth = 6;
412 	else
413 		sc->dc_romwidth = i;
414 
415 	/* Enter EEPROM access mode. */
416 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
417 	dc_delay(sc);
418 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
419 	dc_delay(sc);
420 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
421 	dc_delay(sc);
422 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
423 	dc_delay(sc);
424 
425 	/* Turn off EEPROM access mode. */
426 	dc_eeprom_idle(sc);
427 }
428 
429 static void
430 dc_eeprom_idle(struct dc_softc *sc)
431 {
432 	int i;
433 
434 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
435 	dc_delay(sc);
436 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
437 	dc_delay(sc);
438 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
439 	dc_delay(sc);
440 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
441 	dc_delay(sc);
442 
443 	for (i = 0; i < 25; i++) {
444 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
445 		dc_delay(sc);
446 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
447 		dc_delay(sc);
448 	}
449 
450 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
451 	dc_delay(sc);
452 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
453 	dc_delay(sc);
454 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
455 }
456 
457 /*
458  * Send a read command and address to the EEPROM, check for ACK.
459  */
460 static void
461 dc_eeprom_putbyte(struct dc_softc *sc, int addr)
462 {
463 	int d, i;
464 
465 	d = DC_EECMD_READ >> 6;
466 	for (i = 3; i--; ) {
467 		if (d & (1 << i))
468 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
469 		else
470 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
471 		dc_delay(sc);
472 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
473 		dc_delay(sc);
474 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
475 		dc_delay(sc);
476 	}
477 
478 	/*
479 	 * Feed in each bit and strobe the clock.
480 	 */
481 	for (i = sc->dc_romwidth; i--;) {
482 		if (addr & (1 << i)) {
483 			SIO_SET(DC_SIO_EE_DATAIN);
484 		} else {
485 			SIO_CLR(DC_SIO_EE_DATAIN);
486 		}
487 		dc_delay(sc);
488 		SIO_SET(DC_SIO_EE_CLK);
489 		dc_delay(sc);
490 		SIO_CLR(DC_SIO_EE_CLK);
491 		dc_delay(sc);
492 	}
493 }
494 
495 /*
496  * Read a word of data stored in the EEPROM at address 'addr.'
497  * The PNIC 82c168/82c169 has its own non-standard way to read
498  * the EEPROM.
499  */
500 static void
501 dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, u_int16_t *dest)
502 {
503 	int i;
504 	u_int32_t r;
505 
506 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
507 
508 	for (i = 0; i < DC_TIMEOUT; i++) {
509 		DELAY(1);
510 		r = CSR_READ_4(sc, DC_SIO);
511 		if (!(r & DC_PN_SIOCTL_BUSY)) {
512 			*dest = (u_int16_t)(r & 0xFFFF);
513 			return;
514 		}
515 	}
516 }
517 
518 /*
519  * Read a word of data stored in the EEPROM at address 'addr.'
520  * The Xircom X3201 has its own non-standard way to read
521  * the EEPROM, too.
522  */
523 static void
524 dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, u_int16_t *dest)
525 {
526 
527 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
528 
529 	addr *= 2;
530 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
531 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
532 	addr += 1;
533 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
534 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
535 
536 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
537 }
538 
539 /*
540  * Read a word of data stored in the EEPROM at address 'addr.'
541  */
542 static void
543 dc_eeprom_getword(struct dc_softc *sc, int addr, u_int16_t *dest)
544 {
545 	int i;
546 	u_int16_t word = 0;
547 
548 	/* Force EEPROM to idle state. */
549 	dc_eeprom_idle(sc);
550 
551 	/* Enter EEPROM access mode. */
552 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
553 	dc_delay(sc);
554 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
555 	dc_delay(sc);
556 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
557 	dc_delay(sc);
558 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
559 	dc_delay(sc);
560 
561 	/*
562 	 * Send address of word we want to read.
563 	 */
564 	dc_eeprom_putbyte(sc, addr);
565 
566 	/*
567 	 * Start reading bits from EEPROM.
568 	 */
569 	for (i = 0x8000; i; i >>= 1) {
570 		SIO_SET(DC_SIO_EE_CLK);
571 		dc_delay(sc);
572 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
573 			word |= i;
574 		dc_delay(sc);
575 		SIO_CLR(DC_SIO_EE_CLK);
576 		dc_delay(sc);
577 	}
578 
579 	/* Turn off EEPROM access mode. */
580 	dc_eeprom_idle(sc);
581 
582 	*dest = word;
583 }
584 
585 /*
586  * Read a sequence of words from the EEPROM.
587  */
588 static void
589 dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
590 {
591 	int i;
592 	u_int16_t word = 0, *ptr;
593 
594 	for (i = 0; i < cnt; i++) {
595 		if (DC_IS_PNIC(sc))
596 			dc_eeprom_getword_pnic(sc, off + i, &word);
597 		else if (DC_IS_XIRCOM(sc))
598 			dc_eeprom_getword_xircom(sc, off + i, &word);
599 		else
600 			dc_eeprom_getword(sc, off + i, &word);
601 		ptr = (u_int16_t *)(dest + (i * 2));
602 		if (be)
603 			*ptr = be16toh(word);
604 		else
605 			*ptr = le16toh(word);
606 	}
607 }
608 
609 /*
610  * The following two routines are taken from the Macronix 98713
611  * Application Notes pp.19-21.
612  */
613 /*
614  * Write a bit to the MII bus.
615  */
616 static void
617 dc_mii_writebit(struct dc_softc *sc, int bit)
618 {
619 
620 	if (bit)
621 		CSR_WRITE_4(sc, DC_SIO,
622 		    DC_SIO_ROMCTL_WRITE | DC_SIO_MII_DATAOUT);
623 	else
624 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
625 
626 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
627 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
628 }
629 
630 /*
631  * Read a bit from the MII bus.
632  */
633 static int
634 dc_mii_readbit(struct dc_softc *sc)
635 {
636 
637 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ | DC_SIO_MII_DIR);
638 	CSR_READ_4(sc, DC_SIO);
639 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
640 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
641 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
642 		return (1);
643 
644 	return (0);
645 }
646 
647 /*
648  * Sync the PHYs by setting data bit and strobing the clock 32 times.
649  */
650 static void
651 dc_mii_sync(struct dc_softc *sc)
652 {
653 	int i;
654 
655 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
656 
657 	for (i = 0; i < 32; i++)
658 		dc_mii_writebit(sc, 1);
659 }
660 
661 /*
662  * Clock a series of bits through the MII.
663  */
664 static void
665 dc_mii_send(struct dc_softc *sc, u_int32_t bits, int cnt)
666 {
667 	int i;
668 
669 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
670 		dc_mii_writebit(sc, bits & i);
671 }
672 
673 /*
674  * Read an PHY register through the MII.
675  */
676 static int
677 dc_mii_readreg(struct dc_softc *sc, struct dc_mii_frame *frame)
678 {
679 	int i, ack;
680 
681 	/*
682 	 * Set up frame for RX.
683 	 */
684 	frame->mii_stdelim = DC_MII_STARTDELIM;
685 	frame->mii_opcode = DC_MII_READOP;
686 	frame->mii_turnaround = 0;
687 	frame->mii_data = 0;
688 
689 	/*
690 	 * Sync the PHYs.
691 	 */
692 	dc_mii_sync(sc);
693 
694 	/*
695 	 * Send command/address info.
696 	 */
697 	dc_mii_send(sc, frame->mii_stdelim, 2);
698 	dc_mii_send(sc, frame->mii_opcode, 2);
699 	dc_mii_send(sc, frame->mii_phyaddr, 5);
700 	dc_mii_send(sc, frame->mii_regaddr, 5);
701 
702 #ifdef notdef
703 	/* Idle bit */
704 	dc_mii_writebit(sc, 1);
705 	dc_mii_writebit(sc, 0);
706 #endif
707 
708 	/* Check for ack. */
709 	ack = dc_mii_readbit(sc);
710 
711 	/*
712 	 * Now try reading data bits. If the ack failed, we still
713 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
714 	 */
715 	if (ack) {
716 		for (i = 0; i < 16; i++)
717 			dc_mii_readbit(sc);
718 		goto fail;
719 	}
720 
721 	for (i = 0x8000; i; i >>= 1) {
722 		if (!ack) {
723 			if (dc_mii_readbit(sc))
724 				frame->mii_data |= i;
725 		}
726 	}
727 
728 fail:
729 
730 	dc_mii_writebit(sc, 0);
731 	dc_mii_writebit(sc, 0);
732 
733 	if (ack)
734 		return (1);
735 	return (0);
736 }
737 
738 /*
739  * Write to a PHY register through the MII.
740  */
741 static int
742 dc_mii_writereg(struct dc_softc *sc, struct dc_mii_frame *frame)
743 {
744 
745 	/*
746 	 * Set up frame for TX.
747 	 */
748 
749 	frame->mii_stdelim = DC_MII_STARTDELIM;
750 	frame->mii_opcode = DC_MII_WRITEOP;
751 	frame->mii_turnaround = DC_MII_TURNAROUND;
752 
753 	/*
754 	 * Sync the PHYs.
755 	 */
756 	dc_mii_sync(sc);
757 
758 	dc_mii_send(sc, frame->mii_stdelim, 2);
759 	dc_mii_send(sc, frame->mii_opcode, 2);
760 	dc_mii_send(sc, frame->mii_phyaddr, 5);
761 	dc_mii_send(sc, frame->mii_regaddr, 5);
762 	dc_mii_send(sc, frame->mii_turnaround, 2);
763 	dc_mii_send(sc, frame->mii_data, 16);
764 
765 	/* Idle bit. */
766 	dc_mii_writebit(sc, 0);
767 	dc_mii_writebit(sc, 0);
768 
769 	return (0);
770 }
771 
772 static int
773 dc_miibus_readreg(device_t dev, int phy, int reg)
774 {
775 	struct dc_mii_frame frame;
776 	struct dc_softc	 *sc;
777 	int i, rval, phy_reg = 0;
778 
779 	sc = device_get_softc(dev);
780 	bzero(&frame, sizeof(frame));
781 
782 	/*
783 	 * Note: both the AL981 and AN985 have internal PHYs,
784 	 * however the AL981 provides direct access to the PHY
785 	 * registers while the AN985 uses a serial MII interface.
786 	 * The AN985's MII interface is also buggy in that you
787 	 * can read from any MII address (0 to 31), but only address 1
788 	 * behaves normally. To deal with both cases, we pretend
789 	 * that the PHY is at MII address 1.
790 	 */
791 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
792 		return (0);
793 
794 	/*
795 	 * Note: the ukphy probes of the RS7112 report a PHY at
796 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
797 	 * so we only respond to correct one.
798 	 */
799 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
800 		return (0);
801 
802 	if (sc->dc_pmode != DC_PMODE_MII) {
803 		if (phy == (MII_NPHY - 1)) {
804 			switch (reg) {
805 			case MII_BMSR:
806 			/*
807 			 * Fake something to make the probe
808 			 * code think there's a PHY here.
809 			 */
810 				return (BMSR_MEDIAMASK);
811 				break;
812 			case MII_PHYIDR1:
813 				if (DC_IS_PNIC(sc))
814 					return (DC_VENDORID_LO);
815 				return (DC_VENDORID_DEC);
816 				break;
817 			case MII_PHYIDR2:
818 				if (DC_IS_PNIC(sc))
819 					return (DC_DEVICEID_82C168);
820 				return (DC_DEVICEID_21143);
821 				break;
822 			default:
823 				return (0);
824 				break;
825 			}
826 		} else
827 			return (0);
828 	}
829 
830 	if (DC_IS_PNIC(sc)) {
831 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
832 		    (phy << 23) | (reg << 18));
833 		for (i = 0; i < DC_TIMEOUT; i++) {
834 			DELAY(1);
835 			rval = CSR_READ_4(sc, DC_PN_MII);
836 			if (!(rval & DC_PN_MII_BUSY)) {
837 				rval &= 0xFFFF;
838 				return (rval == 0xFFFF ? 0 : rval);
839 			}
840 		}
841 		return (0);
842 	}
843 
844 	if (DC_IS_COMET(sc)) {
845 		switch (reg) {
846 		case MII_BMCR:
847 			phy_reg = DC_AL_BMCR;
848 			break;
849 		case MII_BMSR:
850 			phy_reg = DC_AL_BMSR;
851 			break;
852 		case MII_PHYIDR1:
853 			phy_reg = DC_AL_VENID;
854 			break;
855 		case MII_PHYIDR2:
856 			phy_reg = DC_AL_DEVID;
857 			break;
858 		case MII_ANAR:
859 			phy_reg = DC_AL_ANAR;
860 			break;
861 		case MII_ANLPAR:
862 			phy_reg = DC_AL_LPAR;
863 			break;
864 		case MII_ANER:
865 			phy_reg = DC_AL_ANER;
866 			break;
867 		default:
868 			device_printf(dev, "phy_read: bad phy register %x\n",
869 			    reg);
870 			return (0);
871 			break;
872 		}
873 
874 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
875 
876 		if (rval == 0xFFFF)
877 			return (0);
878 		return (rval);
879 	}
880 
881 	frame.mii_phyaddr = phy;
882 	frame.mii_regaddr = reg;
883 	if (sc->dc_type == DC_TYPE_98713) {
884 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
885 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
886 	}
887 	dc_mii_readreg(sc, &frame);
888 	if (sc->dc_type == DC_TYPE_98713)
889 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
890 
891 	return (frame.mii_data);
892 }
893 
894 static int
895 dc_miibus_writereg(device_t dev, int phy, int reg, int data)
896 {
897 	struct dc_softc *sc;
898 	struct dc_mii_frame frame;
899 	int i, phy_reg = 0;
900 
901 	sc = device_get_softc(dev);
902 	bzero(&frame, sizeof(frame));
903 
904 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
905 		return (0);
906 
907 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
908 		return (0);
909 
910 	if (DC_IS_PNIC(sc)) {
911 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
912 		    (phy << 23) | (reg << 10) | data);
913 		for (i = 0; i < DC_TIMEOUT; i++) {
914 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
915 				break;
916 		}
917 		return (0);
918 	}
919 
920 	if (DC_IS_COMET(sc)) {
921 		switch (reg) {
922 		case MII_BMCR:
923 			phy_reg = DC_AL_BMCR;
924 			break;
925 		case MII_BMSR:
926 			phy_reg = DC_AL_BMSR;
927 			break;
928 		case MII_PHYIDR1:
929 			phy_reg = DC_AL_VENID;
930 			break;
931 		case MII_PHYIDR2:
932 			phy_reg = DC_AL_DEVID;
933 			break;
934 		case MII_ANAR:
935 			phy_reg = DC_AL_ANAR;
936 			break;
937 		case MII_ANLPAR:
938 			phy_reg = DC_AL_LPAR;
939 			break;
940 		case MII_ANER:
941 			phy_reg = DC_AL_ANER;
942 			break;
943 		default:
944 			device_printf(dev, "phy_write: bad phy register %x\n",
945 			    reg);
946 			return (0);
947 			break;
948 		}
949 
950 		CSR_WRITE_4(sc, phy_reg, data);
951 		return (0);
952 	}
953 
954 	frame.mii_phyaddr = phy;
955 	frame.mii_regaddr = reg;
956 	frame.mii_data = data;
957 
958 	if (sc->dc_type == DC_TYPE_98713) {
959 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
960 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
961 	}
962 	dc_mii_writereg(sc, &frame);
963 	if (sc->dc_type == DC_TYPE_98713)
964 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
965 
966 	return (0);
967 }
968 
969 static void
970 dc_miibus_statchg(device_t dev)
971 {
972 	struct dc_softc *sc;
973 	struct mii_data *mii;
974 	struct ifmedia *ifm;
975 
976 	sc = device_get_softc(dev);
977 	if (DC_IS_ADMTEK(sc))
978 		return;
979 
980 	mii = device_get_softc(sc->dc_miibus);
981 	ifm = &mii->mii_media;
982 	if (DC_IS_DAVICOM(sc) &&
983 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
984 		dc_setcfg(sc, ifm->ifm_media);
985 		sc->dc_if_media = ifm->ifm_media;
986 	} else {
987 		dc_setcfg(sc, mii->mii_media_active);
988 		sc->dc_if_media = mii->mii_media_active;
989 	}
990 }
991 
992 /*
993  * Special support for DM9102A cards with HomePNA PHYs. Note:
994  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
995  * to be impossible to talk to the management interface of the DM9801
996  * PHY (its MDIO pin is not connected to anything). Consequently,
997  * the driver has to just 'know' about the additional mode and deal
998  * with it itself. *sigh*
999  */
1000 static void
1001 dc_miibus_mediainit(device_t dev)
1002 {
1003 	struct dc_softc *sc;
1004 	struct mii_data *mii;
1005 	struct ifmedia *ifm;
1006 	int rev;
1007 
1008 	rev = pci_get_revid(dev);
1009 
1010 	sc = device_get_softc(dev);
1011 	mii = device_get_softc(sc->dc_miibus);
1012 	ifm = &mii->mii_media;
1013 
1014 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
1015 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
1016 }
1017 
1018 #define DC_BITS_512	9
1019 #define DC_BITS_128	7
1020 #define DC_BITS_64	6
1021 
1022 static uint32_t
1023 dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
1024 {
1025 	uint32_t crc;
1026 
1027 	/* Compute CRC for the address value. */
1028 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
1029 
1030 	/*
1031 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
1032 	 * chips is only 128 bits wide.
1033 	 */
1034 	if (sc->dc_flags & DC_128BIT_HASH)
1035 		return (crc & ((1 << DC_BITS_128) - 1));
1036 
1037 	/* The hash table on the MX98715BEC is only 64 bits wide. */
1038 	if (sc->dc_flags & DC_64BIT_HASH)
1039 		return (crc & ((1 << DC_BITS_64) - 1));
1040 
1041 	/* Xircom's hash filtering table is different (read: weird) */
1042 	/* Xircom uses the LEAST significant bits */
1043 	if (DC_IS_XIRCOM(sc)) {
1044 		if ((crc & 0x180) == 0x180)
1045 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
1046 		else
1047 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
1048 			    (12 << 4));
1049 	}
1050 
1051 	return (crc & ((1 << DC_BITS_512) - 1));
1052 }
1053 
1054 /*
1055  * Calculate CRC of a multicast group address, return the lower 6 bits.
1056  */
1057 static uint32_t
1058 dc_mchash_be(const uint8_t *addr)
1059 {
1060 	uint32_t crc;
1061 
1062 	/* Compute CRC for the address value. */
1063 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
1064 
1065 	/* Return the filter bit position. */
1066 	return ((crc >> 26) & 0x0000003F);
1067 }
1068 
1069 /*
1070  * 21143-style RX filter setup routine. Filter programming is done by
1071  * downloading a special setup frame into the TX engine. 21143, Macronix,
1072  * PNIC, PNIC II and Davicom chips are programmed this way.
1073  *
1074  * We always program the chip using 'hash perfect' mode, i.e. one perfect
1075  * address (our node address) and a 512-bit hash filter for multicast
1076  * frames. We also sneak the broadcast address into the hash filter since
1077  * we need that too.
1078  */
1079 static void
1080 dc_setfilt_21143(struct dc_softc *sc)
1081 {
1082 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
1083 	struct dc_desc *sframe;
1084 	u_int32_t h, *sp;
1085 	struct ifmultiaddr *ifma;
1086 	struct ifnet *ifp;
1087 	int i;
1088 
1089 	ifp = sc->dc_ifp;
1090 
1091 	i = sc->dc_cdata.dc_tx_prod;
1092 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1093 	sc->dc_cdata.dc_tx_cnt++;
1094 	sframe = &sc->dc_ldata->dc_tx_list[i];
1095 	sp = sc->dc_cdata.dc_sbuf;
1096 	bzero(sp, DC_SFRAME_LEN);
1097 
1098 	sframe->dc_data = htole32(sc->dc_saddr);
1099 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1100 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1101 
1102 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1103 
1104 	/* If we want promiscuous mode, set the allframes bit. */
1105 	if (ifp->if_flags & IFF_PROMISC)
1106 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1107 	else
1108 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1109 
1110 	if (ifp->if_flags & IFF_ALLMULTI)
1111 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1112 	else
1113 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1114 
1115 	IF_ADDR_LOCK(ifp);
1116 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1117 		if (ifma->ifma_addr->sa_family != AF_LINK)
1118 			continue;
1119 		h = dc_mchash_le(sc,
1120 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1121 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1122 	}
1123 	IF_ADDR_UNLOCK(ifp);
1124 
1125 	if (ifp->if_flags & IFF_BROADCAST) {
1126 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1127 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1128 	}
1129 
1130 	/* Set our MAC address. */
1131 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
1132 	sp[39] = DC_SP_MAC(eaddr[0]);
1133 	sp[40] = DC_SP_MAC(eaddr[1]);
1134 	sp[41] = DC_SP_MAC(eaddr[2]);
1135 
1136 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
1137 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1138 
1139 	/*
1140 	 * The PNIC takes an exceedingly long time to process its
1141 	 * setup frame; wait 10ms after posting the setup frame
1142 	 * before proceeding, just so it has time to swallow its
1143 	 * medicine.
1144 	 */
1145 	DELAY(10000);
1146 
1147 	ifp->if_timer = 5;
1148 }
1149 
1150 static void
1151 dc_setfilt_admtek(struct dc_softc *sc)
1152 {
1153 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
1154 	struct ifnet *ifp;
1155 	struct ifmultiaddr *ifma;
1156 	int h = 0;
1157 	u_int32_t hashes[2] = { 0, 0 };
1158 
1159 	ifp = sc->dc_ifp;
1160 
1161 	/* Init our MAC address. */
1162 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
1163 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[0]);
1164 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[1]);
1165 
1166 	/* If we want promiscuous mode, set the allframes bit. */
1167 	if (ifp->if_flags & IFF_PROMISC)
1168 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1169 	else
1170 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1171 
1172 	if (ifp->if_flags & IFF_ALLMULTI)
1173 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1174 	else
1175 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1176 
1177 	/* First, zot all the existing hash bits. */
1178 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
1179 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
1180 
1181 	/*
1182 	 * If we're already in promisc or allmulti mode, we
1183 	 * don't have to bother programming the multicast filter.
1184 	 */
1185 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
1186 		return;
1187 
1188 	/* Now program new ones. */
1189 	IF_ADDR_LOCK(ifp);
1190 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1191 		if (ifma->ifma_addr->sa_family != AF_LINK)
1192 			continue;
1193 		if (DC_IS_CENTAUR(sc))
1194 			h = dc_mchash_le(sc,
1195 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1196 		else
1197 			h = dc_mchash_be(
1198 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1199 		if (h < 32)
1200 			hashes[0] |= (1 << h);
1201 		else
1202 			hashes[1] |= (1 << (h - 32));
1203 	}
1204 	IF_ADDR_UNLOCK(ifp);
1205 
1206 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
1207 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
1208 }
1209 
1210 static void
1211 dc_setfilt_asix(struct dc_softc *sc)
1212 {
1213 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
1214 	struct ifnet *ifp;
1215 	struct ifmultiaddr *ifma;
1216 	int h = 0;
1217 	u_int32_t hashes[2] = { 0, 0 };
1218 
1219 	ifp = sc->dc_ifp;
1220 
1221 	/* Init our MAC address. */
1222 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
1223 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
1224 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
1225 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
1226 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
1227 
1228 	/* If we want promiscuous mode, set the allframes bit. */
1229 	if (ifp->if_flags & IFF_PROMISC)
1230 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1231 	else
1232 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1233 
1234 	if (ifp->if_flags & IFF_ALLMULTI)
1235 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1236 	else
1237 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1238 
1239 	/*
1240 	 * The ASIX chip has a special bit to enable reception
1241 	 * of broadcast frames.
1242 	 */
1243 	if (ifp->if_flags & IFF_BROADCAST)
1244 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
1245 	else
1246 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
1247 
1248 	/* first, zot all the existing hash bits */
1249 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
1250 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
1251 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
1252 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
1253 
1254 	/*
1255 	 * If we're already in promisc or allmulti mode, we
1256 	 * don't have to bother programming the multicast filter.
1257 	 */
1258 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
1259 		return;
1260 
1261 	/* now program new ones */
1262 	IF_ADDR_LOCK(ifp);
1263 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1264 		if (ifma->ifma_addr->sa_family != AF_LINK)
1265 			continue;
1266 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1267 		if (h < 32)
1268 			hashes[0] |= (1 << h);
1269 		else
1270 			hashes[1] |= (1 << (h - 32));
1271 	}
1272 	IF_ADDR_UNLOCK(ifp);
1273 
1274 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
1275 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
1276 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
1277 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
1278 }
1279 
1280 static void
1281 dc_setfilt_xircom(struct dc_softc *sc)
1282 {
1283 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
1284 	struct ifnet *ifp;
1285 	struct ifmultiaddr *ifma;
1286 	struct dc_desc *sframe;
1287 	u_int32_t h, *sp;
1288 	int i;
1289 
1290 	ifp = sc->dc_ifp;
1291 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1292 
1293 	i = sc->dc_cdata.dc_tx_prod;
1294 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1295 	sc->dc_cdata.dc_tx_cnt++;
1296 	sframe = &sc->dc_ldata->dc_tx_list[i];
1297 	sp = sc->dc_cdata.dc_sbuf;
1298 	bzero(sp, DC_SFRAME_LEN);
1299 
1300 	sframe->dc_data = htole32(sc->dc_saddr);
1301 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1302 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1303 
1304 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1305 
1306 	/* If we want promiscuous mode, set the allframes bit. */
1307 	if (ifp->if_flags & IFF_PROMISC)
1308 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1309 	else
1310 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1311 
1312 	if (ifp->if_flags & IFF_ALLMULTI)
1313 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1314 	else
1315 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1316 
1317 	IF_ADDR_LOCK(ifp);
1318 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1319 		if (ifma->ifma_addr->sa_family != AF_LINK)
1320 			continue;
1321 		h = dc_mchash_le(sc,
1322 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1323 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1324 	}
1325 	IF_ADDR_UNLOCK(ifp);
1326 
1327 	if (ifp->if_flags & IFF_BROADCAST) {
1328 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1329 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1330 	}
1331 
1332 	/* Set our MAC address. */
1333 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
1334 	sp[0] = DC_SP_MAC(eaddr[0]);
1335 	sp[1] = DC_SP_MAC(eaddr[1]);
1336 	sp[2] = DC_SP_MAC(eaddr[2]);
1337 
1338 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1339 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1340 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1341 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
1342 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1343 
1344 	/*
1345 	 * Wait some time...
1346 	 */
1347 	DELAY(1000);
1348 
1349 	ifp->if_timer = 5;
1350 }
1351 
1352 static void
1353 dc_setfilt(struct dc_softc *sc)
1354 {
1355 
1356 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
1357 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
1358 		dc_setfilt_21143(sc);
1359 
1360 	if (DC_IS_ASIX(sc))
1361 		dc_setfilt_asix(sc);
1362 
1363 	if (DC_IS_ADMTEK(sc))
1364 		dc_setfilt_admtek(sc);
1365 
1366 	if (DC_IS_XIRCOM(sc))
1367 		dc_setfilt_xircom(sc);
1368 }
1369 
1370 /*
1371  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
1372  * the netconfig register, we first have to put the transmit and/or
1373  * receive logic in the idle state.
1374  */
1375 static void
1376 dc_setcfg(struct dc_softc *sc, int media)
1377 {
1378 	int i, restart = 0, watchdogreg;
1379 	u_int32_t isr;
1380 
1381 	if (IFM_SUBTYPE(media) == IFM_NONE)
1382 		return;
1383 
1384 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
1385 		restart = 1;
1386 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1387 
1388 		for (i = 0; i < DC_TIMEOUT; i++) {
1389 			isr = CSR_READ_4(sc, DC_ISR);
1390 			if (isr & DC_ISR_TX_IDLE &&
1391 			    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1392 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
1393 				break;
1394 			DELAY(10);
1395 		}
1396 
1397 		if (i == DC_TIMEOUT)
1398 			if_printf(sc->dc_ifp,
1399 			    "failed to force tx and rx to idle state\n");
1400 	}
1401 
1402 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1403 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1404 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
1405 		if (sc->dc_pmode == DC_PMODE_MII) {
1406 			if (DC_IS_INTEL(sc)) {
1407 			/* There's a write enable bit here that reads as 1. */
1408 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
1409 				watchdogreg &= ~DC_WDOG_CTLWREN;
1410 				watchdogreg |= DC_WDOG_JABBERDIS;
1411 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1412 			} else {
1413 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1414 			}
1415 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
1416 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
1417 			if (sc->dc_type == DC_TYPE_98713)
1418 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
1419 				    DC_NETCFG_SCRAMBLER));
1420 			if (!DC_IS_DAVICOM(sc))
1421 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1422 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1423 			if (DC_IS_INTEL(sc))
1424 				dc_apply_fixup(sc, IFM_AUTO);
1425 		} else {
1426 			if (DC_IS_PNIC(sc)) {
1427 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
1428 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
1429 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
1430 			}
1431 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1432 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1433 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
1434 			if (DC_IS_INTEL(sc))
1435 				dc_apply_fixup(sc,
1436 				    (media & IFM_GMASK) == IFM_FDX ?
1437 				    IFM_100_TX | IFM_FDX : IFM_100_TX);
1438 		}
1439 	}
1440 
1441 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1442 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1443 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
1444 		if (sc->dc_pmode == DC_PMODE_MII) {
1445 			/* There's a write enable bit here that reads as 1. */
1446 			if (DC_IS_INTEL(sc)) {
1447 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
1448 				watchdogreg &= ~DC_WDOG_CTLWREN;
1449 				watchdogreg |= DC_WDOG_JABBERDIS;
1450 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1451 			} else {
1452 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1453 			}
1454 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
1455 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
1456 			if (sc->dc_type == DC_TYPE_98713)
1457 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1458 			if (!DC_IS_DAVICOM(sc))
1459 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1460 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1461 			if (DC_IS_INTEL(sc))
1462 				dc_apply_fixup(sc, IFM_AUTO);
1463 		} else {
1464 			if (DC_IS_PNIC(sc)) {
1465 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
1466 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
1467 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
1468 			}
1469 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1470 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1471 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
1472 			if (DC_IS_INTEL(sc)) {
1473 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
1474 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1475 				if ((media & IFM_GMASK) == IFM_FDX)
1476 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
1477 				else
1478 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
1479 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1480 				DC_CLRBIT(sc, DC_10BTCTRL,
1481 				    DC_TCTL_AUTONEGENBL);
1482 				dc_apply_fixup(sc,
1483 				    (media & IFM_GMASK) == IFM_FDX ?
1484 				    IFM_10_T | IFM_FDX : IFM_10_T);
1485 				DELAY(20000);
1486 			}
1487 		}
1488 	}
1489 
1490 	/*
1491 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1492 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1493 	 * on the external MII port.
1494 	 */
1495 	if (DC_IS_DAVICOM(sc)) {
1496 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1497 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1498 			sc->dc_link = 1;
1499 		} else {
1500 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1501 		}
1502 	}
1503 
1504 	if ((media & IFM_GMASK) == IFM_FDX) {
1505 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
1506 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
1507 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
1508 	} else {
1509 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
1510 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
1511 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
1512 	}
1513 
1514 	if (restart)
1515 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
1516 }
1517 
1518 static void
1519 dc_reset(struct dc_softc *sc)
1520 {
1521 	int i;
1522 
1523 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
1524 
1525 	for (i = 0; i < DC_TIMEOUT; i++) {
1526 		DELAY(10);
1527 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
1528 			break;
1529 	}
1530 
1531 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
1532 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
1533 		DELAY(10000);
1534 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
1535 		i = 0;
1536 	}
1537 
1538 	if (i == DC_TIMEOUT)
1539 		if_printf(sc->dc_ifp, "reset never completed!\n");
1540 
1541 	/* Wait a little while for the chip to get its brains in order. */
1542 	DELAY(1000);
1543 
1544 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
1545 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
1546 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
1547 
1548 	/*
1549 	 * Bring the SIA out of reset. In some cases, it looks
1550 	 * like failing to unreset the SIA soon enough gets it
1551 	 * into a state where it will never come out of reset
1552 	 * until we reset the whole chip again.
1553 	 */
1554 	if (DC_IS_INTEL(sc)) {
1555 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
1556 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
1557 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
1558 	}
1559 }
1560 
1561 static struct dc_type *
1562 dc_devtype(device_t dev)
1563 {
1564 	struct dc_type *t;
1565 	u_int32_t devid;
1566 	u_int8_t rev;
1567 
1568 	t = dc_devs;
1569 	devid = pci_get_devid(dev);
1570 	rev = pci_get_revid(dev);
1571 
1572 	while (t->dc_name != NULL) {
1573 		if (devid == t->dc_devid && rev >= t->dc_minrev)
1574 			return (t);
1575 		t++;
1576 	}
1577 
1578 	return (NULL);
1579 }
1580 
1581 /*
1582  * Probe for a 21143 or clone chip. Check the PCI vendor and device
1583  * IDs against our list and return a device name if we find a match.
1584  * We do a little bit of extra work to identify the exact type of
1585  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
1586  * but different revision IDs. The same is true for 98715/98715A
1587  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
1588  * cases, the exact chip revision affects driver behavior.
1589  */
1590 static int
1591 dc_probe(device_t dev)
1592 {
1593 	struct dc_type *t;
1594 
1595 	t = dc_devtype(dev);
1596 
1597 	if (t != NULL) {
1598 		device_set_desc(dev, t->dc_name);
1599 		return (BUS_PROBE_DEFAULT);
1600 	}
1601 
1602 	return (ENXIO);
1603 }
1604 
1605 static void
1606 dc_apply_fixup(struct dc_softc *sc, int media)
1607 {
1608 	struct dc_mediainfo *m;
1609 	u_int8_t *p;
1610 	int i;
1611 	u_int32_t reg;
1612 
1613 	m = sc->dc_mi;
1614 
1615 	while (m != NULL) {
1616 		if (m->dc_media == media)
1617 			break;
1618 		m = m->dc_next;
1619 	}
1620 
1621 	if (m == NULL)
1622 		return;
1623 
1624 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
1625 		reg = (p[0] | (p[1] << 8)) << 16;
1626 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
1627 	}
1628 
1629 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
1630 		reg = (p[0] | (p[1] << 8)) << 16;
1631 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
1632 	}
1633 }
1634 
1635 static void
1636 dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
1637 {
1638 	struct dc_mediainfo *m;
1639 
1640 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1641 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
1642 	case DC_SIA_CODE_10BT:
1643 		m->dc_media = IFM_10_T;
1644 		break;
1645 	case DC_SIA_CODE_10BT_FDX:
1646 		m->dc_media = IFM_10_T | IFM_FDX;
1647 		break;
1648 	case DC_SIA_CODE_10B2:
1649 		m->dc_media = IFM_10_2;
1650 		break;
1651 	case DC_SIA_CODE_10B5:
1652 		m->dc_media = IFM_10_5;
1653 		break;
1654 	default:
1655 		break;
1656 	}
1657 
1658 	/*
1659 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
1660 	 * Things apparently already work for cards that do
1661 	 * supply Media Specific Data.
1662 	 */
1663 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
1664 		m->dc_gp_len = 2;
1665 		m->dc_gp_ptr =
1666 		(u_int8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
1667 	} else {
1668 		m->dc_gp_len = 2;
1669 		m->dc_gp_ptr =
1670 		(u_int8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
1671 	}
1672 
1673 	m->dc_next = sc->dc_mi;
1674 	sc->dc_mi = m;
1675 
1676 	sc->dc_pmode = DC_PMODE_SIA;
1677 }
1678 
1679 static void
1680 dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
1681 {
1682 	struct dc_mediainfo *m;
1683 
1684 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1685 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
1686 		m->dc_media = IFM_100_TX;
1687 
1688 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
1689 		m->dc_media = IFM_100_TX | IFM_FDX;
1690 
1691 	m->dc_gp_len = 2;
1692 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
1693 
1694 	m->dc_next = sc->dc_mi;
1695 	sc->dc_mi = m;
1696 
1697 	sc->dc_pmode = DC_PMODE_SYM;
1698 }
1699 
1700 static void
1701 dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
1702 {
1703 	struct dc_mediainfo *m;
1704 	u_int8_t *p;
1705 
1706 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1707 	/* We abuse IFM_AUTO to represent MII. */
1708 	m->dc_media = IFM_AUTO;
1709 	m->dc_gp_len = l->dc_gpr_len;
1710 
1711 	p = (u_int8_t *)l;
1712 	p += sizeof(struct dc_eblock_mii);
1713 	m->dc_gp_ptr = p;
1714 	p += 2 * l->dc_gpr_len;
1715 	m->dc_reset_len = *p;
1716 	p++;
1717 	m->dc_reset_ptr = p;
1718 
1719 	m->dc_next = sc->dc_mi;
1720 	sc->dc_mi = m;
1721 }
1722 
1723 static void
1724 dc_read_srom(struct dc_softc *sc, int bits)
1725 {
1726 	int size;
1727 
1728 	size = 2 << bits;
1729 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
1730 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1731 }
1732 
1733 static void
1734 dc_parse_21143_srom(struct dc_softc *sc)
1735 {
1736 	struct dc_leaf_hdr *lhdr;
1737 	struct dc_eblock_hdr *hdr;
1738 	int have_mii, i, loff;
1739 	char *ptr;
1740 
1741 	have_mii = 0;
1742 	loff = sc->dc_srom[27];
1743 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
1744 
1745 	ptr = (char *)lhdr;
1746 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1747 	/*
1748 	 * Look if we got a MII media block.
1749 	 */
1750 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1751 		hdr = (struct dc_eblock_hdr *)ptr;
1752 		if (hdr->dc_type == DC_EBLOCK_MII)
1753 		    have_mii++;
1754 
1755 		ptr += (hdr->dc_len & 0x7F);
1756 		ptr++;
1757 	}
1758 
1759 	/*
1760 	 * Do the same thing again. Only use SIA and SYM media
1761 	 * blocks if no MII media block is available.
1762 	 */
1763 	ptr = (char *)lhdr;
1764 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1765 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1766 		hdr = (struct dc_eblock_hdr *)ptr;
1767 		switch (hdr->dc_type) {
1768 		case DC_EBLOCK_MII:
1769 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
1770 			break;
1771 		case DC_EBLOCK_SIA:
1772 			if (! have_mii)
1773 				dc_decode_leaf_sia(sc,
1774 				    (struct dc_eblock_sia *)hdr);
1775 			break;
1776 		case DC_EBLOCK_SYM:
1777 			if (! have_mii)
1778 				dc_decode_leaf_sym(sc,
1779 				    (struct dc_eblock_sym *)hdr);
1780 			break;
1781 		default:
1782 			/* Don't care. Yet. */
1783 			break;
1784 		}
1785 		ptr += (hdr->dc_len & 0x7F);
1786 		ptr++;
1787 	}
1788 }
1789 
1790 static void
1791 dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1792 {
1793 	u_int32_t *paddr;
1794 
1795 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
1796 	paddr = arg;
1797 	*paddr = segs->ds_addr;
1798 }
1799 
1800 /*
1801  * Attach the interface. Allocate softc structures, do ifmedia
1802  * setup and ethernet/BPF attach.
1803  */
1804 static int
1805 dc_attach(device_t dev)
1806 {
1807 	int tmp = 0;
1808 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
1809 	u_int32_t command;
1810 	struct dc_softc *sc;
1811 	struct ifnet *ifp;
1812 	u_int32_t revision;
1813 	int error = 0, rid, mac_offset;
1814 	int i;
1815 	u_int8_t *mac;
1816 
1817 	sc = device_get_softc(dev);
1818 
1819 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1820 	    MTX_DEF);
1821 
1822 	/*
1823 	 * Map control/status registers.
1824 	 */
1825 	pci_enable_busmaster(dev);
1826 
1827 	rid = DC_RID;
1828 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
1829 
1830 	if (sc->dc_res == NULL) {
1831 		device_printf(dev, "couldn't map ports/memory\n");
1832 		error = ENXIO;
1833 		goto fail;
1834 	}
1835 
1836 	sc->dc_btag = rman_get_bustag(sc->dc_res);
1837 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
1838 
1839 	/* Allocate interrupt. */
1840 	rid = 0;
1841 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1842 	    RF_SHAREABLE | RF_ACTIVE);
1843 
1844 	if (sc->dc_irq == NULL) {
1845 		device_printf(dev, "couldn't map interrupt\n");
1846 		error = ENXIO;
1847 		goto fail;
1848 	}
1849 
1850 	/* Need this info to decide on a chip type. */
1851 	sc->dc_info = dc_devtype(dev);
1852 	revision = pci_get_revid(dev);
1853 
1854 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
1855 	if (sc->dc_info->dc_devid !=
1856 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
1857 	    sc->dc_info->dc_devid !=
1858 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
1859 		dc_eeprom_width(sc);
1860 
1861 	switch (sc->dc_info->dc_devid) {
1862 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
1863 		sc->dc_type = DC_TYPE_21143;
1864 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1865 		sc->dc_flags |= DC_REDUCED_MII_POLL;
1866 		/* Save EEPROM contents so we can parse them later. */
1867 		dc_read_srom(sc, sc->dc_romwidth);
1868 		break;
1869 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
1870 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
1871 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
1872 		sc->dc_type = DC_TYPE_DM9102;
1873 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
1874 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
1875 		sc->dc_flags |= DC_TX_ALIGN;
1876 		sc->dc_pmode = DC_PMODE_MII;
1877 
1878 		/* Increase the latency timer value. */
1879 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
1880 		break;
1881 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
1882 		sc->dc_type = DC_TYPE_AL981;
1883 		sc->dc_flags |= DC_TX_USE_TX_INTR;
1884 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
1885 		sc->dc_pmode = DC_PMODE_MII;
1886 		dc_read_srom(sc, sc->dc_romwidth);
1887 		break;
1888 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
1889 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
1890 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
1891 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
1892 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_FA511):
1893 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
1894 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
1895 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
1896 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
1897 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
1898 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
1899 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
1900 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
1901 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
1902 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
1903 		sc->dc_type = DC_TYPE_AN985;
1904 		sc->dc_flags |= DC_64BIT_HASH;
1905 		sc->dc_flags |= DC_TX_USE_TX_INTR;
1906 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
1907 		sc->dc_pmode = DC_PMODE_MII;
1908 		/* Don't read SROM for - auto-loaded on reset */
1909 		break;
1910 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
1911 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
1912 		if (revision < DC_REVISION_98713A) {
1913 			sc->dc_type = DC_TYPE_98713;
1914 		}
1915 		if (revision >= DC_REVISION_98713A) {
1916 			sc->dc_type = DC_TYPE_98713A;
1917 			sc->dc_flags |= DC_21143_NWAY;
1918 		}
1919 		sc->dc_flags |= DC_REDUCED_MII_POLL;
1920 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1921 		break;
1922 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
1923 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
1924 		/*
1925 		 * Macronix MX98715AEC-C/D/E parts have only a
1926 		 * 128-bit hash table. We need to deal with these
1927 		 * in the same manner as the PNIC II so that we
1928 		 * get the right number of bits out of the
1929 		 * CRC routine.
1930 		 */
1931 		if (revision >= DC_REVISION_98715AEC_C &&
1932 		    revision < DC_REVISION_98725)
1933 			sc->dc_flags |= DC_128BIT_HASH;
1934 		sc->dc_type = DC_TYPE_987x5;
1935 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1936 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
1937 		break;
1938 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
1939 		sc->dc_type = DC_TYPE_987x5;
1940 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1941 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
1942 		break;
1943 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
1944 		sc->dc_type = DC_TYPE_PNICII;
1945 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
1946 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
1947 		break;
1948 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
1949 		sc->dc_type = DC_TYPE_PNIC;
1950 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
1951 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
1952 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
1953 		if (revision < DC_REVISION_82C169)
1954 			sc->dc_pmode = DC_PMODE_SYM;
1955 		break;
1956 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
1957 		sc->dc_type = DC_TYPE_ASIX;
1958 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
1959 		sc->dc_flags |= DC_REDUCED_MII_POLL;
1960 		sc->dc_pmode = DC_PMODE_MII;
1961 		break;
1962 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
1963 		sc->dc_type = DC_TYPE_XIRCOM;
1964 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
1965 				DC_TX_ALIGN;
1966 		/*
1967 		 * We don't actually need to coalesce, but we're doing
1968 		 * it to obtain a double word aligned buffer.
1969 		 * The DC_TX_COALESCE flag is required.
1970 		 */
1971 		sc->dc_pmode = DC_PMODE_MII;
1972 		break;
1973 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
1974 		sc->dc_type = DC_TYPE_CONEXANT;
1975 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
1976 		sc->dc_flags |= DC_REDUCED_MII_POLL;
1977 		sc->dc_pmode = DC_PMODE_MII;
1978 		dc_read_srom(sc, sc->dc_romwidth);
1979 		break;
1980 	default:
1981 		device_printf(dev, "unknown device: %x\n",
1982 		    sc->dc_info->dc_devid);
1983 		break;
1984 	}
1985 
1986 	/* Save the cache line size. */
1987 	if (DC_IS_DAVICOM(sc))
1988 		sc->dc_cachesize = 0;
1989 	else
1990 		sc->dc_cachesize = pci_get_cachelnsz(dev);
1991 
1992 	/* Reset the adapter. */
1993 	dc_reset(sc);
1994 
1995 	/* Take 21143 out of snooze mode */
1996 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
1997 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
1998 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
1999 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
2000 	}
2001 
2002 	/*
2003 	 * Try to learn something about the supported media.
2004 	 * We know that ASIX and ADMtek and Davicom devices
2005 	 * will *always* be using MII media, so that's a no-brainer.
2006 	 * The tricky ones are the Macronix/PNIC II and the
2007 	 * Intel 21143.
2008 	 */
2009 	if (DC_IS_INTEL(sc))
2010 		dc_parse_21143_srom(sc);
2011 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
2012 		if (sc->dc_type == DC_TYPE_98713)
2013 			sc->dc_pmode = DC_PMODE_MII;
2014 		else
2015 			sc->dc_pmode = DC_PMODE_SYM;
2016 	} else if (!sc->dc_pmode)
2017 		sc->dc_pmode = DC_PMODE_MII;
2018 
2019 	/*
2020 	 * Get station address from the EEPROM.
2021 	 */
2022 	switch(sc->dc_type) {
2023 	case DC_TYPE_98713:
2024 	case DC_TYPE_98713A:
2025 	case DC_TYPE_987x5:
2026 	case DC_TYPE_PNICII:
2027 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
2028 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
2029 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
2030 		break;
2031 	case DC_TYPE_PNIC:
2032 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
2033 		break;
2034 	case DC_TYPE_DM9102:
2035 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2036 #ifdef __sparc64__
2037 		/*
2038 		 * If this is an onboard dc(4) the station address read from
2039 		 * the EEPROM is all zero and we have to get it from the FCode.
2040 		 */
2041 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
2042 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2043 #endif
2044 		break;
2045 	case DC_TYPE_21143:
2046 	case DC_TYPE_ASIX:
2047 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2048 		break;
2049 	case DC_TYPE_AL981:
2050 	case DC_TYPE_AN985:
2051 		eaddr[0] = CSR_READ_4(sc, DC_AL_PAR0);
2052 		eaddr[1] = CSR_READ_4(sc, DC_AL_PAR1);
2053 		break;
2054 	case DC_TYPE_CONEXANT:
2055 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
2056 		    ETHER_ADDR_LEN);
2057 		break;
2058 	case DC_TYPE_XIRCOM:
2059 		/* The MAC comes from the CIS. */
2060 		mac = pci_get_ether(dev);
2061 		if (!mac) {
2062 			device_printf(dev, "No station address in CIS!\n");
2063 			error = ENXIO;
2064 			goto fail;
2065 		}
2066 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2067 		break;
2068 	default:
2069 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2070 		break;
2071 	}
2072 
2073 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
2074 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
2075 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct dc_list_data), 1,
2076 	    sizeof(struct dc_list_data), 0, NULL, NULL, &sc->dc_ltag);
2077 	if (error) {
2078 		device_printf(dev, "failed to allocate busdma tag\n");
2079 		error = ENXIO;
2080 		goto fail;
2081 	}
2082 	error = bus_dmamem_alloc(sc->dc_ltag, (void **)&sc->dc_ldata,
2083 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->dc_lmap);
2084 	if (error) {
2085 		device_printf(dev, "failed to allocate DMA safe memory\n");
2086 		error = ENXIO;
2087 		goto fail;
2088 	}
2089 	error = bus_dmamap_load(sc->dc_ltag, sc->dc_lmap, sc->dc_ldata,
2090 	    sizeof(struct dc_list_data), dc_dma_map_addr, &sc->dc_laddr,
2091 	    BUS_DMA_NOWAIT);
2092 	if (error) {
2093 		device_printf(dev, "cannot get address of the descriptors\n");
2094 		error = ENXIO;
2095 		goto fail;
2096 	}
2097 
2098 	/*
2099 	 * Allocate a busdma tag and DMA safe memory for the multicast
2100 	 * setup frame.
2101 	 */
2102 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
2103 	    BUS_SPACE_MAXADDR, NULL, NULL, DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1,
2104 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 0, NULL, NULL, &sc->dc_stag);
2105 	if (error) {
2106 		device_printf(dev, "failed to allocate busdma tag\n");
2107 		error = ENXIO;
2108 		goto fail;
2109 	}
2110 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
2111 	    BUS_DMA_NOWAIT, &sc->dc_smap);
2112 	if (error) {
2113 		device_printf(dev, "failed to allocate DMA safe memory\n");
2114 		error = ENXIO;
2115 		goto fail;
2116 	}
2117 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
2118 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
2119 	if (error) {
2120 		device_printf(dev, "cannot get address of the descriptors\n");
2121 		error = ENXIO;
2122 		goto fail;
2123 	}
2124 
2125 	/* Allocate a busdma tag for mbufs. */
2126 	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
2127 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, DC_TX_LIST_CNT, MCLBYTES,
2128 	    0, NULL, NULL, &sc->dc_mtag);
2129 	if (error) {
2130 		device_printf(dev, "failed to allocate busdma tag\n");
2131 		error = ENXIO;
2132 		goto fail;
2133 	}
2134 
2135 	/* Create the TX/RX busdma maps. */
2136 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2137 		error = bus_dmamap_create(sc->dc_mtag, 0,
2138 		    &sc->dc_cdata.dc_tx_map[i]);
2139 		if (error) {
2140 			device_printf(dev, "failed to init TX ring\n");
2141 			error = ENXIO;
2142 			goto fail;
2143 		}
2144 	}
2145 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
2146 		error = bus_dmamap_create(sc->dc_mtag, 0,
2147 		    &sc->dc_cdata.dc_rx_map[i]);
2148 		if (error) {
2149 			device_printf(dev, "failed to init RX ring\n");
2150 			error = ENXIO;
2151 			goto fail;
2152 		}
2153 	}
2154 	error = bus_dmamap_create(sc->dc_mtag, 0, &sc->dc_sparemap);
2155 	if (error) {
2156 		device_printf(dev, "failed to init RX ring\n");
2157 		error = ENXIO;
2158 		goto fail;
2159 	}
2160 
2161 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2162 	if (ifp == NULL) {
2163 		device_printf(dev, "can not if_alloc()\n");
2164 		error = ENOSPC;
2165 		goto fail;
2166 	}
2167 	ifp->if_softc = sc;
2168 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2169 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
2170 	ifp->if_mtu = ETHERMTU;
2171 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2172 	ifp->if_ioctl = dc_ioctl;
2173 	ifp->if_start = dc_start;
2174 	ifp->if_watchdog = dc_watchdog;
2175 	ifp->if_init = dc_init;
2176 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2177 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2178 	IFQ_SET_READY(&ifp->if_snd);
2179 
2180 	/*
2181 	 * Do MII setup. If this is a 21143, check for a PHY on the
2182 	 * MII bus after applying any necessary fixups to twiddle the
2183 	 * GPIO bits. If we don't end up finding a PHY, restore the
2184 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
2185 	 * driver instead.
2186 	 */
2187 	if (DC_IS_INTEL(sc)) {
2188 		dc_apply_fixup(sc, IFM_AUTO);
2189 		tmp = sc->dc_pmode;
2190 		sc->dc_pmode = DC_PMODE_MII;
2191 	}
2192 
2193 	/*
2194 	 * Setup General Purpose port mode and data so the tulip can talk
2195 	 * to the MII.  This needs to be done before mii_phy_probe so that
2196 	 * we can actually see them.
2197 	 */
2198 	if (DC_IS_XIRCOM(sc)) {
2199 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
2200 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2201 		DELAY(10);
2202 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
2203 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2204 		DELAY(10);
2205 	}
2206 
2207 	error = mii_phy_probe(dev, &sc->dc_miibus,
2208 	    dc_ifmedia_upd, dc_ifmedia_sts);
2209 
2210 	if (error && DC_IS_INTEL(sc)) {
2211 		sc->dc_pmode = tmp;
2212 		if (sc->dc_pmode != DC_PMODE_SIA)
2213 			sc->dc_pmode = DC_PMODE_SYM;
2214 		sc->dc_flags |= DC_21143_NWAY;
2215 		mii_phy_probe(dev, &sc->dc_miibus,
2216 		    dc_ifmedia_upd, dc_ifmedia_sts);
2217 		/*
2218 		 * For non-MII cards, we need to have the 21143
2219 		 * drive the LEDs. Except there are some systems
2220 		 * like the NEC VersaPro NoteBook PC which have no
2221 		 * LEDs, and twiddling these bits has adverse effects
2222 		 * on them. (I.e. you suddenly can't get a link.)
2223 		 */
2224 		if (!(pci_get_subvendor(dev) == 0x1033 &&
2225 		    pci_get_subdevice(dev) == 0x8028))
2226 			sc->dc_flags |= DC_TULIP_LEDS;
2227 		error = 0;
2228 	}
2229 
2230 	if (error) {
2231 		device_printf(dev, "MII without any PHY!\n");
2232 		goto fail;
2233 	}
2234 
2235 	if (DC_IS_ADMTEK(sc)) {
2236 		/*
2237 		 * Set automatic TX underrun recovery for the ADMtek chips
2238 		 */
2239 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2240 	}
2241 
2242 	/*
2243 	 * Tell the upper layer(s) we support long frames.
2244 	 */
2245 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
2246 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
2247 	ifp->if_capenable = ifp->if_capabilities;
2248 #ifdef DEVICE_POLLING
2249 	ifp->if_capabilities |= IFCAP_POLLING;
2250 #endif
2251 
2252 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2253 
2254 	/*
2255 	 * Call MI attach routine.
2256 	 */
2257 	ether_ifattach(ifp, (caddr_t)eaddr);
2258 
2259 	/* Hook interrupt last to avoid having to lock softc */
2260 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2261 	    dc_intr, sc, &sc->dc_intrhand);
2262 
2263 	if (error) {
2264 		device_printf(dev, "couldn't set up irq\n");
2265 		ether_ifdetach(ifp);
2266 		goto fail;
2267 	}
2268 
2269 fail:
2270 	if (error)
2271 		dc_detach(dev);
2272 	return (error);
2273 }
2274 
2275 /*
2276  * Shutdown hardware and free up resources. This can be called any
2277  * time after the mutex has been initialized. It is called in both
2278  * the error case in attach and the normal detach case so it needs
2279  * to be careful about only freeing resources that have actually been
2280  * allocated.
2281  */
2282 static int
2283 dc_detach(device_t dev)
2284 {
2285 	struct dc_softc *sc;
2286 	struct ifnet *ifp;
2287 	struct dc_mediainfo *m;
2288 	int i;
2289 
2290 	sc = device_get_softc(dev);
2291 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2292 
2293 	ifp = sc->dc_ifp;
2294 
2295 #ifdef DEVICE_POLLING
2296 	if (ifp->if_capenable & IFCAP_POLLING)
2297 		ether_poll_deregister(ifp);
2298 #endif
2299 
2300 	/* These should only be active if attach succeeded */
2301 	if (device_is_attached(dev)) {
2302 		DC_LOCK(sc);
2303 		dc_stop(sc);
2304 		DC_UNLOCK(sc);
2305 		callout_drain(&sc->dc_stat_ch);
2306 		ether_ifdetach(ifp);
2307 	}
2308 	if (sc->dc_miibus)
2309 		device_delete_child(dev, sc->dc_miibus);
2310 	bus_generic_detach(dev);
2311 
2312 	if (sc->dc_intrhand)
2313 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
2314 	if (sc->dc_irq)
2315 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
2316 	if (sc->dc_res)
2317 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
2318 
2319 	if (ifp)
2320 		if_free(ifp);
2321 
2322 	if (sc->dc_cdata.dc_sbuf != NULL)
2323 		bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf, sc->dc_smap);
2324 	if (sc->dc_ldata != NULL)
2325 		bus_dmamem_free(sc->dc_ltag, sc->dc_ldata, sc->dc_lmap);
2326 	if (sc->dc_mtag) {
2327 		for (i = 0; i < DC_TX_LIST_CNT; i++)
2328 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
2329 				bus_dmamap_destroy(sc->dc_mtag,
2330 				    sc->dc_cdata.dc_tx_map[i]);
2331 		for (i = 0; i < DC_RX_LIST_CNT; i++)
2332 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
2333 				bus_dmamap_destroy(sc->dc_mtag,
2334 				    sc->dc_cdata.dc_rx_map[i]);
2335 		bus_dmamap_destroy(sc->dc_mtag, sc->dc_sparemap);
2336 	}
2337 	if (sc->dc_stag)
2338 		bus_dma_tag_destroy(sc->dc_stag);
2339 	if (sc->dc_mtag)
2340 		bus_dma_tag_destroy(sc->dc_mtag);
2341 	if (sc->dc_ltag)
2342 		bus_dma_tag_destroy(sc->dc_ltag);
2343 
2344 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
2345 
2346 	while (sc->dc_mi != NULL) {
2347 		m = sc->dc_mi->dc_next;
2348 		free(sc->dc_mi, M_DEVBUF);
2349 		sc->dc_mi = m;
2350 	}
2351 	free(sc->dc_srom, M_DEVBUF);
2352 
2353 	mtx_destroy(&sc->dc_mtx);
2354 
2355 	return (0);
2356 }
2357 
2358 /*
2359  * Initialize the transmit descriptors.
2360  */
2361 static int
2362 dc_list_tx_init(struct dc_softc *sc)
2363 {
2364 	struct dc_chain_data *cd;
2365 	struct dc_list_data *ld;
2366 	int i, nexti;
2367 
2368 	cd = &sc->dc_cdata;
2369 	ld = sc->dc_ldata;
2370 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2371 		if (i == DC_TX_LIST_CNT - 1)
2372 			nexti = 0;
2373 		else
2374 			nexti = i + 1;
2375 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
2376 		cd->dc_tx_chain[i] = NULL;
2377 		ld->dc_tx_list[i].dc_data = 0;
2378 		ld->dc_tx_list[i].dc_ctl = 0;
2379 	}
2380 
2381 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
2382 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
2383 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2384 	return (0);
2385 }
2386 
2387 
2388 /*
2389  * Initialize the RX descriptors and allocate mbufs for them. Note that
2390  * we arrange the descriptors in a closed ring, so that the last descriptor
2391  * points back to the first.
2392  */
2393 static int
2394 dc_list_rx_init(struct dc_softc *sc)
2395 {
2396 	struct dc_chain_data *cd;
2397 	struct dc_list_data *ld;
2398 	int i, nexti;
2399 
2400 	cd = &sc->dc_cdata;
2401 	ld = sc->dc_ldata;
2402 
2403 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
2404 		if (dc_newbuf(sc, i, 1) != 0)
2405 			return (ENOBUFS);
2406 		if (i == DC_RX_LIST_CNT - 1)
2407 			nexti = 0;
2408 		else
2409 			nexti = i + 1;
2410 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
2411 	}
2412 
2413 	cd->dc_rx_prod = 0;
2414 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
2415 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2416 	return (0);
2417 }
2418 
2419 static void
2420 dc_dma_map_rxbuf(arg, segs, nseg, mapsize, error)
2421 	void *arg;
2422 	bus_dma_segment_t *segs;
2423 	int nseg;
2424 	bus_size_t mapsize;
2425 	int error;
2426 {
2427 	struct dc_softc *sc;
2428 	struct dc_desc *c;
2429 
2430 	sc = arg;
2431 	c = &sc->dc_ldata->dc_rx_list[sc->dc_cdata.dc_rx_cur];
2432 	if (error) {
2433 		sc->dc_cdata.dc_rx_err = error;
2434 		return;
2435 	}
2436 
2437 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
2438 	sc->dc_cdata.dc_rx_err = 0;
2439 	c->dc_data = htole32(segs->ds_addr);
2440 }
2441 
2442 /*
2443  * Initialize an RX descriptor and attach an MBUF cluster.
2444  */
2445 static int
2446 dc_newbuf(struct dc_softc *sc, int i, int alloc)
2447 {
2448 	struct mbuf *m_new;
2449 	bus_dmamap_t tmp;
2450 	int error;
2451 
2452 	if (alloc) {
2453 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2454 		if (m_new == NULL)
2455 			return (ENOBUFS);
2456 	} else {
2457 		m_new = sc->dc_cdata.dc_rx_chain[i];
2458 		m_new->m_data = m_new->m_ext.ext_buf;
2459 	}
2460 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
2461 	m_adj(m_new, sizeof(u_int64_t));
2462 
2463 	/*
2464 	 * If this is a PNIC chip, zero the buffer. This is part
2465 	 * of the workaround for the receive bug in the 82c168 and
2466 	 * 82c169 chips.
2467 	 */
2468 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
2469 		bzero(mtod(m_new, char *), m_new->m_len);
2470 
2471 	/* No need to remap the mbuf if we're reusing it. */
2472 	if (alloc) {
2473 		sc->dc_cdata.dc_rx_cur = i;
2474 		error = bus_dmamap_load_mbuf(sc->dc_mtag, sc->dc_sparemap,
2475 		    m_new, dc_dma_map_rxbuf, sc, 0);
2476 		if (error) {
2477 			m_freem(m_new);
2478 			return (error);
2479 		}
2480 		if (sc->dc_cdata.dc_rx_err != 0) {
2481 			m_freem(m_new);
2482 			return (sc->dc_cdata.dc_rx_err);
2483 		}
2484 		bus_dmamap_unload(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i]);
2485 		tmp = sc->dc_cdata.dc_rx_map[i];
2486 		sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
2487 		sc->dc_sparemap = tmp;
2488 		sc->dc_cdata.dc_rx_chain[i] = m_new;
2489 	}
2490 
2491 	sc->dc_ldata->dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
2492 	sc->dc_ldata->dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
2493 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
2494 	    BUS_DMASYNC_PREREAD);
2495 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
2496 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
2497 	return (0);
2498 }
2499 
2500 /*
2501  * Grrrrr.
2502  * The PNIC chip has a terrible bug in it that manifests itself during
2503  * periods of heavy activity. The exact mode of failure if difficult to
2504  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
2505  * will happen on slow machines. The bug is that sometimes instead of
2506  * uploading one complete frame during reception, it uploads what looks
2507  * like the entire contents of its FIFO memory. The frame we want is at
2508  * the end of the whole mess, but we never know exactly how much data has
2509  * been uploaded, so salvaging the frame is hard.
2510  *
2511  * There is only one way to do it reliably, and it's disgusting.
2512  * Here's what we know:
2513  *
2514  * - We know there will always be somewhere between one and three extra
2515  *   descriptors uploaded.
2516  *
2517  * - We know the desired received frame will always be at the end of the
2518  *   total data upload.
2519  *
2520  * - We know the size of the desired received frame because it will be
2521  *   provided in the length field of the status word in the last descriptor.
2522  *
2523  * Here's what we do:
2524  *
2525  * - When we allocate buffers for the receive ring, we bzero() them.
2526  *   This means that we know that the buffer contents should be all
2527  *   zeros, except for data uploaded by the chip.
2528  *
2529  * - We also force the PNIC chip to upload frames that include the
2530  *   ethernet CRC at the end.
2531  *
2532  * - We gather all of the bogus frame data into a single buffer.
2533  *
2534  * - We then position a pointer at the end of this buffer and scan
2535  *   backwards until we encounter the first non-zero byte of data.
2536  *   This is the end of the received frame. We know we will encounter
2537  *   some data at the end of the frame because the CRC will always be
2538  *   there, so even if the sender transmits a packet of all zeros,
2539  *   we won't be fooled.
2540  *
2541  * - We know the size of the actual received frame, so we subtract
2542  *   that value from the current pointer location. This brings us
2543  *   to the start of the actual received packet.
2544  *
2545  * - We copy this into an mbuf and pass it on, along with the actual
2546  *   frame length.
2547  *
2548  * The performance hit is tremendous, but it beats dropping frames all
2549  * the time.
2550  */
2551 
2552 #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2553 static void
2554 dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
2555 {
2556 	struct dc_desc *cur_rx;
2557 	struct dc_desc *c = NULL;
2558 	struct mbuf *m = NULL;
2559 	unsigned char *ptr;
2560 	int i, total_len;
2561 	u_int32_t rxstat = 0;
2562 
2563 	i = sc->dc_pnic_rx_bug_save;
2564 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
2565 	ptr = sc->dc_pnic_rx_buf;
2566 	bzero(ptr, DC_RXLEN * 5);
2567 
2568 	/* Copy all the bytes from the bogus buffers. */
2569 	while (1) {
2570 		c = &sc->dc_ldata->dc_rx_list[i];
2571 		rxstat = le32toh(c->dc_status);
2572 		m = sc->dc_cdata.dc_rx_chain[i];
2573 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
2574 		ptr += DC_RXLEN;
2575 		/* If this is the last buffer, break out. */
2576 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
2577 			break;
2578 		dc_newbuf(sc, i, 0);
2579 		DC_INC(i, DC_RX_LIST_CNT);
2580 	}
2581 
2582 	/* Find the length of the actual receive frame. */
2583 	total_len = DC_RXBYTES(rxstat);
2584 
2585 	/* Scan backwards until we hit a non-zero byte. */
2586 	while (*ptr == 0x00)
2587 		ptr--;
2588 
2589 	/* Round off. */
2590 	if ((uintptr_t)(ptr) & 0x3)
2591 		ptr -= 1;
2592 
2593 	/* Now find the start of the frame. */
2594 	ptr -= total_len;
2595 	if (ptr < sc->dc_pnic_rx_buf)
2596 		ptr = sc->dc_pnic_rx_buf;
2597 
2598 	/*
2599 	 * Now copy the salvaged frame to the last mbuf and fake up
2600 	 * the status word to make it look like a successful
2601 	 * frame reception.
2602 	 */
2603 	dc_newbuf(sc, i, 0);
2604 	bcopy(ptr, mtod(m, char *), total_len);
2605 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
2606 }
2607 
2608 /*
2609  * This routine searches the RX ring for dirty descriptors in the
2610  * event that the rxeof routine falls out of sync with the chip's
2611  * current descriptor pointer. This may happen sometimes as a result
2612  * of a "no RX buffer available" condition that happens when the chip
2613  * consumes all of the RX buffers before the driver has a chance to
2614  * process the RX ring. This routine may need to be called more than
2615  * once to bring the driver back in sync with the chip, however we
2616  * should still be getting RX DONE interrupts to drive the search
2617  * for new packets in the RX ring, so we should catch up eventually.
2618  */
2619 static int
2620 dc_rx_resync(struct dc_softc *sc)
2621 {
2622 	struct dc_desc *cur_rx;
2623 	int i, pos;
2624 
2625 	pos = sc->dc_cdata.dc_rx_prod;
2626 
2627 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
2628 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
2629 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
2630 			break;
2631 		DC_INC(pos, DC_RX_LIST_CNT);
2632 	}
2633 
2634 	/* If the ring really is empty, then just return. */
2635 	if (i == DC_RX_LIST_CNT)
2636 		return (0);
2637 
2638 	/* We've fallen behing the chip: catch it. */
2639 	sc->dc_cdata.dc_rx_prod = pos;
2640 
2641 	return (EAGAIN);
2642 }
2643 
2644 /*
2645  * A frame has been uploaded: pass the resulting mbuf chain up to
2646  * the higher level protocols.
2647  */
2648 static void
2649 dc_rxeof(struct dc_softc *sc)
2650 {
2651 	struct mbuf *m;
2652 	struct ifnet *ifp;
2653 	struct dc_desc *cur_rx;
2654 	int i, total_len = 0;
2655 	u_int32_t rxstat;
2656 
2657 	DC_LOCK_ASSERT(sc);
2658 
2659 	ifp = sc->dc_ifp;
2660 	i = sc->dc_cdata.dc_rx_prod;
2661 
2662 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
2663 	while (!(le32toh(sc->dc_ldata->dc_rx_list[i].dc_status) &
2664 	    DC_RXSTAT_OWN)) {
2665 #ifdef DEVICE_POLLING
2666 		if (ifp->if_capenable & IFCAP_POLLING) {
2667 			if (sc->rxcycles <= 0)
2668 				break;
2669 			sc->rxcycles--;
2670 		}
2671 #endif
2672 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
2673 		rxstat = le32toh(cur_rx->dc_status);
2674 		m = sc->dc_cdata.dc_rx_chain[i];
2675 		bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
2676 		    BUS_DMASYNC_POSTREAD);
2677 		total_len = DC_RXBYTES(rxstat);
2678 
2679 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
2680 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
2681 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
2682 					sc->dc_pnic_rx_bug_save = i;
2683 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
2684 					DC_INC(i, DC_RX_LIST_CNT);
2685 					continue;
2686 				}
2687 				dc_pnic_rx_bug_war(sc, i);
2688 				rxstat = le32toh(cur_rx->dc_status);
2689 				total_len = DC_RXBYTES(rxstat);
2690 			}
2691 		}
2692 
2693 		/*
2694 		 * If an error occurs, update stats, clear the
2695 		 * status word and leave the mbuf cluster in place:
2696 		 * it should simply get re-used next time this descriptor
2697 		 * comes up in the ring.  However, don't report long
2698 		 * frames as errors since they could be vlans.
2699 		 */
2700 		if ((rxstat & DC_RXSTAT_RXERR)) {
2701 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2702 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2703 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2704 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
2705 				ifp->if_ierrors++;
2706 				if (rxstat & DC_RXSTAT_COLLSEEN)
2707 					ifp->if_collisions++;
2708 				dc_newbuf(sc, i, 0);
2709 				if (rxstat & DC_RXSTAT_CRCERR) {
2710 					DC_INC(i, DC_RX_LIST_CNT);
2711 					continue;
2712 				} else {
2713 					dc_init_locked(sc);
2714 					return;
2715 				}
2716 			}
2717 		}
2718 
2719 		/* No errors; receive the packet. */
2720 		total_len -= ETHER_CRC_LEN;
2721 #ifdef __i386__
2722 		/*
2723 		 * On the x86 we do not have alignment problems, so try to
2724 		 * allocate a new buffer for the receive ring, and pass up
2725 		 * the one where the packet is already, saving the expensive
2726 		 * copy done in m_devget().
2727 		 * If we are on an architecture with alignment problems, or
2728 		 * if the allocation fails, then use m_devget and leave the
2729 		 * existing buffer in the receive ring.
2730 		 */
2731 		if (dc_quick && dc_newbuf(sc, i, 1) == 0) {
2732 			m->m_pkthdr.rcvif = ifp;
2733 			m->m_pkthdr.len = m->m_len = total_len;
2734 			DC_INC(i, DC_RX_LIST_CNT);
2735 		} else
2736 #endif
2737 		{
2738 			struct mbuf *m0;
2739 
2740 			m0 = m_devget(mtod(m, char *), total_len,
2741 				ETHER_ALIGN, ifp, NULL);
2742 			dc_newbuf(sc, i, 0);
2743 			DC_INC(i, DC_RX_LIST_CNT);
2744 			if (m0 == NULL) {
2745 				ifp->if_ierrors++;
2746 				continue;
2747 			}
2748 			m = m0;
2749 		}
2750 
2751 		ifp->if_ipackets++;
2752 		DC_UNLOCK(sc);
2753 		(*ifp->if_input)(ifp, m);
2754 		DC_LOCK(sc);
2755 	}
2756 
2757 	sc->dc_cdata.dc_rx_prod = i;
2758 }
2759 
2760 /*
2761  * A frame was downloaded to the chip. It's safe for us to clean up
2762  * the list buffers.
2763  */
2764 
2765 static void
2766 dc_txeof(struct dc_softc *sc)
2767 {
2768 	struct dc_desc *cur_tx = NULL;
2769 	struct ifnet *ifp;
2770 	int idx;
2771 	u_int32_t ctl, txstat;
2772 
2773 	ifp = sc->dc_ifp;
2774 
2775 	/*
2776 	 * Go through our tx list and free mbufs for those
2777 	 * frames that have been transmitted.
2778 	 */
2779 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
2780 	idx = sc->dc_cdata.dc_tx_cons;
2781 	while (idx != sc->dc_cdata.dc_tx_prod) {
2782 
2783 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
2784 		txstat = le32toh(cur_tx->dc_status);
2785 		ctl = le32toh(cur_tx->dc_ctl);
2786 
2787 		if (txstat & DC_TXSTAT_OWN)
2788 			break;
2789 
2790 		if (!(ctl & DC_TXCTL_LASTFRAG) || ctl & DC_TXCTL_SETUP) {
2791 			if (ctl & DC_TXCTL_SETUP) {
2792 				/*
2793 				 * Yes, the PNIC is so brain damaged
2794 				 * that it will sometimes generate a TX
2795 				 * underrun error while DMAing the RX
2796 				 * filter setup frame. If we detect this,
2797 				 * we have to send the setup frame again,
2798 				 * or else the filter won't be programmed
2799 				 * correctly.
2800 				 */
2801 				if (DC_IS_PNIC(sc)) {
2802 					if (txstat & DC_TXSTAT_ERRSUM)
2803 						dc_setfilt(sc);
2804 				}
2805 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
2806 			}
2807 			sc->dc_cdata.dc_tx_cnt--;
2808 			DC_INC(idx, DC_TX_LIST_CNT);
2809 			continue;
2810 		}
2811 
2812 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2813 			/*
2814 			 * XXX: Why does my Xircom taunt me so?
2815 			 * For some reason it likes setting the CARRLOST flag
2816 			 * even when the carrier is there. wtf?!?
2817 			 * Who knows, but Conexant chips have the
2818 			 * same problem. Maybe they took lessons
2819 			 * from Xircom.
2820 			 */
2821 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2822 			    sc->dc_pmode == DC_PMODE_MII &&
2823 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2824 			    DC_TXSTAT_NOCARRIER)))
2825 				txstat &= ~DC_TXSTAT_ERRSUM;
2826 		} else {
2827 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2828 			    sc->dc_pmode == DC_PMODE_MII &&
2829 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2830 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
2831 				txstat &= ~DC_TXSTAT_ERRSUM;
2832 		}
2833 
2834 		if (txstat & DC_TXSTAT_ERRSUM) {
2835 			ifp->if_oerrors++;
2836 			if (txstat & DC_TXSTAT_EXCESSCOLL)
2837 				ifp->if_collisions++;
2838 			if (txstat & DC_TXSTAT_LATECOLL)
2839 				ifp->if_collisions++;
2840 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
2841 				dc_init_locked(sc);
2842 				return;
2843 			}
2844 		}
2845 
2846 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
2847 
2848 		ifp->if_opackets++;
2849 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
2850 			bus_dmamap_sync(sc->dc_mtag,
2851 			    sc->dc_cdata.dc_tx_map[idx],
2852 			    BUS_DMASYNC_POSTWRITE);
2853 			bus_dmamap_unload(sc->dc_mtag,
2854 			    sc->dc_cdata.dc_tx_map[idx]);
2855 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
2856 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
2857 		}
2858 
2859 		sc->dc_cdata.dc_tx_cnt--;
2860 		DC_INC(idx, DC_TX_LIST_CNT);
2861 	}
2862 
2863 	if (idx != sc->dc_cdata.dc_tx_cons) {
2864 	    	/* Some buffers have been freed. */
2865 		sc->dc_cdata.dc_tx_cons = idx;
2866 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2867 	}
2868 	ifp->if_timer = (sc->dc_cdata.dc_tx_cnt == 0) ? 0 : 5;
2869 }
2870 
2871 static void
2872 dc_tick(void *xsc)
2873 {
2874 	struct dc_softc *sc;
2875 	struct mii_data *mii;
2876 	struct ifnet *ifp;
2877 	u_int32_t r;
2878 
2879 	sc = xsc;
2880 	DC_LOCK_ASSERT(sc);
2881 	ifp = sc->dc_ifp;
2882 	mii = device_get_softc(sc->dc_miibus);
2883 
2884 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2885 		if (sc->dc_flags & DC_21143_NWAY) {
2886 			r = CSR_READ_4(sc, DC_10BTSTAT);
2887 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2888 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
2889 				sc->dc_link = 0;
2890 				mii_mediachg(mii);
2891 			}
2892 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2893 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2894 				sc->dc_link = 0;
2895 				mii_mediachg(mii);
2896 			}
2897 			if (sc->dc_link == 0)
2898 				mii_tick(mii);
2899 		} else {
2900 			r = CSR_READ_4(sc, DC_ISR);
2901 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2902 			    sc->dc_cdata.dc_tx_cnt == 0) {
2903 				mii_tick(mii);
2904 				if (!(mii->mii_media_status & IFM_ACTIVE))
2905 					sc->dc_link = 0;
2906 			}
2907 		}
2908 	} else
2909 		mii_tick(mii);
2910 
2911 	/*
2912 	 * When the init routine completes, we expect to be able to send
2913 	 * packets right away, and in fact the network code will send a
2914 	 * gratuitous ARP the moment the init routine marks the interface
2915 	 * as running. However, even though the MAC may have been initialized,
2916 	 * there may be a delay of a few seconds before the PHY completes
2917 	 * autonegotiation and the link is brought up. Any transmissions
2918 	 * made during that delay will be lost. Dealing with this is tricky:
2919 	 * we can't just pause in the init routine while waiting for the
2920 	 * PHY to come ready since that would bring the whole system to
2921 	 * a screeching halt for several seconds.
2922 	 *
2923 	 * What we do here is prevent the TX start routine from sending
2924 	 * any packets until a link has been established. After the
2925 	 * interface has been initialized, the tick routine will poll
2926 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
2927 	 * that time, packets will stay in the send queue, and once the
2928 	 * link comes up, they will be flushed out to the wire.
2929 	 */
2930 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
2931 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
2932 		sc->dc_link++;
2933 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2934 			dc_start_locked(ifp);
2935 	}
2936 
2937 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2938 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2939 	else
2940 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
2941 }
2942 
2943 /*
2944  * A transmit underrun has occurred.  Back off the transmit threshold,
2945  * or switch to store and forward mode if we have to.
2946  */
2947 static void
2948 dc_tx_underrun(struct dc_softc *sc)
2949 {
2950 	u_int32_t isr;
2951 	int i;
2952 
2953 	if (DC_IS_DAVICOM(sc))
2954 		dc_init_locked(sc);
2955 
2956 	if (DC_IS_INTEL(sc)) {
2957 		/*
2958 		 * The real 21143 requires that the transmitter be idle
2959 		 * in order to change the transmit threshold or store
2960 		 * and forward state.
2961 		 */
2962 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2963 
2964 		for (i = 0; i < DC_TIMEOUT; i++) {
2965 			isr = CSR_READ_4(sc, DC_ISR);
2966 			if (isr & DC_ISR_TX_IDLE)
2967 				break;
2968 			DELAY(10);
2969 		}
2970 		if (i == DC_TIMEOUT) {
2971 			if_printf(sc->dc_ifp,
2972 			    "failed to force tx to idle state\n");
2973 			dc_init_locked(sc);
2974 		}
2975 	}
2976 
2977 	if_printf(sc->dc_ifp, "TX underrun -- ");
2978 	sc->dc_txthresh += DC_TXTHRESH_INC;
2979 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
2980 		printf("using store and forward mode\n");
2981 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
2982 	} else {
2983 		printf("increasing TX threshold\n");
2984 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
2985 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
2986 	}
2987 
2988 	if (DC_IS_INTEL(sc))
2989 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2990 }
2991 
2992 #ifdef DEVICE_POLLING
2993 static poll_handler_t dc_poll;
2994 
2995 static void
2996 dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2997 {
2998 	struct dc_softc *sc = ifp->if_softc;
2999 
3000 	DC_LOCK(sc);
3001 
3002 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3003 		DC_UNLOCK(sc);
3004 		return;
3005 	}
3006 
3007 	sc->rxcycles = count;
3008 	dc_rxeof(sc);
3009 	dc_txeof(sc);
3010 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
3011 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3012 		dc_start_locked(ifp);
3013 
3014 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3015 		u_int32_t	status;
3016 
3017 		status = CSR_READ_4(sc, DC_ISR);
3018 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3019 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3020 			DC_ISR_BUS_ERR);
3021 		if (!status) {
3022 			DC_UNLOCK(sc);
3023 			return;
3024 		}
3025 		/* ack what we have */
3026 		CSR_WRITE_4(sc, DC_ISR, status);
3027 
3028 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3029 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3030 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3031 
3032 			if (dc_rx_resync(sc))
3033 				dc_rxeof(sc);
3034 		}
3035 		/* restart transmit unit if necessary */
3036 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3037 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3038 
3039 		if (status & DC_ISR_TX_UNDERRUN)
3040 			dc_tx_underrun(sc);
3041 
3042 		if (status & DC_ISR_BUS_ERR) {
3043 			if_printf(ifp, "dc_poll: bus error\n");
3044 			dc_reset(sc);
3045 			dc_init_locked(sc);
3046 		}
3047 	}
3048 	DC_UNLOCK(sc);
3049 }
3050 #endif /* DEVICE_POLLING */
3051 
3052 static void
3053 dc_intr(void *arg)
3054 {
3055 	struct dc_softc *sc;
3056 	struct ifnet *ifp;
3057 	u_int32_t status;
3058 
3059 	sc = arg;
3060 
3061 	if (sc->suspended)
3062 		return;
3063 
3064 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
3065 		return;
3066 
3067 	DC_LOCK(sc);
3068 	ifp = sc->dc_ifp;
3069 #ifdef DEVICE_POLLING
3070 	if (ifp->if_capenable & IFCAP_POLLING) {
3071 		DC_UNLOCK(sc);
3072 		return;
3073 	}
3074 #endif
3075 
3076 	/* Suppress unwanted interrupts */
3077 	if (!(ifp->if_flags & IFF_UP)) {
3078 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
3079 			dc_stop(sc);
3080 		DC_UNLOCK(sc);
3081 		return;
3082 	}
3083 
3084 	/* Disable interrupts. */
3085 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3086 
3087 	while (((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS) &&
3088 	    status != 0xFFFFFFFF &&
3089 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
3090 
3091 		CSR_WRITE_4(sc, DC_ISR, status);
3092 
3093 		if (status & DC_ISR_RX_OK) {
3094 			int		curpkts;
3095 			curpkts = ifp->if_ipackets;
3096 			dc_rxeof(sc);
3097 			if (curpkts == ifp->if_ipackets) {
3098 				while (dc_rx_resync(sc))
3099 					dc_rxeof(sc);
3100 			}
3101 		}
3102 
3103 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
3104 			dc_txeof(sc);
3105 
3106 		if (status & DC_ISR_TX_IDLE) {
3107 			dc_txeof(sc);
3108 			if (sc->dc_cdata.dc_tx_cnt) {
3109 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
3110 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3111 			}
3112 		}
3113 
3114 		if (status & DC_ISR_TX_UNDERRUN)
3115 			dc_tx_underrun(sc);
3116 
3117 		if ((status & DC_ISR_RX_WATDOGTIMEO)
3118 		    || (status & DC_ISR_RX_NOBUF)) {
3119 			int		curpkts;
3120 			curpkts = ifp->if_ipackets;
3121 			dc_rxeof(sc);
3122 			if (curpkts == ifp->if_ipackets) {
3123 				while (dc_rx_resync(sc))
3124 					dc_rxeof(sc);
3125 			}
3126 		}
3127 
3128 		if (status & DC_ISR_BUS_ERR) {
3129 			dc_reset(sc);
3130 			dc_init_locked(sc);
3131 		}
3132 	}
3133 
3134 	/* Re-enable interrupts. */
3135 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
3136 
3137 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3138 		dc_start_locked(ifp);
3139 
3140 	DC_UNLOCK(sc);
3141 }
3142 
3143 static void
3144 dc_dma_map_txbuf(arg, segs, nseg, mapsize, error)
3145 	void *arg;
3146 	bus_dma_segment_t *segs;
3147 	int nseg;
3148 	bus_size_t mapsize;
3149 	int error;
3150 {
3151 	struct dc_softc *sc;
3152 	struct dc_desc *f;
3153 	int cur, first, frag, i;
3154 
3155 	sc = arg;
3156 	if (error) {
3157 		sc->dc_cdata.dc_tx_err = error;
3158 		return;
3159 	}
3160 
3161 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3162 	for (i = 0; i < nseg; i++) {
3163 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3164 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3165 		    (first != sc->dc_cdata.dc_tx_first)) {
3166 			bus_dmamap_unload(sc->dc_mtag,
3167 			    sc->dc_cdata.dc_tx_map[first]);
3168 			sc->dc_cdata.dc_tx_err = ENOBUFS;
3169 			return;
3170 		}
3171 
3172 		f = &sc->dc_ldata->dc_tx_list[frag];
3173 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3174 		if (i == 0) {
3175 			f->dc_status = 0;
3176 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3177 		} else
3178 			f->dc_status = htole32(DC_TXSTAT_OWN);
3179 		f->dc_data = htole32(segs[i].ds_addr);
3180 		cur = frag;
3181 		DC_INC(frag, DC_TX_LIST_CNT);
3182 	}
3183 
3184 	sc->dc_cdata.dc_tx_err = 0;
3185 	sc->dc_cdata.dc_tx_prod = frag;
3186 	sc->dc_cdata.dc_tx_cnt += nseg;
3187 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3188 	sc->dc_cdata.dc_tx_chain[cur] = sc->dc_cdata.dc_tx_mapping;
3189 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
3190 		sc->dc_ldata->dc_tx_list[first].dc_ctl |=
3191 		    htole32(DC_TXCTL_FINT);
3192 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
3193 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3194 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
3195 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3196 	sc->dc_ldata->dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3197 }
3198 
3199 /*
3200  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
3201  * pointers to the fragment pointers.
3202  */
3203 static int
3204 dc_encap(struct dc_softc *sc, struct mbuf **m_head)
3205 {
3206 	struct mbuf *m;
3207 	int error, idx, chainlen = 0;
3208 
3209 	/*
3210 	 * If there's no way we can send any packets, return now.
3211 	 */
3212 	if (DC_TX_LIST_CNT - sc->dc_cdata.dc_tx_cnt < 6)
3213 		return (ENOBUFS);
3214 
3215 	/*
3216 	 * Count the number of frags in this chain to see if
3217 	 * we need to m_defrag.  Since the descriptor list is shared
3218 	 * by all packets, we'll m_defrag long chains so that they
3219 	 * do not use up the entire list, even if they would fit.
3220 	 */
3221 	for (m = *m_head; m != NULL; m = m->m_next)
3222 		chainlen++;
3223 
3224 	if ((chainlen > DC_TX_LIST_CNT / 4) ||
3225 	    ((DC_TX_LIST_CNT - (chainlen + sc->dc_cdata.dc_tx_cnt)) < 6)) {
3226 		m = m_defrag(*m_head, M_DONTWAIT);
3227 		if (m == NULL)
3228 			return (ENOBUFS);
3229 		*m_head = m;
3230 	}
3231 
3232 	/*
3233 	 * Start packing the mbufs in this chain into
3234 	 * the fragment pointers. Stop when we run out
3235 	 * of fragments or hit the end of the mbuf chain.
3236 	 */
3237 	idx = sc->dc_cdata.dc_tx_prod;
3238 	sc->dc_cdata.dc_tx_mapping = *m_head;
3239 	error = bus_dmamap_load_mbuf(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
3240 	    *m_head, dc_dma_map_txbuf, sc, 0);
3241 	if (error)
3242 		return (error);
3243 	if (sc->dc_cdata.dc_tx_err != 0)
3244 		return (sc->dc_cdata.dc_tx_err);
3245 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
3246 	    BUS_DMASYNC_PREWRITE);
3247 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
3248 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3249 	return (0);
3250 }
3251 
3252 /*
3253  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
3254  * to the mbuf data regions directly in the transmit lists. We also save a
3255  * copy of the pointers since the transmit list fragment pointers are
3256  * physical addresses.
3257  */
3258 
3259 static void
3260 dc_start(struct ifnet *ifp)
3261 {
3262 	struct dc_softc *sc;
3263 
3264 	sc = ifp->if_softc;
3265 	DC_LOCK(sc);
3266 	dc_start_locked(ifp);
3267 	DC_UNLOCK(sc);
3268 }
3269 
3270 static void
3271 dc_start_locked(struct ifnet *ifp)
3272 {
3273 	struct dc_softc *sc;
3274 	struct mbuf *m_head = NULL, *m;
3275 	unsigned int queued = 0;
3276 	int idx;
3277 
3278 	sc = ifp->if_softc;
3279 
3280 	DC_LOCK_ASSERT(sc);
3281 
3282 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10)
3283 		return;
3284 
3285 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
3286 		return;
3287 
3288 	idx = sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
3289 
3290 	while (sc->dc_cdata.dc_tx_chain[idx] == NULL) {
3291 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
3292 		if (m_head == NULL)
3293 			break;
3294 
3295 		if (sc->dc_flags & DC_TX_COALESCE &&
3296 		    (m_head->m_next != NULL ||
3297 		     sc->dc_flags & DC_TX_ALIGN)) {
3298 			m = m_defrag(m_head, M_DONTWAIT);
3299 			if (m == NULL) {
3300 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
3301 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3302 				break;
3303 			} else {
3304 				m_head = m;
3305 			}
3306 		}
3307 
3308 		if (dc_encap(sc, &m_head)) {
3309 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
3310 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3311 			break;
3312 		}
3313 		idx = sc->dc_cdata.dc_tx_prod;
3314 
3315 		queued++;
3316 		/*
3317 		 * If there's a BPF listener, bounce a copy of this frame
3318 		 * to him.
3319 		 */
3320 		BPF_MTAP(ifp, m_head);
3321 
3322 		if (sc->dc_flags & DC_TX_ONE) {
3323 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3324 			break;
3325 		}
3326 	}
3327 
3328 	if (queued > 0) {
3329 		/* Transmit */
3330 		if (!(sc->dc_flags & DC_TX_POLL))
3331 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3332 
3333 		/*
3334 		 * Set a timeout in case the chip goes out to lunch.
3335 		 */
3336 		ifp->if_timer = 5;
3337 	}
3338 }
3339 
3340 static void
3341 dc_init(void *xsc)
3342 {
3343 	struct dc_softc *sc = xsc;
3344 
3345 	DC_LOCK(sc);
3346 	dc_init_locked(sc);
3347 	DC_UNLOCK(sc);
3348 }
3349 
3350 static void
3351 dc_init_locked(struct dc_softc *sc)
3352 {
3353 	struct ifnet *ifp = sc->dc_ifp;
3354 	struct mii_data *mii;
3355 
3356 	DC_LOCK_ASSERT(sc);
3357 
3358 	mii = device_get_softc(sc->dc_miibus);
3359 
3360 	/*
3361 	 * Cancel pending I/O and free all RX/TX buffers.
3362 	 */
3363 	dc_stop(sc);
3364 	dc_reset(sc);
3365 
3366 	/*
3367 	 * Set cache alignment and burst length.
3368 	 */
3369 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
3370 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
3371 	else
3372 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3373 	/*
3374 	 * Evenly share the bus between receive and transmit process.
3375 	 */
3376 	if (DC_IS_INTEL(sc))
3377 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
3378 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
3379 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
3380 	} else {
3381 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
3382 	}
3383 	if (sc->dc_flags & DC_TX_POLL)
3384 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
3385 	switch(sc->dc_cachesize) {
3386 	case 32:
3387 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
3388 		break;
3389 	case 16:
3390 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
3391 		break;
3392 	case 8:
3393 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
3394 		break;
3395 	case 0:
3396 	default:
3397 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
3398 		break;
3399 	}
3400 
3401 	if (sc->dc_flags & DC_TX_STORENFWD)
3402 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
3403 	else {
3404 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
3405 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
3406 		} else {
3407 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
3408 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
3409 		}
3410 	}
3411 
3412 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
3413 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
3414 
3415 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
3416 		/*
3417 		 * The app notes for the 98713 and 98715A say that
3418 		 * in order to have the chips operate properly, a magic
3419 		 * number must be written to CSR16. Macronix does not
3420 		 * document the meaning of these bits so there's no way
3421 		 * to know exactly what they do. The 98713 has a magic
3422 		 * number all its own; the rest all use a different one.
3423 		 */
3424 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
3425 		if (sc->dc_type == DC_TYPE_98713)
3426 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
3427 		else
3428 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
3429 	}
3430 
3431 	if (DC_IS_XIRCOM(sc)) {
3432 		/*
3433 		 * setup General Purpose Port mode and data so the tulip
3434 		 * can talk to the MII.
3435 		 */
3436 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3437 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3438 		DELAY(10);
3439 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3440 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3441 		DELAY(10);
3442 	}
3443 
3444 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3445 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
3446 
3447 	/* Init circular RX list. */
3448 	if (dc_list_rx_init(sc) == ENOBUFS) {
3449 		if_printf(ifp,
3450 		    "initialization failed: no memory for rx buffers\n");
3451 		dc_stop(sc);
3452 		return;
3453 	}
3454 
3455 	/*
3456 	 * Init TX descriptors.
3457 	 */
3458 	dc_list_tx_init(sc);
3459 
3460 	/*
3461 	 * Load the address of the RX list.
3462 	 */
3463 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
3464 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
3465 
3466 	/*
3467 	 * Enable interrupts.
3468 	 */
3469 #ifdef DEVICE_POLLING
3470 	/*
3471 	 * ... but only if we are not polling, and make sure they are off in
3472 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3473 	 * after a reset.
3474 	 */
3475 	if (ifp->if_capenable & IFCAP_POLLING)
3476 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3477 	else
3478 #endif
3479 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
3480 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
3481 
3482 	/* Enable transmitter. */
3483 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
3484 
3485 	/*
3486 	 * If this is an Intel 21143 and we're not using the
3487 	 * MII port, program the LED control pins so we get
3488 	 * link and activity indications.
3489 	 */
3490 	if (sc->dc_flags & DC_TULIP_LEDS) {
3491 		CSR_WRITE_4(sc, DC_WATCHDOG,
3492 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
3493 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3494 	}
3495 
3496 	/*
3497 	 * Load the RX/multicast filter. We do this sort of late
3498 	 * because the filter programming scheme on the 21143 and
3499 	 * some clones requires DMAing a setup frame via the TX
3500 	 * engine, and we need the transmitter enabled for that.
3501 	 */
3502 	dc_setfilt(sc);
3503 
3504 	/* Enable receiver. */
3505 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
3506 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
3507 
3508 	mii_mediachg(mii);
3509 	dc_setcfg(sc, sc->dc_if_media);
3510 
3511 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
3512 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3513 
3514 	/* Don't start the ticker if this is a homePNA link. */
3515 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3516 		sc->dc_link = 1;
3517 	else {
3518 		if (sc->dc_flags & DC_21143_NWAY)
3519 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3520 		else
3521 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3522 	}
3523 }
3524 
3525 /*
3526  * Set media options.
3527  */
3528 static int
3529 dc_ifmedia_upd(struct ifnet *ifp)
3530 {
3531 	struct dc_softc *sc;
3532 	struct mii_data *mii;
3533 	struct ifmedia *ifm;
3534 
3535 	sc = ifp->if_softc;
3536 	mii = device_get_softc(sc->dc_miibus);
3537 	DC_LOCK(sc);
3538 	mii_mediachg(mii);
3539 	ifm = &mii->mii_media;
3540 
3541 	if (DC_IS_DAVICOM(sc) &&
3542 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3543 		dc_setcfg(sc, ifm->ifm_media);
3544 	else
3545 		sc->dc_link = 0;
3546 	DC_UNLOCK(sc);
3547 
3548 	return (0);
3549 }
3550 
3551 /*
3552  * Report current media status.
3553  */
3554 static void
3555 dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3556 {
3557 	struct dc_softc *sc;
3558 	struct mii_data *mii;
3559 	struct ifmedia *ifm;
3560 
3561 	sc = ifp->if_softc;
3562 	mii = device_get_softc(sc->dc_miibus);
3563 	DC_LOCK(sc);
3564 	mii_pollstat(mii);
3565 	ifm = &mii->mii_media;
3566 	if (DC_IS_DAVICOM(sc)) {
3567 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3568 			ifmr->ifm_active = ifm->ifm_media;
3569 			ifmr->ifm_status = 0;
3570 			return;
3571 		}
3572 	}
3573 	ifmr->ifm_active = mii->mii_media_active;
3574 	ifmr->ifm_status = mii->mii_media_status;
3575 	DC_UNLOCK(sc);
3576 }
3577 
3578 static int
3579 dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3580 {
3581 	struct dc_softc *sc = ifp->if_softc;
3582 	struct ifreq *ifr = (struct ifreq *)data;
3583 	struct mii_data *mii;
3584 	int error = 0;
3585 
3586 	switch (command) {
3587 	case SIOCSIFFLAGS:
3588 		DC_LOCK(sc);
3589 		if (ifp->if_flags & IFF_UP) {
3590 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
3591 				(IFF_PROMISC | IFF_ALLMULTI);
3592 
3593 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
3594 				if (need_setfilt)
3595 					dc_setfilt(sc);
3596 			} else {
3597 				sc->dc_txthresh = 0;
3598 				dc_init_locked(sc);
3599 			}
3600 		} else {
3601 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
3602 				dc_stop(sc);
3603 		}
3604 		sc->dc_if_flags = ifp->if_flags;
3605 		DC_UNLOCK(sc);
3606 		error = 0;
3607 		break;
3608 	case SIOCADDMULTI:
3609 	case SIOCDELMULTI:
3610 		DC_LOCK(sc);
3611 		dc_setfilt(sc);
3612 		DC_UNLOCK(sc);
3613 		error = 0;
3614 		break;
3615 	case SIOCGIFMEDIA:
3616 	case SIOCSIFMEDIA:
3617 		mii = device_get_softc(sc->dc_miibus);
3618 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3619 		break;
3620 	case SIOCSIFCAP:
3621 #ifdef DEVICE_POLLING
3622 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
3623 		    !(ifp->if_capenable & IFCAP_POLLING)) {
3624 			error = ether_poll_register(dc_poll, ifp);
3625 			if (error)
3626 				return(error);
3627 			DC_LOCK(sc);
3628 			/* Disable interrupts */
3629 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3630 			ifp->if_capenable |= IFCAP_POLLING;
3631 			DC_UNLOCK(sc);
3632 			return (error);
3633 
3634 		}
3635 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
3636 		    ifp->if_capenable & IFCAP_POLLING) {
3637 			error = ether_poll_deregister(ifp);
3638 			/* Enable interrupts. */
3639 			DC_LOCK(sc);
3640 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
3641 			ifp->if_capenable &= ~IFCAP_POLLING;
3642 			DC_UNLOCK(sc);
3643 			return (error);
3644 		}
3645 #endif /* DEVICE_POLLING */
3646 		break;
3647 	default:
3648 		error = ether_ioctl(ifp, command, data);
3649 		break;
3650 	}
3651 
3652 	return (error);
3653 }
3654 
3655 static void
3656 dc_watchdog(struct ifnet *ifp)
3657 {
3658 	struct dc_softc *sc;
3659 
3660 	sc = ifp->if_softc;
3661 
3662 	DC_LOCK(sc);
3663 
3664 	ifp->if_oerrors++;
3665 	if_printf(ifp, "watchdog timeout\n");
3666 
3667 	dc_stop(sc);
3668 	dc_reset(sc);
3669 	dc_init_locked(sc);
3670 
3671 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3672 		dc_start_locked(ifp);
3673 
3674 	DC_UNLOCK(sc);
3675 }
3676 
3677 /*
3678  * Stop the adapter and free any mbufs allocated to the
3679  * RX and TX lists.
3680  */
3681 static void
3682 dc_stop(struct dc_softc *sc)
3683 {
3684 	struct ifnet *ifp;
3685 	struct dc_list_data *ld;
3686 	struct dc_chain_data *cd;
3687 	int i;
3688 	u_int32_t ctl;
3689 
3690 	DC_LOCK_ASSERT(sc);
3691 
3692 	ifp = sc->dc_ifp;
3693 	ifp->if_timer = 0;
3694 	ld = sc->dc_ldata;
3695 	cd = &sc->dc_cdata;
3696 
3697 	callout_stop(&sc->dc_stat_ch);
3698 
3699 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3700 
3701 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
3702 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3703 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
3704 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
3705 	sc->dc_link = 0;
3706 
3707 	/*
3708 	 * Free data in the RX lists.
3709 	 */
3710 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3711 		if (cd->dc_rx_chain[i] != NULL) {
3712 			m_freem(cd->dc_rx_chain[i]);
3713 			cd->dc_rx_chain[i] = NULL;
3714 		}
3715 	}
3716 	bzero(&ld->dc_rx_list, sizeof(ld->dc_rx_list));
3717 
3718 	/*
3719 	 * Free the TX list buffers.
3720 	 */
3721 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
3722 		if (cd->dc_tx_chain[i] != NULL) {
3723 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
3724 			if ((ctl & DC_TXCTL_SETUP) ||
3725 			    !(ctl & DC_TXCTL_LASTFRAG)) {
3726 				cd->dc_tx_chain[i] = NULL;
3727 				continue;
3728 			}
3729 			bus_dmamap_unload(sc->dc_mtag, cd->dc_tx_map[i]);
3730 			m_freem(cd->dc_tx_chain[i]);
3731 			cd->dc_tx_chain[i] = NULL;
3732 		}
3733 	}
3734 	bzero(&ld->dc_tx_list, sizeof(ld->dc_tx_list));
3735 }
3736 
3737 /*
3738  * Device suspend routine.  Stop the interface and save some PCI
3739  * settings in case the BIOS doesn't restore them properly on
3740  * resume.
3741  */
3742 static int
3743 dc_suspend(device_t dev)
3744 {
3745 	struct dc_softc *sc;
3746 
3747 	sc = device_get_softc(dev);
3748 	DC_LOCK(sc);
3749 	dc_stop(sc);
3750 	sc->suspended = 1;
3751 	DC_UNLOCK(sc);
3752 
3753 	return (0);
3754 }
3755 
3756 /*
3757  * Device resume routine.  Restore some PCI settings in case the BIOS
3758  * doesn't, re-enable busmastering, and restart the interface if
3759  * appropriate.
3760  */
3761 static int
3762 dc_resume(device_t dev)
3763 {
3764 	struct dc_softc *sc;
3765 	struct ifnet *ifp;
3766 
3767 	sc = device_get_softc(dev);
3768 	ifp = sc->dc_ifp;
3769 
3770 	/* reinitialize interface if necessary */
3771 	DC_LOCK(sc);
3772 	if (ifp->if_flags & IFF_UP)
3773 		dc_init_locked(sc);
3774 
3775 	sc->suspended = 0;
3776 	DC_UNLOCK(sc);
3777 
3778 	return (0);
3779 }
3780 
3781 /*
3782  * Stop all chip I/O so that the kernel's probe routines don't
3783  * get confused by errant DMAs when rebooting.
3784  */
3785 static void
3786 dc_shutdown(device_t dev)
3787 {
3788 	struct dc_softc *sc;
3789 
3790 	sc = device_get_softc(dev);
3791 
3792 	DC_LOCK(sc);
3793 	dc_stop(sc);
3794 	DC_UNLOCK(sc);
3795 }
3796