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