xref: /dragonfly/sys/dev/netif/xl/if_xl.c (revision 38b720cd)
1 /*
2  * Copyright (c) 1997, 1998, 1999
3  *	Bill Paul <wpaul@ctr.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  * $FreeBSD: src/sys/pci/if_xl.c,v 1.72.2.28 2003/10/08 06:01:57 murray Exp $
33  */
34 
35 /*
36  * 3Com 3c90x Etherlink XL PCI NIC driver
37  *
38  * Supports the 3Com "boomerang", "cyclone" and "hurricane" PCI
39  * bus-master chips (3c90x cards and embedded controllers) including
40  * the following:
41  *
42  * 3Com 3c900-TPO	10Mbps/RJ-45
43  * 3Com 3c900-COMBO	10Mbps/RJ-45,AUI,BNC
44  * 3Com 3c905-TX	10/100Mbps/RJ-45
45  * 3Com 3c905-T4	10/100Mbps/RJ-45
46  * 3Com 3c900B-TPO	10Mbps/RJ-45
47  * 3Com 3c900B-COMBO	10Mbps/RJ-45,AUI,BNC
48  * 3Com 3c900B-TPC	10Mbps/RJ-45,BNC
49  * 3Com 3c900B-FL	10Mbps/Fiber-optic
50  * 3Com 3c905B-COMBO	10/100Mbps/RJ-45,AUI,BNC
51  * 3Com 3c905B-TX	10/100Mbps/RJ-45
52  * 3Com 3c905B-FL/FX	10/100Mbps/Fiber-optic
53  * 3Com 3c905C-TX	10/100Mbps/RJ-45 (Tornado ASIC)
54  * 3Com 3c980-TX	10/100Mbps server adapter (Hurricane ASIC)
55  * 3Com 3c980C-TX	10/100Mbps server adapter (Tornado ASIC)
56  * 3Com 3cSOHO100-TX	10/100Mbps/RJ-45 (Hurricane ASIC)
57  * 3Com 3c450-TX	10/100Mbps/RJ-45 (Tornado ASIC)
58  * 3Com 3c555		10/100Mbps/RJ-45 (MiniPCI, Laptop Hurricane)
59  * 3Com 3c556		10/100Mbps/RJ-45 (MiniPCI, Hurricane ASIC)
60  * 3Com 3c556B		10/100Mbps/RJ-45 (MiniPCI, Hurricane ASIC)
61  * 3Com 3c575TX		10/100Mbps/RJ-45 (Cardbus, Hurricane ASIC)
62  * 3Com 3c575B		10/100Mbps/RJ-45 (Cardbus, Hurricane ASIC)
63  * 3Com 3c575C		10/100Mbps/RJ-45 (Cardbus, Hurricane ASIC)
64  * 3Com 3cxfem656	10/100Mbps/RJ-45 (Cardbus, Hurricane ASIC)
65  * 3Com 3cxfem656b	10/100Mbps/RJ-45 (Cardbus, Hurricane ASIC)
66  * 3Com 3cxfem656c	10/100Mbps/RJ-45 (Cardbus, Tornado ASIC)
67  * Dell Optiplex GX1 on-board 3c918 10/100Mbps/RJ-45
68  * Dell on-board 3c920 10/100Mbps/RJ-45
69  * Dell Precision on-board 3c905B 10/100Mbps/RJ-45
70  * Dell Latitude laptop docking station embedded 3c905-TX
71  *
72  * Written by Bill Paul <wpaul@ctr.columbia.edu>
73  * Electrical Engineering Department
74  * Columbia University, New York City
75  */
76 
77 /*
78  * The 3c90x series chips use a bus-master DMA interface for transfering
79  * packets to and from the controller chip. Some of the "vortex" cards
80  * (3c59x) also supported a bus master mode, however for those chips
81  * you could only DMA packets to/from a contiguous memory buffer. For
82  * transmission this would mean copying the contents of the queued mbuf
83  * chain into an mbuf cluster and then DMAing the cluster. This extra
84  * copy would sort of defeat the purpose of the bus master support for
85  * any packet that doesn't fit into a single mbuf.
86  *
87  * By contrast, the 3c90x cards support a fragment-based bus master
88  * mode where mbuf chains can be encapsulated using TX descriptors.
89  * This is similar to other PCI chips such as the Texas Instruments
90  * ThunderLAN and the Intel 82557/82558.
91  *
92  * The "vortex" driver (if_vx.c) happens to work for the "boomerang"
93  * bus master chips because they maintain the old PIO interface for
94  * backwards compatibility, but starting with the 3c905B and the
95  * "cyclone" chips, the compatibility interface has been dropped.
96  * Since using bus master DMA is a big win, we use this driver to
97  * support the PCI "boomerang" chips even though they work with the
98  * "vortex" driver in order to obtain better performance.
99  */
100 
101 #include "opt_ifpoll.h"
102 
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/sockio.h>
106 #include <sys/endian.h>
107 #include <sys/mbuf.h>
108 #include <sys/kernel.h>
109 #include <sys/socket.h>
110 #include <sys/serialize.h>
111 #include <sys/bus.h>
112 #include <sys/rman.h>
113 #include <sys/thread2.h>
114 #include <sys/interrupt.h>
115 
116 #include <net/if.h>
117 #include <net/ifq_var.h>
118 #include <net/if_arp.h>
119 #include <net/ethernet.h>
120 #include <net/if_dl.h>
121 #include <net/if_media.h>
122 #include <net/if_poll.h>
123 #include <net/vlan/if_vlan_var.h>
124 
125 #include <net/bpf.h>
126 
127 #include "../mii_layer/mii.h"
128 #include "../mii_layer/miivar.h"
129 
130 #include <bus/pci/pcireg.h>
131 #include <bus/pci/pcivar.h>
132 
133 /* "controller miibus0" required.  See GENERIC if you get errors here. */
134 #include "miibus_if.h"
135 
136 #include "if_xlreg.h"
137 
138 #define XL905B_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
139 
140 /*
141  * Various supported device vendors/types and their names.
142  */
143 static struct xl_type xl_devs[] = {
144 	{ TC_VENDORID, TC_DEVICEID_BOOMERANG_10BT,
145 		"3Com 3c900-TPO Etherlink XL" },
146 	{ TC_VENDORID, TC_DEVICEID_BOOMERANG_10BT_COMBO,
147 		"3Com 3c900-COMBO Etherlink XL" },
148 	{ TC_VENDORID, TC_DEVICEID_BOOMERANG_10_100BT,
149 		"3Com 3c905-TX Fast Etherlink XL" },
150 	{ TC_VENDORID, TC_DEVICEID_BOOMERANG_100BT4,
151 		"3Com 3c905-T4 Fast Etherlink XL" },
152 	{ TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT,
153 		"3Com 3c900B-TPO Etherlink XL" },
154 	{ TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT_COMBO,
155 		"3Com 3c900B-COMBO Etherlink XL" },
156 	{ TC_VENDORID, TC_DEVICEID_KRAKATOA_10BT_TPC,
157 		"3Com 3c900B-TPC Etherlink XL" },
158 	{ TC_VENDORID, TC_DEVICEID_CYCLONE_10FL,
159 		"3Com 3c900B-FL Etherlink XL" },
160 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_10_100BT,
161 		"3Com 3c905B-TX Fast Etherlink XL" },
162 	{ TC_VENDORID, TC_DEVICEID_CYCLONE_10_100BT4,
163 		"3Com 3c905B-T4 Fast Etherlink XL" },
164 	{ TC_VENDORID, TC_DEVICEID_CYCLONE_10_100FX,
165 		"3Com 3c905B-FX/SC Fast Etherlink XL" },
166 	{ TC_VENDORID, TC_DEVICEID_CYCLONE_10_100_COMBO,
167 		"3Com 3c905B-COMBO Fast Etherlink XL" },
168 	{ TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT,
169 		"3Com 3c905C-TX Fast Etherlink XL" },
170 	{ TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT_920B,
171 		"3Com 3c920B-EMB Integrated Fast Etherlink XL" },
172 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_10_100BT_SERV,
173 		"3Com 3c980 Fast Etherlink XL" },
174 	{ TC_VENDORID, TC_DEVICEID_TORNADO_10_100BT_SERV,
175 		"3Com 3c980C Fast Etherlink XL" },
176 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_SOHO100TX,
177 		"3Com 3cSOHO100-TX OfficeConnect" },
178 	{ TC_VENDORID, TC_DEVICEID_TORNADO_HOMECONNECT,
179 		"3Com 3c450-TX HomeConnect" },
180 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_555,
181 		"3Com 3c555 Fast Etherlink XL" },
182 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_556,
183 		"3Com 3c556 Fast Etherlink XL" },
184 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_556B,
185 		"3Com 3c556B Fast Etherlink XL" },
186 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_575A,
187 		"3Com 3c575TX Fast Etherlink XL" },
188 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_575B,
189 		"3Com 3c575B Fast Etherlink XL" },
190 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_575C,
191 		"3Com 3c575C Fast Etherlink XL" },
192 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_656,
193 		"3Com 3c656 Fast Etherlink XL" },
194 	{ TC_VENDORID, TC_DEVICEID_HURRICANE_656B,
195 		"3Com 3c656B Fast Etherlink XL" },
196 	{ TC_VENDORID, TC_DEVICEID_TORNADO_656C,
197 		"3Com 3c656C Fast Etherlink XL" },
198 	{ 0, 0, NULL }
199 };
200 
201 static int xl_probe		(device_t);
202 static int xl_attach		(device_t);
203 static int xl_detach		(device_t);
204 static void xl_shutdown		(device_t);
205 static int xl_suspend		(device_t);
206 static int xl_resume		(device_t);
207 
208 static int xl_newbuf		(struct xl_softc *, struct xl_chain_onefrag *,
209 				 int);
210 static void xl_stats_update	(void *);
211 static void xl_stats_update_serialized(void *);
212 static int xl_encap		(struct xl_softc *, struct xl_chain *,
213 						struct mbuf *);
214 static void xl_rxeof		(struct xl_softc *, int);
215 static int xl_rx_resync		(struct xl_softc *);
216 static void xl_txeof		(struct xl_softc *);
217 static void xl_txeof_90xB	(struct xl_softc *);
218 static void xl_txeoc		(struct xl_softc *);
219 static void xl_intr		(void *);
220 static void xl_start_body	(struct ifnet *, int);
221 static void xl_start		(struct ifnet *, struct ifaltq_subque *);
222 static void xl_start_90xB	(struct ifnet *, struct ifaltq_subque *);
223 static int xl_ioctl		(struct ifnet *, u_long, caddr_t,
224 						struct ucred *);
225 static void xl_init		(void *);
226 static void xl_stop		(struct xl_softc *);
227 static void xl_watchdog		(struct ifnet *);
228 #ifdef IFPOLL_ENABLE
229 static void xl_start_poll	(struct ifnet *, struct ifaltq_subque *);
230 static void xl_npoll		(struct ifnet *, struct ifpoll_info *);
231 static void xl_npoll_compat	(struct ifnet *, void *, int);
232 #endif
233 static void xl_enable_intrs	(struct xl_softc *, uint16_t);
234 
235 static int xl_ifmedia_upd	(struct ifnet *);
236 static void xl_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
237 
238 static int xl_eeprom_wait	(struct xl_softc *);
239 static int xl_read_eeprom	(struct xl_softc *, caddr_t, int, int, int);
240 static void xl_mii_sync		(struct xl_softc *);
241 static void xl_mii_send		(struct xl_softc *, u_int32_t, int);
242 static int xl_mii_readreg	(struct xl_softc *, struct xl_mii_frame *);
243 static int xl_mii_writereg	(struct xl_softc *, struct xl_mii_frame *);
244 
245 static void xl_setcfg		(struct xl_softc *);
246 static void xl_setmode		(struct xl_softc *, int);
247 static void xl_setmulti		(struct xl_softc *);
248 static void xl_setmulti_hash	(struct xl_softc *);
249 static void xl_reset		(struct xl_softc *);
250 static int xl_list_rx_init	(struct xl_softc *);
251 static void xl_list_tx_init	(struct xl_softc *);
252 static void xl_list_tx_init_90xB(struct xl_softc *);
253 static void xl_wait		(struct xl_softc *);
254 static void xl_mediacheck	(struct xl_softc *);
255 static void xl_choose_xcvr	(struct xl_softc *, int);
256 
257 static int xl_dma_alloc		(device_t);
258 static void xl_dma_free		(device_t);
259 
260 #ifdef notdef
261 static void xl_testpacket	(struct xl_softc *);
262 #endif
263 
264 static int xl_miibus_readreg	(device_t, int, int);
265 static int xl_miibus_writereg	(device_t, int, int, int);
266 static void xl_miibus_statchg	(device_t);
267 static void xl_miibus_mediainit	(device_t);
268 
269 static device_method_t xl_methods[] = {
270 	/* Device interface */
271 	DEVMETHOD(device_probe,		xl_probe),
272 	DEVMETHOD(device_attach,	xl_attach),
273 	DEVMETHOD(device_detach,	xl_detach),
274 	DEVMETHOD(device_shutdown,	xl_shutdown),
275 	DEVMETHOD(device_suspend,	xl_suspend),
276 	DEVMETHOD(device_resume,	xl_resume),
277 
278 	/* bus interface */
279 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
280 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
281 
282 	/* MII interface */
283 	DEVMETHOD(miibus_readreg,	xl_miibus_readreg),
284 	DEVMETHOD(miibus_writereg,	xl_miibus_writereg),
285 	DEVMETHOD(miibus_statchg,	xl_miibus_statchg),
286 	DEVMETHOD(miibus_mediainit,	xl_miibus_mediainit),
287 
288 	DEVMETHOD_END
289 };
290 
291 static driver_t xl_driver = {
292 	"xl",
293 	xl_methods,
294 	sizeof(struct xl_softc)
295 };
296 
297 static devclass_t xl_devclass;
298 
299 DECLARE_DUMMY_MODULE(if_xl);
300 MODULE_DEPEND(if_xl, miibus, 1, 1, 1);
301 DRIVER_MODULE(if_xl, pci, xl_driver, xl_devclass, NULL, NULL);
302 DRIVER_MODULE(if_xl, cardbus, xl_driver, xl_devclass, NULL, NULL);
303 DRIVER_MODULE(miibus, xl, miibus_driver, miibus_devclass, NULL, NULL);
304 
305 static void
306 xl_enable_intrs(struct xl_softc *sc, uint16_t intrs)
307 {
308 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ACK | 0xFF);
309 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB | intrs);
310 	if (sc->xl_flags & XL_FLAG_FUNCREG)
311 		bus_space_write_4(sc->xl_ftag, sc->xl_fhandle, 4, 0x8000);
312 	sc->xl_npoll.ifpc_stcount = 0;
313 }
314 
315 /*
316  * Murphy's law says that it's possible the chip can wedge and
317  * the 'command in progress' bit may never clear. Hence, we wait
318  * only a finite amount of time to avoid getting caught in an
319  * infinite loop. Normally this delay routine would be a macro,
320  * but it isn't called during normal operation so we can afford
321  * to make it a function.
322  */
323 static void
324 xl_wait(struct xl_softc *sc)
325 {
326 	int		i;
327 
328 	for (i = 0; i < XL_TIMEOUT; i++) {
329 		if (!(CSR_READ_2(sc, XL_STATUS) & XL_STAT_CMDBUSY))
330 			break;
331 	}
332 
333 	if (i == XL_TIMEOUT)
334 		if_printf(&sc->arpcom.ac_if, "command never completed!");
335 
336 	return;
337 }
338 
339 /*
340  * MII access routines are provided for adapters with external
341  * PHYs (3c905-TX, 3c905-T4, 3c905B-T4) and those with built-in
342  * autoneg logic that's faked up to look like a PHY (3c905B-TX).
343  * Note: if you don't perform the MDIO operations just right,
344  * it's possible to end up with code that works correctly with
345  * some chips/CPUs/processor speeds/bus speeds/etc but not
346  * with others.
347  */
348 #define MII_SET(x)					\
349 	CSR_WRITE_2(sc, XL_W4_PHY_MGMT,			\
350 		CSR_READ_2(sc, XL_W4_PHY_MGMT) | (x))
351 
352 #define MII_CLR(x)					\
353 	CSR_WRITE_2(sc, XL_W4_PHY_MGMT,			\
354 		CSR_READ_2(sc, XL_W4_PHY_MGMT) & ~(x))
355 
356 /*
357  * Sync the PHYs by setting data bit and strobing the clock 32 times.
358  */
359 static void
360 xl_mii_sync(struct xl_softc *sc)
361 {
362 	int		i;
363 
364 	XL_SEL_WIN(4);
365 	MII_SET(XL_MII_DIR|XL_MII_DATA);
366 
367 	for (i = 0; i < 32; i++) {
368 		MII_SET(XL_MII_CLK);
369 		MII_SET(XL_MII_DATA);
370 		MII_SET(XL_MII_DATA);
371 		MII_CLR(XL_MII_CLK);
372 		MII_SET(XL_MII_DATA);
373 		MII_SET(XL_MII_DATA);
374 	}
375 
376 	return;
377 }
378 
379 /*
380  * Clock a series of bits through the MII.
381  */
382 static void
383 xl_mii_send(struct xl_softc *sc, u_int32_t bits, int cnt)
384 {
385 	int			i;
386 
387 	XL_SEL_WIN(4);
388 	MII_CLR(XL_MII_CLK);
389 
390 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
391                 if (bits & i) {
392 			MII_SET(XL_MII_DATA);
393                 } else {
394 			MII_CLR(XL_MII_DATA);
395                 }
396 		MII_CLR(XL_MII_CLK);
397 		MII_SET(XL_MII_CLK);
398 	}
399 }
400 
401 /*
402  * Read an PHY register through the MII.
403  */
404 static int
405 xl_mii_readreg(struct xl_softc *sc, struct xl_mii_frame *frame)
406 {
407 	int			i, ack;
408 
409 	/*
410 	 * Set up frame for RX.
411 	 */
412 	frame->mii_stdelim = XL_MII_STARTDELIM;
413 	frame->mii_opcode = XL_MII_READOP;
414 	frame->mii_turnaround = 0;
415 	frame->mii_data = 0;
416 
417 	/*
418 	 * Select register window 4.
419 	 */
420 
421 	XL_SEL_WIN(4);
422 
423 	CSR_WRITE_2(sc, XL_W4_PHY_MGMT, 0);
424 	/*
425  	 * Turn on data xmit.
426 	 */
427 	MII_SET(XL_MII_DIR);
428 
429 	xl_mii_sync(sc);
430 
431 	/*
432 	 * Send command/address info.
433 	 */
434 	xl_mii_send(sc, frame->mii_stdelim, 2);
435 	xl_mii_send(sc, frame->mii_opcode, 2);
436 	xl_mii_send(sc, frame->mii_phyaddr, 5);
437 	xl_mii_send(sc, frame->mii_regaddr, 5);
438 
439 	/* Idle bit */
440 	MII_CLR((XL_MII_CLK|XL_MII_DATA));
441 	MII_SET(XL_MII_CLK);
442 
443 	/* Turn off xmit. */
444 	MII_CLR(XL_MII_DIR);
445 
446 	/* Check for ack */
447 	MII_CLR(XL_MII_CLK);
448 	ack = CSR_READ_2(sc, XL_W4_PHY_MGMT) & XL_MII_DATA;
449 	MII_SET(XL_MII_CLK);
450 
451 	/*
452 	 * Now try reading data bits. If the ack failed, we still
453 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
454 	 */
455 	if (ack) {
456 		for(i = 0; i < 16; i++) {
457 			MII_CLR(XL_MII_CLK);
458 			MII_SET(XL_MII_CLK);
459 		}
460 		goto fail;
461 	}
462 
463 	for (i = 0x8000; i; i >>= 1) {
464 		MII_CLR(XL_MII_CLK);
465 		if (!ack) {
466 			if (CSR_READ_2(sc, XL_W4_PHY_MGMT) & XL_MII_DATA)
467 				frame->mii_data |= i;
468 		}
469 		MII_SET(XL_MII_CLK);
470 	}
471 
472 fail:
473 
474 	MII_CLR(XL_MII_CLK);
475 	MII_SET(XL_MII_CLK);
476 
477 	if (ack)
478 		return(1);
479 	return(0);
480 }
481 
482 /*
483  * Write to a PHY register through the MII.
484  */
485 static int
486 xl_mii_writereg(struct xl_softc *sc, struct xl_mii_frame *frame)
487 {
488 	/*
489 	 * Set up frame for TX.
490 	 */
491 
492 	frame->mii_stdelim = XL_MII_STARTDELIM;
493 	frame->mii_opcode = XL_MII_WRITEOP;
494 	frame->mii_turnaround = XL_MII_TURNAROUND;
495 
496 	/*
497 	 * Select the window 4.
498 	 */
499 	XL_SEL_WIN(4);
500 
501 	/*
502  	 * Turn on data output.
503 	 */
504 	MII_SET(XL_MII_DIR);
505 
506 	xl_mii_sync(sc);
507 
508 	xl_mii_send(sc, frame->mii_stdelim, 2);
509 	xl_mii_send(sc, frame->mii_opcode, 2);
510 	xl_mii_send(sc, frame->mii_phyaddr, 5);
511 	xl_mii_send(sc, frame->mii_regaddr, 5);
512 	xl_mii_send(sc, frame->mii_turnaround, 2);
513 	xl_mii_send(sc, frame->mii_data, 16);
514 
515 	/* Idle bit. */
516 	MII_SET(XL_MII_CLK);
517 	MII_CLR(XL_MII_CLK);
518 
519 	/*
520 	 * Turn off xmit.
521 	 */
522 	MII_CLR(XL_MII_DIR);
523 
524 	return(0);
525 }
526 
527 static int
528 xl_miibus_readreg(device_t dev, int phy, int reg)
529 {
530 	struct xl_softc		*sc;
531 	struct xl_mii_frame	frame;
532 
533 	sc = device_get_softc(dev);
534 
535 	/*
536 	 * Pretend that PHYs are only available at MII address 24.
537 	 * This is to guard against problems with certain 3Com ASIC
538 	 * revisions that incorrectly map the internal transceiver
539 	 * control registers at all MII addresses. This can cause
540 	 * the miibus code to attach the same PHY several times over.
541 	 */
542 	if ((!(sc->xl_flags & XL_FLAG_PHYOK)) && phy != 24)
543 		return(0);
544 
545 	bzero((char *)&frame, sizeof(frame));
546 
547 	frame.mii_phyaddr = phy;
548 	frame.mii_regaddr = reg;
549 	xl_mii_readreg(sc, &frame);
550 
551 	return(frame.mii_data);
552 }
553 
554 static int
555 xl_miibus_writereg(device_t dev, int phy, int reg, int data)
556 {
557 	struct xl_softc		*sc;
558 	struct xl_mii_frame	frame;
559 
560 	sc = device_get_softc(dev);
561 
562 	if ((!(sc->xl_flags & XL_FLAG_PHYOK)) && phy != 24)
563 		return(0);
564 
565 	bzero((char *)&frame, sizeof(frame));
566 
567 	frame.mii_phyaddr = phy;
568 	frame.mii_regaddr = reg;
569 	frame.mii_data = data;
570 
571 	xl_mii_writereg(sc, &frame);
572 
573 	return(0);
574 }
575 
576 static void
577 xl_miibus_statchg(device_t dev)
578 {
579         struct xl_softc		*sc;
580         struct mii_data		*mii;
581 
582 	sc = device_get_softc(dev);
583 	mii = device_get_softc(sc->xl_miibus);
584 
585 	ASSERT_SERIALIZED(sc->arpcom.ac_if.if_serializer);
586 
587 	xl_setcfg(sc);
588 
589 	/* Set ASIC's duplex mode to match the PHY. */
590 	XL_SEL_WIN(3);
591 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX)
592 		CSR_WRITE_1(sc, XL_W3_MAC_CTRL, XL_MACCTRL_DUPLEX);
593 	else
594 		CSR_WRITE_1(sc, XL_W3_MAC_CTRL,
595 			(CSR_READ_1(sc, XL_W3_MAC_CTRL) & ~XL_MACCTRL_DUPLEX));
596 }
597 
598 /*
599  * Special support for the 3c905B-COMBO. This card has 10/100 support
600  * plus BNC and AUI ports. This means we will have both an miibus attached
601  * plus some non-MII media settings. In order to allow this, we have to
602  * add the extra media to the miibus's ifmedia struct, but we can't do
603  * that during xl_attach() because the miibus hasn't been attached yet.
604  * So instead, we wait until the miibus probe/attach is done, at which
605  * point we will get a callback telling is that it's safe to add our
606  * extra media.
607  */
608 static void
609 xl_miibus_mediainit(device_t dev)
610 {
611         struct xl_softc		*sc;
612         struct mii_data		*mii;
613 	struct ifmedia		*ifm;
614 
615 	sc = device_get_softc(dev);
616 	mii = device_get_softc(sc->xl_miibus);
617 	ifm = &mii->mii_media;
618 
619 	if (sc->xl_media & (XL_MEDIAOPT_AUI|XL_MEDIAOPT_10FL)) {
620 		/*
621 		 * Check for a 10baseFL board in disguise.
622 		 */
623 		if (sc->xl_type == XL_TYPE_905B &&
624 		    sc->xl_media == XL_MEDIAOPT_10FL) {
625 			if (bootverbose)
626 				device_printf(dev, "found 10baseFL\n");
627 			ifmedia_add(ifm, IFM_ETHER|IFM_10_FL, 0, NULL);
628 			ifmedia_add(ifm, IFM_ETHER|IFM_10_FL|IFM_HDX, 0, NULL);
629 			if (sc->xl_caps & XL_CAPS_FULL_DUPLEX)
630 				ifmedia_add(ifm,
631 				    IFM_ETHER|IFM_10_FL|IFM_FDX, 0, NULL);
632 		} else {
633 			if (bootverbose)
634 				device_printf(dev, "found AUI\n");
635 			ifmedia_add(ifm, IFM_ETHER|IFM_10_5, 0, NULL);
636 		}
637 	}
638 
639 	if (sc->xl_media & XL_MEDIAOPT_BNC) {
640 		if (bootverbose)
641 			device_printf(dev, "found BNC\n");
642 		ifmedia_add(ifm, IFM_ETHER|IFM_10_2, 0, NULL);
643 	}
644 
645 	return;
646 }
647 
648 /*
649  * The EEPROM is slow: give it time to come ready after issuing
650  * it a command.
651  */
652 static int
653 xl_eeprom_wait(struct xl_softc *sc)
654 {
655 	int			i;
656 
657 	for (i = 0; i < 100; i++) {
658 		if (CSR_READ_2(sc, XL_W0_EE_CMD) & XL_EE_BUSY)
659 			DELAY(162);
660 		else
661 			break;
662 	}
663 
664 	if (i == 100) {
665 		if_printf(&sc->arpcom.ac_if, "eeprom failed to come ready\n");
666 		return(1);
667 	}
668 
669 	return(0);
670 }
671 
672 /*
673  * Read a sequence of words from the EEPROM. Note that ethernet address
674  * data is stored in the EEPROM in network byte order.
675  */
676 static int
677 xl_read_eeprom(struct xl_softc *sc, caddr_t dest, int off, int cnt, int swap)
678 {
679 	int			err = 0, i;
680 	u_int16_t		word = 0, *ptr;
681 #define EEPROM_5BIT_OFFSET(A) ((((A) << 2) & 0x7F00) | ((A) & 0x003F))
682 #define EEPROM_8BIT_OFFSET(A) ((A) & 0x003F)
683 	/* WARNING! DANGER!
684 	 * It's easy to accidentally overwrite the rom content!
685 	 * Note: the 3c575 uses 8bit EEPROM offsets.
686 	 */
687 	XL_SEL_WIN(0);
688 
689 	if (xl_eeprom_wait(sc))
690 		return(1);
691 
692 	if (sc->xl_flags & XL_FLAG_EEPROM_OFFSET_30)
693 		off += 0x30;
694 
695 	for (i = 0; i < cnt; i++) {
696 		if (sc->xl_flags & XL_FLAG_8BITROM)
697 			CSR_WRITE_2(sc, XL_W0_EE_CMD,
698 			    XL_EE_8BIT_READ | EEPROM_8BIT_OFFSET(off + i));
699 		else
700 			CSR_WRITE_2(sc, XL_W0_EE_CMD,
701 			    XL_EE_READ | EEPROM_5BIT_OFFSET(off + i));
702 		err = xl_eeprom_wait(sc);
703 		if (err)
704 			break;
705 		word = CSR_READ_2(sc, XL_W0_EE_DATA);
706 		ptr = (u_int16_t *)(dest + (i * 2));
707 		if (swap)
708 			*ptr = ntohs(word);
709 		else
710 			*ptr = word;
711 	}
712 
713 	return(err ? 1 : 0);
714 }
715 
716 /*
717  * NICs older than the 3c905B have only one multicast option, which
718  * is to enable reception of all multicast frames.
719  */
720 static void
721 xl_setmulti(struct xl_softc *sc)
722 {
723 	struct ifnet		*ifp;
724 	struct ifmultiaddr	*ifma;
725 	u_int8_t		rxfilt;
726 	int			mcnt = 0;
727 
728 	ifp = &sc->arpcom.ac_if;
729 
730 	XL_SEL_WIN(5);
731 	rxfilt = CSR_READ_1(sc, XL_W5_RX_FILTER);
732 
733 	if (ifp->if_flags & IFF_ALLMULTI) {
734 		rxfilt |= XL_RXFILTER_ALLMULTI;
735 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
736 		return;
737 	}
738 
739 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
740 		mcnt++;
741 
742 	if (mcnt)
743 		rxfilt |= XL_RXFILTER_ALLMULTI;
744 	else
745 		rxfilt &= ~XL_RXFILTER_ALLMULTI;
746 
747 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
748 
749 	return;
750 }
751 
752 /*
753  * 3c905B adapters have a hash filter that we can program.
754  */
755 static void
756 xl_setmulti_hash(struct xl_softc *sc)
757 {
758 	struct ifnet		*ifp;
759 	int			h = 0, i;
760 	struct ifmultiaddr	*ifma;
761 	u_int8_t		rxfilt;
762 	int			mcnt = 0;
763 
764 	ifp = &sc->arpcom.ac_if;
765 
766 	XL_SEL_WIN(5);
767 	rxfilt = CSR_READ_1(sc, XL_W5_RX_FILTER);
768 
769 	if (ifp->if_flags & IFF_ALLMULTI) {
770 		rxfilt |= XL_RXFILTER_ALLMULTI;
771 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
772 		return;
773 	} else
774 		rxfilt &= ~XL_RXFILTER_ALLMULTI;
775 
776 
777 	/* first, zot all the existing hash bits */
778 	for (i = 0; i < XL_HASHFILT_SIZE; i++)
779 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_HASH|i);
780 
781 	/* now program new ones */
782 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
783 		if (ifma->ifma_addr->sa_family != AF_LINK)
784 			continue;
785 
786 		/*
787 		 * Note: the 3c905B currently only supports a 64-bit
788 		 * hash table, which means we really only need 6 bits,
789 		 * but the manual indicates that future chip revisions
790 		 * will have a 256-bit hash table, hence the routine is
791 		 * set up to calculate 8 bits of position info in case
792 		 * we need it some day.
793 		 * Note II, The Sequel: _CURRENT_ versions of the 3c905B
794 		 * have a 256 bit hash table. This means we have to use
795 		 * all 8 bits regardless.  On older cards, the upper 2
796 		 * bits will be ignored. Grrrr....
797 		 */
798 		h = ether_crc32_be(
799 			LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
800 			ETHER_ADDR_LEN) & 0xff;
801 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_HASH|XL_HASH_SET|h);
802 		mcnt++;
803 	}
804 
805 	if (mcnt)
806 		rxfilt |= XL_RXFILTER_MULTIHASH;
807 	else
808 		rxfilt &= ~XL_RXFILTER_MULTIHASH;
809 
810 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
811 
812 	return;
813 }
814 
815 static void
816 xl_setcfg(struct xl_softc *sc)
817 {
818 	u_int32_t		icfg;
819 
820 	XL_SEL_WIN(3);
821 	icfg = CSR_READ_4(sc, XL_W3_INTERNAL_CFG);
822 	icfg &= ~XL_ICFG_CONNECTOR_MASK;
823 	if (sc->xl_media & XL_MEDIAOPT_MII ||
824 		sc->xl_media & XL_MEDIAOPT_BT4)
825 		icfg |= (XL_XCVR_MII << XL_ICFG_CONNECTOR_BITS);
826 	if (sc->xl_media & XL_MEDIAOPT_BTX)
827 		icfg |= (XL_XCVR_AUTO << XL_ICFG_CONNECTOR_BITS);
828 
829 	CSR_WRITE_4(sc, XL_W3_INTERNAL_CFG, icfg);
830 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
831 
832 	return;
833 }
834 
835 static void
836 xl_setmode(struct xl_softc *sc, int media)
837 {
838 	struct ifnet *ifp = &sc->arpcom.ac_if;
839 	u_int32_t		icfg;
840 	u_int16_t		mediastat;
841 
842 	if_printf(ifp, "selecting ");
843 
844 	XL_SEL_WIN(4);
845 	mediastat = CSR_READ_2(sc, XL_W4_MEDIA_STATUS);
846 	XL_SEL_WIN(3);
847 	icfg = CSR_READ_4(sc, XL_W3_INTERNAL_CFG);
848 
849 	if (sc->xl_media & XL_MEDIAOPT_BT) {
850 		if (IFM_SUBTYPE(media) == IFM_10_T) {
851 			kprintf("10baseT transceiver, ");
852 			sc->xl_xcvr = XL_XCVR_10BT;
853 			icfg &= ~XL_ICFG_CONNECTOR_MASK;
854 			icfg |= (XL_XCVR_10BT << XL_ICFG_CONNECTOR_BITS);
855 			mediastat |= XL_MEDIASTAT_LINKBEAT|
856 					XL_MEDIASTAT_JABGUARD;
857 			mediastat &= ~XL_MEDIASTAT_SQEENB;
858 		}
859 	}
860 
861 	if (sc->xl_media & XL_MEDIAOPT_BFX) {
862 		if (IFM_SUBTYPE(media) == IFM_100_FX) {
863 			kprintf("100baseFX port, ");
864 			sc->xl_xcvr = XL_XCVR_100BFX;
865 			icfg &= ~XL_ICFG_CONNECTOR_MASK;
866 			icfg |= (XL_XCVR_100BFX << XL_ICFG_CONNECTOR_BITS);
867 			mediastat |= XL_MEDIASTAT_LINKBEAT;
868 			mediastat &= ~XL_MEDIASTAT_SQEENB;
869 		}
870 	}
871 
872 	if (sc->xl_media & (XL_MEDIAOPT_AUI|XL_MEDIAOPT_10FL)) {
873 		if (IFM_SUBTYPE(media) == IFM_10_5) {
874 			kprintf("AUI port, ");
875 			sc->xl_xcvr = XL_XCVR_AUI;
876 			icfg &= ~XL_ICFG_CONNECTOR_MASK;
877 			icfg |= (XL_XCVR_AUI << XL_ICFG_CONNECTOR_BITS);
878 			mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
879 					XL_MEDIASTAT_JABGUARD);
880 			mediastat |= ~XL_MEDIASTAT_SQEENB;
881 		}
882 		if (IFM_SUBTYPE(media) == IFM_10_FL) {
883 			kprintf("10baseFL transceiver, ");
884 			sc->xl_xcvr = XL_XCVR_AUI;
885 			icfg &= ~XL_ICFG_CONNECTOR_MASK;
886 			icfg |= (XL_XCVR_AUI << XL_ICFG_CONNECTOR_BITS);
887 			mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
888 					XL_MEDIASTAT_JABGUARD);
889 			mediastat |= ~XL_MEDIASTAT_SQEENB;
890 		}
891 	}
892 
893 	if (sc->xl_media & XL_MEDIAOPT_BNC) {
894 		if (IFM_SUBTYPE(media) == IFM_10_2) {
895 			kprintf("BNC port, ");
896 			sc->xl_xcvr = XL_XCVR_COAX;
897 			icfg &= ~XL_ICFG_CONNECTOR_MASK;
898 			icfg |= (XL_XCVR_COAX << XL_ICFG_CONNECTOR_BITS);
899 			mediastat &= ~(XL_MEDIASTAT_LINKBEAT|
900 					XL_MEDIASTAT_JABGUARD|
901 					XL_MEDIASTAT_SQEENB);
902 		}
903 	}
904 
905 	if ((media & IFM_GMASK) == IFM_FDX ||
906 			IFM_SUBTYPE(media) == IFM_100_FX) {
907 		kprintf("full duplex\n");
908 		XL_SEL_WIN(3);
909 		CSR_WRITE_1(sc, XL_W3_MAC_CTRL, XL_MACCTRL_DUPLEX);
910 	} else {
911 		kprintf("half duplex\n");
912 		XL_SEL_WIN(3);
913 		CSR_WRITE_1(sc, XL_W3_MAC_CTRL,
914 			(CSR_READ_1(sc, XL_W3_MAC_CTRL) & ~XL_MACCTRL_DUPLEX));
915 	}
916 
917 	if (IFM_SUBTYPE(media) == IFM_10_2)
918 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_START);
919 	else
920 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
921 	CSR_WRITE_4(sc, XL_W3_INTERNAL_CFG, icfg);
922 	XL_SEL_WIN(4);
923 	CSR_WRITE_2(sc, XL_W4_MEDIA_STATUS, mediastat);
924 	DELAY(800);
925 	XL_SEL_WIN(7);
926 }
927 
928 static void
929 xl_reset(struct xl_softc *sc)
930 {
931 	int		i;
932 
933 	XL_SEL_WIN(0);
934 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RESET |
935 		    ((sc->xl_flags & XL_FLAG_WEIRDRESET) ?
936 		     XL_RESETOPT_DISADVFD:0));
937 
938 	/*
939 	 * If we're using memory mapped register mode, pause briefly
940 	 * after issuing the reset command before trying to access any
941 	 * other registers. With my 3c575C cardbus card, failing to do
942 	 * this results in the system locking up while trying to poll
943 	 * the command busy bit in the status register.
944 	 */
945 	if (sc->xl_flags & XL_FLAG_USE_MMIO)
946 		DELAY(100000);
947 
948 	for (i = 0; i < XL_TIMEOUT; i++) {
949 		DELAY(10);
950 		if (!(CSR_READ_2(sc, XL_STATUS) & XL_STAT_CMDBUSY))
951 			break;
952 	}
953 
954 	if (i == XL_TIMEOUT)
955 		if_printf(&sc->arpcom.ac_if, "reset didn't complete\n");
956 
957 	/* Reset TX and RX. */
958 	/* Note: the RX reset takes an absurd amount of time
959 	 * on newer versions of the Tornado chips such as those
960 	 * on the 3c905CX and newer 3c908C cards. We wait an
961 	 * extra amount of time so that xl_wait() doesn't complain
962 	 * and annoy the users.
963 	 */
964 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_RESET);
965 	DELAY(100000);
966 	xl_wait(sc);
967 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET);
968 	xl_wait(sc);
969 
970 	if (sc->xl_flags & XL_FLAG_INVERT_LED_PWR ||
971 	    sc->xl_flags & XL_FLAG_INVERT_MII_PWR) {
972 		XL_SEL_WIN(2);
973 		CSR_WRITE_2(sc, XL_W2_RESET_OPTIONS, CSR_READ_2(sc,
974 		    XL_W2_RESET_OPTIONS)
975 		    | ((sc->xl_flags & XL_FLAG_INVERT_LED_PWR)?XL_RESETOPT_INVERT_LED:0)
976 		    | ((sc->xl_flags & XL_FLAG_INVERT_MII_PWR)?XL_RESETOPT_INVERT_MII:0)
977 		    );
978 	}
979 
980 	/* Wait a little while for the chip to get its brains in order. */
981 	DELAY(100000);
982         return;
983 }
984 
985 /*
986  * Probe for a 3Com Etherlink XL chip. Check the PCI vendor and device
987  * IDs against our list and return a device name if we find a match.
988  */
989 static int
990 xl_probe(device_t dev)
991 {
992 	struct xl_type *t;
993 	uint16_t vid, did;
994 
995 	vid = pci_get_vendor(dev);
996 	did = pci_get_device(dev);
997 	for (t = xl_devs; t->xl_name != NULL; t++) {
998 		if (vid == t->xl_vid && did == t->xl_did) {
999 			device_set_desc(dev, t->xl_name);
1000 			return(0);
1001 		}
1002 	}
1003 	return(ENXIO);
1004 }
1005 
1006 /*
1007  * This routine is a kludge to work around possible hardware faults
1008  * or manufacturing defects that can cause the media options register
1009  * (or reset options register, as it's called for the first generation
1010  * 3c90x adapters) to return an incorrect result. I have encountered
1011  * one Dell Latitude laptop docking station with an integrated 3c905-TX
1012  * which doesn't have any of the 'mediaopt' bits set. This screws up
1013  * the attach routine pretty badly because it doesn't know what media
1014  * to look for. If we find ourselves in this predicament, this routine
1015  * will try to guess the media options values and warn the user of a
1016  * possible manufacturing defect with his adapter/system/whatever.
1017  */
1018 static void
1019 xl_mediacheck(struct xl_softc *sc)
1020 {
1021 	struct ifnet *ifp = &sc->arpcom.ac_if;
1022 
1023 	/*
1024 	 * If some of the media options bits are set, assume they are
1025 	 * correct. If not, try to figure it out down below.
1026 	 * XXX I should check for 10baseFL, but I don't have an adapter
1027 	 * to test with.
1028 	 */
1029 	if (sc->xl_media & (XL_MEDIAOPT_MASK & ~XL_MEDIAOPT_VCO)) {
1030 		/*
1031 	 	 * Check the XCVR value. If it's not in the normal range
1032 	 	 * of values, we need to fake it up here.
1033 	 	 */
1034 		if (sc->xl_xcvr <= XL_XCVR_AUTO)
1035 			return;
1036 		else {
1037 			if_printf(ifp, "bogus xcvr value in EEPROM (%x)\n",
1038 			    sc->xl_xcvr);
1039 			if_printf(ifp,
1040 			    "choosing new default based on card type\n");
1041 		}
1042 	} else {
1043 		if (sc->xl_type == XL_TYPE_905B &&
1044 		    sc->xl_media & XL_MEDIAOPT_10FL)
1045 			return;
1046 		if_printf(ifp, "WARNING: no media options bits set in "
1047 			"the media options register!!\n");
1048 		if_printf(ifp, "this could be a manufacturing defect in "
1049 			"your adapter or system\n");
1050 		if_printf(ifp, "attempting to guess media type; you "
1051 			"should probably consult your vendor\n");
1052 	}
1053 
1054 	xl_choose_xcvr(sc, 1);
1055 }
1056 
1057 static void
1058 xl_choose_xcvr(struct xl_softc *sc, int verbose)
1059 {
1060 	struct ifnet *ifp = &sc->arpcom.ac_if;
1061 	u_int16_t		devid;
1062 
1063 	/*
1064 	 * Read the device ID from the EEPROM.
1065 	 * This is what's loaded into the PCI device ID register, so it has
1066 	 * to be correct otherwise we wouldn't have gotten this far.
1067 	 */
1068 	devid = 0;	/* silence gcc warnings */
1069 	xl_read_eeprom(sc, (caddr_t)&devid, XL_EE_PRODID, 1, 0);
1070 
1071 	switch(devid) {
1072 	case TC_DEVICEID_BOOMERANG_10BT:	/* 3c900-TPO */
1073 	case TC_DEVICEID_KRAKATOA_10BT:		/* 3c900B-TPO */
1074 		sc->xl_media = XL_MEDIAOPT_BT;
1075 		sc->xl_xcvr = XL_XCVR_10BT;
1076 		if (verbose)
1077 			if_printf(ifp, "guessing 10BaseT transceiver\n");
1078 		break;
1079 	case TC_DEVICEID_BOOMERANG_10BT_COMBO:	/* 3c900-COMBO */
1080 	case TC_DEVICEID_KRAKATOA_10BT_COMBO:	/* 3c900B-COMBO */
1081 		sc->xl_media = XL_MEDIAOPT_BT|XL_MEDIAOPT_BNC|XL_MEDIAOPT_AUI;
1082 		sc->xl_xcvr = XL_XCVR_10BT;
1083 		if (verbose)
1084 			if_printf(ifp, "guessing COMBO (AUI/BNC/TP)\n");
1085 		break;
1086 	case TC_DEVICEID_KRAKATOA_10BT_TPC:	/* 3c900B-TPC */
1087 		sc->xl_media = XL_MEDIAOPT_BT|XL_MEDIAOPT_BNC;
1088 		sc->xl_xcvr = XL_XCVR_10BT;
1089 		if (verbose)
1090 			if_printf(ifp, "guessing TPC (BNC/TP)\n");
1091 		break;
1092 	case TC_DEVICEID_CYCLONE_10FL:		/* 3c900B-FL */
1093 		sc->xl_media = XL_MEDIAOPT_10FL;
1094 		sc->xl_xcvr = XL_XCVR_AUI;
1095 		if (verbose)
1096 			if_printf(ifp, "guessing 10baseFL\n");
1097 		break;
1098 	case TC_DEVICEID_BOOMERANG_10_100BT:	/* 3c905-TX */
1099 	case TC_DEVICEID_HURRICANE_555:		/* 3c555 */
1100 	case TC_DEVICEID_HURRICANE_556:		/* 3c556 */
1101 	case TC_DEVICEID_HURRICANE_556B:	/* 3c556B */
1102 	case TC_DEVICEID_HURRICANE_575A:	/* 3c575TX */
1103 	case TC_DEVICEID_HURRICANE_575B:	/* 3c575B */
1104 	case TC_DEVICEID_HURRICANE_575C:	/* 3c575C */
1105 	case TC_DEVICEID_HURRICANE_656:		/* 3c656 */
1106 	case TC_DEVICEID_HURRICANE_656B:	/* 3c656B */
1107 	case TC_DEVICEID_TORNADO_656C:		/* 3c656C */
1108 	case TC_DEVICEID_TORNADO_10_100BT_920B:	/* 3c920B-EMB */
1109 		sc->xl_media = XL_MEDIAOPT_MII;
1110 		sc->xl_xcvr = XL_XCVR_MII;
1111 		if (verbose)
1112 			if_printf(ifp, "guessing MII\n");
1113 		break;
1114 	case TC_DEVICEID_BOOMERANG_100BT4:	/* 3c905-T4 */
1115 	case TC_DEVICEID_CYCLONE_10_100BT4:	/* 3c905B-T4 */
1116 		sc->xl_media = XL_MEDIAOPT_BT4;
1117 		sc->xl_xcvr = XL_XCVR_MII;
1118 		if (verbose)
1119 			if_printf(ifp, "guessing 100BaseT4/MII\n");
1120 		break;
1121 	case TC_DEVICEID_HURRICANE_10_100BT:	/* 3c905B-TX */
1122 	case TC_DEVICEID_HURRICANE_10_100BT_SERV:/*3c980-TX */
1123 	case TC_DEVICEID_TORNADO_10_100BT_SERV:	/* 3c980C-TX */
1124 	case TC_DEVICEID_HURRICANE_SOHO100TX:	/* 3cSOHO100-TX */
1125 	case TC_DEVICEID_TORNADO_10_100BT:	/* 3c905C-TX */
1126 	case TC_DEVICEID_TORNADO_HOMECONNECT:	/* 3c450-TX */
1127 		sc->xl_media = XL_MEDIAOPT_BTX;
1128 		sc->xl_xcvr = XL_XCVR_AUTO;
1129 		if (verbose)
1130 			if_printf(ifp, "guessing 10/100 internal\n");
1131 		break;
1132 	case TC_DEVICEID_CYCLONE_10_100_COMBO:	/* 3c905B-COMBO */
1133 		sc->xl_media = XL_MEDIAOPT_BTX|XL_MEDIAOPT_BNC|XL_MEDIAOPT_AUI;
1134 		sc->xl_xcvr = XL_XCVR_AUTO;
1135 		if (verbose)
1136 			if_printf(ifp, "guessing 10/100 plus BNC/AUI\n");
1137 		break;
1138 	default:
1139 		if_printf(ifp,
1140 		    "unknown device ID: %x -- defaulting to 10baseT\n", devid);
1141 		sc->xl_media = XL_MEDIAOPT_BT;
1142 		break;
1143 	}
1144 
1145 	return;
1146 }
1147 
1148 /*
1149  * Attach the interface. Allocate softc structures, do ifmedia
1150  * setup and ethernet/BPF attach.
1151  */
1152 static int
1153 xl_attach(device_t dev)
1154 {
1155 	u_char			eaddr[ETHER_ADDR_LEN];
1156 	u_int16_t		xcvr[2];
1157 	struct xl_softc		*sc;
1158 	struct ifnet		*ifp;
1159 	int			media = IFM_ETHER|IFM_100_TX|IFM_FDX;
1160 	int			error = 0, rid, res;
1161 	uint16_t		did;
1162 
1163 	sc = device_get_softc(dev);
1164 
1165 	ifmedia_init(&sc->ifmedia, 0, xl_ifmedia_upd, xl_ifmedia_sts);
1166 
1167 	did = pci_get_device(dev);
1168 
1169 	sc->xl_flags = 0;
1170 	if (did == TC_DEVICEID_HURRICANE_555)
1171 		sc->xl_flags |= XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_PHYOK;
1172 	if (did == TC_DEVICEID_HURRICANE_556 ||
1173 	    did == TC_DEVICEID_HURRICANE_556B)
1174 		sc->xl_flags |= XL_FLAG_FUNCREG | XL_FLAG_PHYOK |
1175 		    XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_WEIRDRESET |
1176 		    XL_FLAG_INVERT_LED_PWR | XL_FLAG_INVERT_MII_PWR;
1177 	if (did == TC_DEVICEID_HURRICANE_555 ||
1178 	    did == TC_DEVICEID_HURRICANE_556)
1179 		sc->xl_flags |= XL_FLAG_8BITROM;
1180 	if (did == TC_DEVICEID_HURRICANE_556B)
1181 		sc->xl_flags |= XL_FLAG_NO_XCVR_PWR;
1182 	if (did == TC_DEVICEID_HURRICANE_575B ||
1183 	    did == TC_DEVICEID_HURRICANE_575C ||
1184 	    did == TC_DEVICEID_HURRICANE_656B ||
1185 	    did == TC_DEVICEID_TORNADO_656C)
1186 		sc->xl_flags |= XL_FLAG_FUNCREG;
1187 	if (did == TC_DEVICEID_HURRICANE_575A ||
1188 	    did == TC_DEVICEID_HURRICANE_575B ||
1189 	    did == TC_DEVICEID_HURRICANE_575C ||
1190 	    did == TC_DEVICEID_HURRICANE_656B ||
1191 	    did == TC_DEVICEID_TORNADO_656C)
1192 		sc->xl_flags |= XL_FLAG_PHYOK | XL_FLAG_EEPROM_OFFSET_30 |
1193 		    XL_FLAG_8BITROM;
1194 	if (did == TC_DEVICEID_HURRICANE_656)
1195 		sc->xl_flags |= XL_FLAG_FUNCREG | XL_FLAG_PHYOK;
1196 	if (did == TC_DEVICEID_HURRICANE_575B)
1197 		sc->xl_flags |= XL_FLAG_INVERT_LED_PWR;
1198 	if (did == TC_DEVICEID_HURRICANE_575C)
1199 		sc->xl_flags |= XL_FLAG_INVERT_MII_PWR;
1200 	if (did == TC_DEVICEID_TORNADO_656C)
1201 		sc->xl_flags |= XL_FLAG_INVERT_MII_PWR;
1202 	if (did == TC_DEVICEID_HURRICANE_656 ||
1203 	    did == TC_DEVICEID_HURRICANE_656B)
1204 		sc->xl_flags |= XL_FLAG_INVERT_MII_PWR |
1205 		    XL_FLAG_INVERT_LED_PWR;
1206 	if (did == TC_DEVICEID_TORNADO_10_100BT_920B)
1207 		sc->xl_flags |= XL_FLAG_PHYOK;
1208 #ifndef BURN_BRIDGES
1209 	/*
1210 	 * If this is a 3c905B, we have to check one extra thing.
1211 	 * The 905B supports power management and may be placed in
1212 	 * a low-power mode (D3 mode), typically by certain operating
1213 	 * systems which shall not be named. The PCI BIOS is supposed
1214 	 * to reset the NIC and bring it out of low-power mode, but
1215 	 * some do not. Consequently, we have to see if this chip
1216 	 * supports power management, and if so, make sure it's not
1217 	 * in low-power mode. If power management is available, the
1218 	 * capid byte will be 0x01.
1219 	 *
1220 	 * I _think_ that what actually happens is that the chip
1221 	 * loses its PCI configuration during the transition from
1222 	 * D3 back to D0; this means that it should be possible for
1223 	 * us to save the PCI iobase, membase and IRQ, put the chip
1224 	 * back in the D0 state, then restore the PCI config ourselves.
1225 	 */
1226 
1227 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
1228 		u_int32_t		iobase, membase, irq;
1229 
1230 		/* Save important PCI config data. */
1231 		iobase = pci_read_config(dev, XL_PCI_LOIO, 4);
1232 		membase = pci_read_config(dev, XL_PCI_LOMEM, 4);
1233 		irq = pci_read_config(dev, XL_PCI_INTLINE, 4);
1234 
1235 		/* Reset the power state. */
1236 		device_printf(dev, "chip is in D%d power mode "
1237 		    "-- setting to D0\n", pci_get_powerstate(dev));
1238 
1239 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
1240 
1241 		/* Restore PCI config data. */
1242 		pci_write_config(dev, XL_PCI_LOIO, iobase, 4);
1243 		pci_write_config(dev, XL_PCI_LOMEM, membase, 4);
1244 		pci_write_config(dev, XL_PCI_INTLINE, irq, 4);
1245 	}
1246 #endif
1247 	/*
1248 	 * Map control/status registers.
1249 	 */
1250 	pci_enable_busmaster(dev);
1251 
1252 	rid = XL_PCI_LOMEM;
1253 	res = SYS_RES_MEMORY;
1254 
1255 #if 0
1256 	sc->xl_res = bus_alloc_resource_any(dev, res, &rid, RF_ACTIVE);
1257 #endif
1258 
1259 	if (sc->xl_res != NULL) {
1260 		sc->xl_flags |= XL_FLAG_USE_MMIO;
1261 		if (bootverbose)
1262 			device_printf(dev, "using memory mapped I/O\n");
1263 	} else {
1264 		rid = XL_PCI_LOIO;
1265 		res = SYS_RES_IOPORT;
1266 		sc->xl_res = bus_alloc_resource_any(dev, res, &rid, RF_ACTIVE);
1267 		if (sc->xl_res == NULL) {
1268 			device_printf(dev, "couldn't map ports/memory\n");
1269 			error = ENXIO;
1270 			goto fail;
1271 		}
1272 		if (bootverbose)
1273 			device_printf(dev, "using port I/O\n");
1274 	}
1275 
1276 	sc->xl_btag = rman_get_bustag(sc->xl_res);
1277 	sc->xl_bhandle = rman_get_bushandle(sc->xl_res);
1278 
1279 	if (sc->xl_flags & XL_FLAG_FUNCREG) {
1280 		rid = XL_PCI_FUNCMEM;
1281 		sc->xl_fres = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
1282 		    RF_ACTIVE);
1283 
1284 		if (sc->xl_fres == NULL) {
1285 			device_printf(dev, "couldn't map funcreg memory\n");
1286 			error = ENXIO;
1287 			goto fail;
1288 		}
1289 
1290 		sc->xl_ftag = rman_get_bustag(sc->xl_fres);
1291 		sc->xl_fhandle = rman_get_bushandle(sc->xl_fres);
1292 	}
1293 
1294 	/* Allocate interrupt */
1295 	rid = 0;
1296 	sc->xl_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1297 	    RF_SHAREABLE | RF_ACTIVE);
1298 	if (sc->xl_irq == NULL) {
1299 		device_printf(dev, "couldn't map interrupt\n");
1300 		error = ENXIO;
1301 		goto fail;
1302 	}
1303 
1304 	ifp = &sc->arpcom.ac_if;
1305 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1306 
1307 	/* Reset the adapter. */
1308 	xl_reset(sc);
1309 
1310 	/*
1311 	 * Get station address from the EEPROM.
1312 	 */
1313 	if (xl_read_eeprom(sc, (caddr_t)&eaddr, XL_EE_OEM_ADR0, 3, 1)) {
1314 		device_printf(dev, "failed to read station address\n");
1315 		error = ENXIO;
1316 		goto fail;
1317 	}
1318 
1319 	callout_init(&sc->xl_stat_timer);
1320 
1321 	error = xl_dma_alloc(dev);
1322 	if (error)
1323 		goto fail;
1324 
1325 	/*
1326 	 * Figure out the card type. 3c905B adapters have the
1327 	 * 'supportsNoTxLength' bit set in the capabilities
1328 	 * word in the EEPROM.
1329 	 * Note: my 3c575C cardbus card lies. It returns a value
1330 	 * of 0x1578 for its capabilities word, which is somewhat
1331  	 * nonsensical. Another way to distinguish a 3c90x chip
1332 	 * from a 3c90xB/C chip is to check for the 'supportsLargePackets'
1333 	 * bit. This will only be set for 3c90x boomerage chips.
1334 	 */
1335 	xl_read_eeprom(sc, (caddr_t)&sc->xl_caps, XL_EE_CAPS, 1, 0);
1336 	if (sc->xl_caps & XL_CAPS_NO_TXLENGTH ||
1337 	    !(sc->xl_caps & XL_CAPS_LARGE_PKTS))
1338 		sc->xl_type = XL_TYPE_905B;
1339 	else
1340 		sc->xl_type = XL_TYPE_90X;
1341 	if (bootverbose) {
1342 		device_printf(dev, "type %s\n",
1343 			      sc->xl_type == XL_TYPE_905B ? "90XB" : "90X");
1344 	}
1345 
1346 	ifp->if_softc = sc;
1347 	ifp->if_mtu = ETHERMTU;
1348 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1349 	ifp->if_ioctl = xl_ioctl;
1350 	if (sc->xl_type == XL_TYPE_905B) {
1351 		ifp->if_start = xl_start_90xB;
1352 		ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_VLAN_MTU;
1353 	} else {
1354 		ifp->if_start = xl_start;
1355 	}
1356 	ifp->if_watchdog = xl_watchdog;
1357 	ifp->if_init = xl_init;
1358 #ifdef IFPOLL_ENABLE
1359 	ifp->if_npoll = xl_npoll;
1360 #endif
1361 	ifp->if_baudrate = 10000000;
1362 	ifq_set_maxlen(&ifp->if_snd, XL_TX_LIST_CNT - 1);
1363 	ifq_set_ready(&ifp->if_snd);
1364 	/*
1365 	 * NOTE: Hardware checksum features disabled by default.
1366 	 * This seems to corrupt tx packet data one out of a
1367 	 * million packets or so and then generates a good checksum
1368 	 * so the receiver doesn't know the packet is bad
1369 	 */
1370 	ifp->if_capenable = ifp->if_capabilities & ~IFCAP_HWCSUM;
1371 	if (ifp->if_capenable & IFCAP_TXCSUM)
1372 		ifp->if_hwassist = XL905B_CSUM_FEATURES;
1373 
1374 	/*
1375 	 * Now we have to see what sort of media we have.
1376 	 * This includes probing for an MII interace and a
1377 	 * possible PHY.
1378 	 */
1379 	XL_SEL_WIN(3);
1380 	sc->xl_media = CSR_READ_2(sc, XL_W3_MEDIA_OPT);
1381 	if (bootverbose)
1382 		if_printf(ifp, "media options word: %x\n", sc->xl_media);
1383 
1384 	xl_read_eeprom(sc, (char *)&xcvr, XL_EE_ICFG_0, 2, 0);
1385 	sc->xl_xcvr = xcvr[0] | xcvr[1] << 16;
1386 	sc->xl_xcvr &= XL_ICFG_CONNECTOR_MASK;
1387 	sc->xl_xcvr >>= XL_ICFG_CONNECTOR_BITS;
1388 
1389 	xl_mediacheck(sc);
1390 
1391 	if (sc->xl_media & XL_MEDIAOPT_MII || sc->xl_media & XL_MEDIAOPT_BTX
1392 			|| sc->xl_media & XL_MEDIAOPT_BT4) {
1393 		if (bootverbose)
1394 			if_printf(ifp, "found MII/AUTO\n");
1395 		xl_setcfg(sc);
1396 
1397 		error = mii_phy_probe(dev, &sc->xl_miibus,
1398 				      xl_ifmedia_upd, xl_ifmedia_sts);
1399 		if (error) {
1400 			if_printf(ifp, "no PHY found!\n");
1401 			goto fail;
1402 		}
1403 
1404 		goto done;
1405 	}
1406 
1407 	/*
1408 	 * Sanity check. If the user has selected "auto" and this isn't
1409 	 * a 10/100 card of some kind, we need to force the transceiver
1410 	 * type to something sane.
1411 	 */
1412 	if (sc->xl_xcvr == XL_XCVR_AUTO)
1413 		xl_choose_xcvr(sc, bootverbose);
1414 
1415 	/*
1416 	 * Do ifmedia setup.
1417 	 */
1418 	if (sc->xl_media & XL_MEDIAOPT_BT) {
1419 		if (bootverbose)
1420 			if_printf(ifp, "found 10baseT\n");
1421 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
1422 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
1423 		if (sc->xl_caps & XL_CAPS_FULL_DUPLEX)
1424 			ifmedia_add(&sc->ifmedia,
1425 			    IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
1426 	}
1427 
1428 	if (sc->xl_media & (XL_MEDIAOPT_AUI|XL_MEDIAOPT_10FL)) {
1429 		/*
1430 		 * Check for a 10baseFL board in disguise.
1431 		 */
1432 		if (sc->xl_type == XL_TYPE_905B &&
1433 		    sc->xl_media == XL_MEDIAOPT_10FL) {
1434 			if (bootverbose)
1435 				if_printf(ifp, "found 10baseFL\n");
1436 			ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_FL, 0, NULL);
1437 			ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_FL|IFM_HDX,
1438 			    0, NULL);
1439 			if (sc->xl_caps & XL_CAPS_FULL_DUPLEX)
1440 				ifmedia_add(&sc->ifmedia,
1441 				    IFM_ETHER|IFM_10_FL|IFM_FDX, 0, NULL);
1442 		} else {
1443 			if (bootverbose)
1444 				if_printf(ifp, "found AUI\n");
1445 			ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
1446 		}
1447 	}
1448 
1449 	if (sc->xl_media & XL_MEDIAOPT_BNC) {
1450 		if (bootverbose)
1451 			if_printf(ifp, "found BNC\n");
1452 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_2, 0, NULL);
1453 	}
1454 
1455 	if (sc->xl_media & XL_MEDIAOPT_BFX) {
1456 		if (bootverbose)
1457 			if_printf(ifp, "found 100baseFX\n");
1458 		ifp->if_baudrate = 100000000;
1459 		ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_100_FX, 0, NULL);
1460 	}
1461 
1462 	/* Choose a default media. */
1463 	switch(sc->xl_xcvr) {
1464 	case XL_XCVR_10BT:
1465 		media = IFM_ETHER|IFM_10_T;
1466 		xl_setmode(sc, media);
1467 		break;
1468 	case XL_XCVR_AUI:
1469 		if (sc->xl_type == XL_TYPE_905B &&
1470 		    sc->xl_media == XL_MEDIAOPT_10FL) {
1471 			media = IFM_ETHER|IFM_10_FL;
1472 			xl_setmode(sc, media);
1473 		} else {
1474 			media = IFM_ETHER|IFM_10_5;
1475 			xl_setmode(sc, media);
1476 		}
1477 		break;
1478 	case XL_XCVR_COAX:
1479 		media = IFM_ETHER|IFM_10_2;
1480 		xl_setmode(sc, media);
1481 		break;
1482 	case XL_XCVR_AUTO:
1483 	case XL_XCVR_100BTX:
1484 	case XL_XCVR_MII:
1485 		/* Chosen by miibus */
1486 		break;
1487 	case XL_XCVR_100BFX:
1488 		media = IFM_ETHER|IFM_100_FX;
1489 		break;
1490 	default:
1491 		if_printf(ifp, "unknown XCVR type: %d\n", sc->xl_xcvr);
1492 		/*
1493 		 * This will probably be wrong, but it prevents
1494 	 	 * the ifmedia code from panicking.
1495 		 */
1496 		media = IFM_ETHER|IFM_10_T;
1497 		break;
1498 	}
1499 
1500 	if (sc->xl_miibus == NULL)
1501 		ifmedia_set(&sc->ifmedia, media);
1502 
1503 done:
1504 
1505 	if (sc->xl_flags & XL_FLAG_NO_XCVR_PWR) {
1506 		XL_SEL_WIN(0);
1507 		CSR_WRITE_2(sc, XL_W0_MFG_ID, XL_NO_XCVR_PWR_MAGICBITS);
1508 	}
1509 
1510 	/*
1511 	 * Call MI attach routine.
1512 	 */
1513 	ether_ifattach(ifp, eaddr, NULL);
1514 
1515 	ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->xl_irq));
1516 
1517 #ifdef IFPOLL_ENABLE
1518 	ifpoll_compat_setup(&sc->xl_npoll, NULL, NULL, device_get_unit(dev),
1519 	    ifp->if_serializer);
1520 #endif
1521 
1522         /*
1523          * Tell the upper layer(s) we support long frames.
1524          */
1525         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1526 
1527 	/* Hook interrupt last to avoid having to lock softc */
1528 	error = bus_setup_intr(dev, sc->xl_irq, INTR_MPSAFE,
1529 			       xl_intr, sc, &sc->xl_intrhand,
1530 			       ifp->if_serializer);
1531 	if (error) {
1532 		if_printf(ifp, "couldn't set up irq\n");
1533 		ether_ifdetach(ifp);
1534 		goto fail;
1535 	}
1536 
1537 	return 0;
1538 
1539 fail:
1540 	xl_detach(dev);
1541 	return error;
1542 }
1543 
1544 /*
1545  * Shutdown hardware and free up resources. This can be called any
1546  * time after the mutex has been initialized. It is called in both
1547  * the error case in attach and the normal detach case so it needs
1548  * to be careful about only freeing resources that have actually been
1549  * allocated.
1550  */
1551 static int
1552 xl_detach(device_t dev)
1553 {
1554 	struct xl_softc		*sc;
1555 	struct ifnet		*ifp;
1556 	int			rid, res;
1557 
1558 	sc = device_get_softc(dev);
1559 	ifp = &sc->arpcom.ac_if;
1560 
1561 	if (sc->xl_flags & XL_FLAG_USE_MMIO) {
1562 		rid = XL_PCI_LOMEM;
1563 		res = SYS_RES_MEMORY;
1564 	} else {
1565 		rid = XL_PCI_LOIO;
1566 		res = SYS_RES_IOPORT;
1567 	}
1568 
1569 	if (device_is_attached(dev)) {
1570 		lwkt_serialize_enter(ifp->if_serializer);
1571 		xl_reset(sc);
1572 		xl_stop(sc);
1573 		bus_teardown_intr(dev, sc->xl_irq, sc->xl_intrhand);
1574 		lwkt_serialize_exit(ifp->if_serializer);
1575 
1576 		ether_ifdetach(ifp);
1577 	}
1578 
1579 	if (sc->xl_miibus)
1580 		device_delete_child(dev, sc->xl_miibus);
1581 	bus_generic_detach(dev);
1582 	ifmedia_removeall(&sc->ifmedia);
1583 
1584 	if (sc->xl_irq)
1585 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->xl_irq);
1586 	if (sc->xl_fres != NULL)
1587 		bus_release_resource(dev, SYS_RES_MEMORY,
1588 		    XL_PCI_FUNCMEM, sc->xl_fres);
1589 	if (sc->xl_res)
1590 		bus_release_resource(dev, res, rid, sc->xl_res);
1591 
1592 	xl_dma_free(dev);
1593 
1594 	return(0);
1595 }
1596 
1597 static int
1598 xl_dma_alloc(device_t dev)
1599 {
1600 	struct xl_softc *sc;
1601 	struct xl_chain_data *cd;
1602 	struct xl_list_data *ld;
1603 	bus_dmamem_t dmem;
1604 	int i, error;
1605 
1606 	sc = device_get_softc(dev);
1607 	cd = &sc->xl_cdata;
1608 	ld = &sc->xl_ldata;
1609 
1610 	/*
1611 	 * Allocate the parent bus DMA tag appropriate for PCI.
1612 	 */
1613 	error = bus_dma_tag_create(NULL, 1, 0,
1614 				   BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
1615 				   NULL, NULL,
1616 				   BUS_SPACE_MAXSIZE_32BIT, 0,
1617 				   BUS_SPACE_MAXSIZE_32BIT,
1618 				   0, &sc->xl_parent_tag);
1619 	if (error) {
1620 		device_printf(dev, "could not allocate parent dma tag\n");
1621 		return error;
1622 	}
1623 
1624 	/*
1625 	 * Now allocate a tag for the DMA descriptor lists and a chunk
1626 	 * of DMA-able memory based on the tag.  Also obtain the DMA
1627 	 * addresses of the RX and TX ring, which we'll need later.
1628 	 * All of our lists are allocated as a contiguous block
1629 	 * of memory.
1630 	 */
1631 	error = bus_dmamem_coherent(sc->xl_parent_tag, XL_LIST_ALIGN, 0,
1632 				    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1633 				    XL_RX_LIST_SZ, BUS_DMA_WAITOK, &dmem);
1634 	if (error) {
1635 		device_printf(dev, "failed to allocate rx list\n");
1636 		return error;
1637 	}
1638 	ld->xl_rx_tag = dmem.dmem_tag;
1639 	ld->xl_rx_dmamap = dmem.dmem_map;
1640 	ld->xl_rx_list = dmem.dmem_addr;
1641 	ld->xl_rx_dmaaddr = dmem.dmem_busaddr;
1642 
1643 	error = bus_dmamem_coherent(sc->xl_parent_tag, XL_LIST_ALIGN, 0,
1644 				    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1645 				    XL_TX_LIST_SZ, BUS_DMA_WAITOK, &dmem);
1646 	if (error) {
1647 		device_printf(dev, "failed to allocate tx list\n");
1648 		return error;
1649 	}
1650 	ld->xl_tx_tag = dmem.dmem_tag;
1651 	ld->xl_tx_dmamap = dmem.dmem_map;
1652 	ld->xl_tx_list = dmem.dmem_addr;
1653 	ld->xl_tx_dmaaddr = dmem.dmem_busaddr;
1654 
1655 	/*
1656 	 * Allocate a DMA tag for the mapping of mbufs.
1657 	 */
1658 	error = bus_dma_tag_create(sc->xl_parent_tag, 1, 0,
1659 				   BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1660 				   NULL, NULL,
1661 				   MCLBYTES, 1, MCLBYTES,
1662 				   BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW,
1663 				   &sc->xl_rx_mtag);
1664 	if (error) {
1665 		device_printf(dev, "failed to allocate RX mbuf dma tag\n");
1666 		return error;
1667 	}
1668 
1669 	/*
1670 	 * Allocate a spare DMA map for the RX ring.
1671 	 */
1672 	error = bus_dmamap_create(sc->xl_rx_mtag, BUS_DMA_WAITOK,
1673 				  &sc->xl_tmpmap);
1674 	if (error) {
1675 		device_printf(dev, "failed to create RX mbuf tmp dma map\n");
1676 		bus_dma_tag_destroy(sc->xl_rx_mtag);
1677 		sc->xl_rx_mtag = NULL;
1678 		return error;
1679 	}
1680 
1681 	for (i = 0; i < XL_RX_LIST_CNT; i++) {
1682 		error = bus_dmamap_create(sc->xl_rx_mtag, BUS_DMA_WAITOK,
1683 					  &cd->xl_rx_chain[i].xl_map);
1684 		if (error) {
1685 			device_printf(dev, "failed to create %dth "
1686 				      "rx descriptor dma map!\n", i);
1687 			return error;
1688 		}
1689 		cd->xl_rx_chain[i].xl_ptr = &ld->xl_rx_list[i];
1690 	}
1691 
1692 	error = bus_dma_tag_create(sc->xl_parent_tag, 1, 0,
1693 				   BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
1694 				   NULL, NULL,
1695 				   MCLBYTES, XL_MAXFRAGS, MCLBYTES,
1696 				   BUS_DMA_ALLOCNOW | BUS_DMA_WAITOK,
1697 				   &sc->xl_tx_mtag);
1698 	if (error) {
1699 		device_printf(dev, "failed to allocate TX mbuf dma tag\n");
1700 		return error;
1701 	}
1702 
1703 	for (i = 0; i < XL_TX_LIST_CNT; i++) {
1704 		error = bus_dmamap_create(sc->xl_tx_mtag, BUS_DMA_WAITOK,
1705 					  &cd->xl_tx_chain[i].xl_map);
1706 		if (error) {
1707 			device_printf(dev, "failed to create %dth "
1708 				      "tx descriptor dma map!\n", i);
1709 			return error;
1710 		}
1711 		cd->xl_tx_chain[i].xl_ptr = &ld->xl_tx_list[i];
1712 	}
1713 	return 0;
1714 }
1715 
1716 static void
1717 xl_dma_free(device_t dev)
1718 {
1719 	struct xl_softc *sc;
1720 	struct xl_chain_data *cd;
1721 	struct xl_list_data *ld;
1722 	int i;
1723 
1724 	sc = device_get_softc(dev);
1725 	cd = &sc->xl_cdata;
1726 	ld = &sc->xl_ldata;
1727 
1728 	for (i = 0; i < XL_RX_LIST_CNT; ++i) {
1729 		if (cd->xl_rx_chain[i].xl_ptr != NULL) {
1730 			if (cd->xl_rx_chain[i].xl_mbuf != NULL) {
1731 				bus_dmamap_unload(sc->xl_rx_mtag,
1732 						  cd->xl_rx_chain[i].xl_map);
1733 				m_freem(cd->xl_rx_chain[i].xl_mbuf);
1734 			}
1735 			bus_dmamap_destroy(sc->xl_rx_mtag,
1736 					   cd->xl_rx_chain[i].xl_map);
1737 		}
1738 	}
1739 
1740 	for (i = 0; i < XL_TX_LIST_CNT; ++i) {
1741 		if (cd->xl_tx_chain[i].xl_ptr != NULL) {
1742 			if (cd->xl_tx_chain[i].xl_mbuf != NULL) {
1743 				bus_dmamap_unload(sc->xl_tx_mtag,
1744 						  cd->xl_tx_chain[i].xl_map);
1745 				m_freem(cd->xl_tx_chain[i].xl_mbuf);
1746 			}
1747 			bus_dmamap_destroy(sc->xl_tx_mtag,
1748 					   cd->xl_tx_chain[i].xl_map);
1749 		}
1750 	}
1751 
1752 	if (ld->xl_rx_tag) {
1753 		bus_dmamap_unload(ld->xl_rx_tag, ld->xl_rx_dmamap);
1754 		bus_dmamem_free(ld->xl_rx_tag, ld->xl_rx_list,
1755 				ld->xl_rx_dmamap);
1756 		bus_dma_tag_destroy(ld->xl_rx_tag);
1757 	}
1758 
1759 	if (ld->xl_tx_tag) {
1760 		bus_dmamap_unload(ld->xl_tx_tag, ld->xl_tx_dmamap);
1761 		bus_dmamem_free(ld->xl_tx_tag, ld->xl_tx_list,
1762 				ld->xl_tx_dmamap);
1763 		bus_dma_tag_destroy(ld->xl_tx_tag);
1764 	}
1765 
1766 	if (sc->xl_rx_mtag) {
1767 		bus_dmamap_destroy(sc->xl_rx_mtag, sc->xl_tmpmap);
1768 		bus_dma_tag_destroy(sc->xl_rx_mtag);
1769 	}
1770 	if (sc->xl_tx_mtag)
1771 		bus_dma_tag_destroy(sc->xl_tx_mtag);
1772 
1773 	if (sc->xl_parent_tag)
1774 		bus_dma_tag_destroy(sc->xl_parent_tag);
1775 }
1776 
1777 /*
1778  * Initialize the transmit descriptors.
1779  */
1780 static void
1781 xl_list_tx_init(struct xl_softc *sc)
1782 {
1783 	struct xl_chain_data	*cd;
1784 	struct xl_list_data	*ld;
1785 	int			i;
1786 
1787 	cd = &sc->xl_cdata;
1788 	ld = &sc->xl_ldata;
1789 	for (i = 0; i < XL_TX_LIST_CNT; i++) {
1790 		cd->xl_tx_chain[i].xl_phys = ld->xl_tx_dmaaddr +
1791 		    i * sizeof(struct xl_list);
1792 		if (i == (XL_TX_LIST_CNT - 1))
1793 			cd->xl_tx_chain[i].xl_next = NULL;
1794 		else
1795 			cd->xl_tx_chain[i].xl_next = &cd->xl_tx_chain[i + 1];
1796 	}
1797 
1798 	cd->xl_tx_free = &cd->xl_tx_chain[0];
1799 	cd->xl_tx_tail = cd->xl_tx_head = NULL;
1800 }
1801 
1802 /*
1803  * Initialize the transmit descriptors.
1804  */
1805 static void
1806 xl_list_tx_init_90xB(struct xl_softc *sc)
1807 {
1808 	struct xl_chain_data	*cd;
1809 	struct xl_list_data	*ld;
1810 	int			i;
1811 
1812 	cd = &sc->xl_cdata;
1813 	ld = &sc->xl_ldata;
1814 	for (i = 0; i < XL_TX_LIST_CNT; i++) {
1815 		cd->xl_tx_chain[i].xl_phys = ld->xl_tx_dmaaddr +
1816 		    i * sizeof(struct xl_list);
1817 		if (i == (XL_TX_LIST_CNT - 1))
1818 			cd->xl_tx_chain[i].xl_next = &cd->xl_tx_chain[0];
1819 		else
1820 			cd->xl_tx_chain[i].xl_next = &cd->xl_tx_chain[i + 1];
1821 		if (i == 0) {
1822 			cd->xl_tx_chain[i].xl_prev =
1823 			    &cd->xl_tx_chain[XL_TX_LIST_CNT - 1];
1824 		} else {
1825 			cd->xl_tx_chain[i].xl_prev =
1826 			    &cd->xl_tx_chain[i - 1];
1827 		}
1828 	}
1829 
1830 	ld->xl_tx_list[0].xl_status = htole32(XL_TXSTAT_EMPTY);
1831 
1832 	cd->xl_tx_prod = 1;
1833 	cd->xl_tx_cons = 1;
1834 	cd->xl_tx_cnt = 0;
1835 }
1836 
1837 /*
1838  * Initialize the RX descriptors and allocate mbufs for them. Note that
1839  * we arrange the descriptors in a closed ring, so that the last descriptor
1840  * points back to the first.
1841  */
1842 static int
1843 xl_list_rx_init(struct xl_softc *sc)
1844 {
1845 	struct xl_chain_data	*cd;
1846 	struct xl_list_data	*ld;
1847 	int			error, i, next;
1848 	u_int32_t		nextptr;
1849 
1850 	cd = &sc->xl_cdata;
1851 	ld = &sc->xl_ldata;
1852 
1853 	for (i = 0; i < XL_RX_LIST_CNT; i++) {
1854 		error = xl_newbuf(sc, &cd->xl_rx_chain[i], 1);
1855 		if (error)
1856 			return(error);
1857 		if (i == (XL_RX_LIST_CNT - 1))
1858 			next = 0;
1859 		else
1860 			next = i + 1;
1861 		nextptr = ld->xl_rx_dmaaddr +
1862 		    next * sizeof(struct xl_list_onefrag);
1863 		cd->xl_rx_chain[i].xl_next = &cd->xl_rx_chain[next];
1864 		ld->xl_rx_list[i].xl_next = htole32(nextptr);
1865 	}
1866 
1867 	cd->xl_rx_head = &cd->xl_rx_chain[0];
1868 
1869 	return(0);
1870 }
1871 
1872 /*
1873  * Initialize an RX descriptor and attach an MBUF cluster.
1874  * If we fail to do so, we need to leave the old mbuf and
1875  * the old DMA map untouched so that it can be reused.
1876  */
1877 static int
1878 xl_newbuf(struct xl_softc *sc, struct xl_chain_onefrag *c, int init)
1879 {
1880 	struct mbuf		*m_new;
1881 	bus_dmamap_t		map;
1882 	int			error, nsegs;
1883 	bus_dma_segment_t	seg;
1884 
1885 	m_new = m_getcl(init ? M_WAITOK : M_NOWAIT, MT_DATA, M_PKTHDR);
1886 	if (m_new == NULL)
1887 		return(ENOBUFS);
1888 
1889 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
1890 
1891 	/* Force longword alignment for packet payload. */
1892 	m_adj(m_new, ETHER_ALIGN);
1893 
1894 	error = bus_dmamap_load_mbuf_segment(sc->xl_rx_mtag, sc->xl_tmpmap,
1895 			m_new, &seg, 1, &nsegs, BUS_DMA_NOWAIT);
1896 	if (error) {
1897 		m_freem(m_new);
1898 		if (init) {
1899 			if_printf(&sc->arpcom.ac_if,
1900 				  "can't map mbuf (error %d)\n", error);
1901 		}
1902 		return(error);
1903 	}
1904 
1905 	if (c->xl_mbuf != NULL) {
1906 		bus_dmamap_sync(sc->xl_rx_mtag, c->xl_map,
1907 				BUS_DMASYNC_POSTREAD);
1908 		bus_dmamap_unload(sc->xl_rx_mtag, c->xl_map);
1909 	}
1910 
1911 	map = c->xl_map;
1912 	c->xl_map = sc->xl_tmpmap;
1913 	sc->xl_tmpmap = map;
1914 	c->xl_mbuf = m_new;
1915 
1916 	c->xl_ptr->xl_frag.xl_len = htole32(seg.ds_len | XL_LAST_FRAG);
1917 	c->xl_ptr->xl_frag.xl_addr = htole32(seg.ds_addr);
1918 	c->xl_ptr->xl_status = 0;
1919 
1920 	return(0);
1921 }
1922 
1923 static int
1924 xl_rx_resync(struct xl_softc *sc)
1925 {
1926 	struct xl_chain_onefrag	*pos;
1927 	int			i;
1928 
1929 	pos = sc->xl_cdata.xl_rx_head;
1930 
1931 	for (i = 0; i < XL_RX_LIST_CNT; i++) {
1932 		if (pos->xl_ptr->xl_status)
1933 			break;
1934 		pos = pos->xl_next;
1935 	}
1936 
1937 	if (i == XL_RX_LIST_CNT)
1938 		return(0);
1939 
1940 	sc->xl_cdata.xl_rx_head = pos;
1941 
1942 	return(EAGAIN);
1943 }
1944 
1945 /*
1946  * A frame has been uploaded: pass the resulting mbuf chain up to
1947  * the higher level protocols.
1948  */
1949 static void
1950 xl_rxeof(struct xl_softc *sc, int count)
1951 {
1952         struct mbuf		*m;
1953         struct ifnet		*ifp;
1954 	struct xl_chain_onefrag	*cur_rx;
1955 	int			total_len = 0;
1956 	u_int32_t		rxstat;
1957 
1958 	ifp = &sc->arpcom.ac_if;
1959 again:
1960 	while((rxstat = le32toh(sc->xl_cdata.xl_rx_head->xl_ptr->xl_status))) {
1961 #ifdef IFPOLL_ENABLE
1962 		if (count >= 0 && count-- == 0)
1963 			break;
1964 #endif
1965 		cur_rx = sc->xl_cdata.xl_rx_head;
1966 		sc->xl_cdata.xl_rx_head = cur_rx->xl_next;
1967 		total_len = rxstat & XL_RXSTAT_LENMASK;
1968 
1969 		/*
1970 		 * Since we have told the chip to allow large frames,
1971 		 * we need to trap giant frame errors in software. We allow
1972 		 * a little more than the normal frame size to account for
1973 		 * frames with VLAN tags.
1974 		 */
1975 		if (total_len > XL_MAX_FRAMELEN)
1976 			rxstat |= (XL_RXSTAT_UP_ERROR|XL_RXSTAT_OVERSIZE);
1977 
1978 		/*
1979 		 * If an error occurs, update stats, clear the
1980 		 * status word and leave the mbuf cluster in place:
1981 		 * it should simply get re-used next time this descriptor
1982 	 	 * comes up in the ring.
1983 		 */
1984 		if (rxstat & XL_RXSTAT_UP_ERROR) {
1985 			IFNET_STAT_INC(ifp, ierrors, 1);
1986 			cur_rx->xl_ptr->xl_status = 0;
1987 			continue;
1988 		}
1989 
1990 		/*
1991 		 * If the error bit was not set, the upload complete
1992 		 * bit should be set which means we have a valid packet.
1993 		 * If not, something truly strange has happened.
1994 		 */
1995 		if (!(rxstat & XL_RXSTAT_UP_CMPLT)) {
1996 			if_printf(ifp,
1997 				  "bad receive status -- packet dropped\n");
1998 			IFNET_STAT_INC(ifp, ierrors, 1);
1999 			cur_rx->xl_ptr->xl_status = 0;
2000 			continue;
2001 		}
2002 
2003 		/* No errors; receive the packet. */
2004 		m = cur_rx->xl_mbuf;
2005 
2006 		/*
2007 		 * Try to conjure up a new mbuf cluster. If that
2008 		 * fails, it means we have an out of memory condition and
2009 		 * should leave the buffer in place and continue. This will
2010 		 * result in a lost packet, but there's little else we
2011 		 * can do in this situation.
2012 		 */
2013 		if (xl_newbuf(sc, cur_rx, 0)) {
2014 			IFNET_STAT_INC(ifp, ierrors, 1);
2015 			cur_rx->xl_ptr->xl_status = 0;
2016 			continue;
2017 		}
2018 
2019 		IFNET_STAT_INC(ifp, ipackets, 1);
2020 		m->m_pkthdr.rcvif = ifp;
2021 		m->m_pkthdr.len = m->m_len = total_len;
2022 
2023 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2024 			/* Do IP checksum checking. */
2025 			if (rxstat & XL_RXSTAT_IPCKOK)
2026 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2027 			if (!(rxstat & XL_RXSTAT_IPCKERR))
2028 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2029 			if ((rxstat & XL_RXSTAT_TCPCOK &&
2030 			     !(rxstat & XL_RXSTAT_TCPCKERR)) ||
2031 			    (rxstat & XL_RXSTAT_UDPCKOK &&
2032 			     !(rxstat & XL_RXSTAT_UDPCKERR))) {
2033 				m->m_pkthdr.csum_flags |=
2034 					CSUM_DATA_VALID|CSUM_PSEUDO_HDR|
2035 					CSUM_FRAG_NOT_CHECKED;
2036 				m->m_pkthdr.csum_data = 0xffff;
2037 			}
2038 		}
2039 
2040 		ifp->if_input(ifp, m, NULL, -1);
2041 	}
2042 
2043 	if (sc->xl_type != XL_TYPE_905B) {
2044 		/*
2045 		 * Handle the 'end of channel' condition. When the upload
2046 		 * engine hits the end of the RX ring, it will stall. This
2047 		 * is our cue to flush the RX ring, reload the uplist pointer
2048 		 * register and unstall the engine.
2049 		 * XXX This is actually a little goofy. With the ThunderLAN
2050 		 * chip, you get an interrupt when the receiver hits the end
2051 		 * of the receive ring, which tells you exactly when you
2052 		 * you need to reload the ring pointer. Here we have to
2053 		 * fake it. I'm mad at myself for not being clever enough
2054 		 * to avoid the use of a goto here.
2055 		 */
2056 		if (CSR_READ_4(sc, XL_UPLIST_PTR) == 0 ||
2057 		    CSR_READ_4(sc, XL_UPLIST_STATUS) & XL_PKTSTAT_UP_STALLED) {
2058 			CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_UP_STALL);
2059 			xl_wait(sc);
2060 			CSR_WRITE_4(sc, XL_UPLIST_PTR,
2061 				    sc->xl_ldata.xl_rx_dmaaddr);
2062 			sc->xl_cdata.xl_rx_head = &sc->xl_cdata.xl_rx_chain[0];
2063 			CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_UP_UNSTALL);
2064 			goto again;
2065 		}
2066 	}
2067 }
2068 
2069 /*
2070  * A frame was downloaded to the chip. It's safe for us to clean up
2071  * the list buffers.
2072  */
2073 static void
2074 xl_txeof(struct xl_softc *sc)
2075 {
2076 	struct xl_chain		*cur_tx;
2077 	struct ifnet		*ifp;
2078 
2079 	ifp = &sc->arpcom.ac_if;
2080 
2081 	/* Clear the timeout timer. */
2082 	ifp->if_timer = 0;
2083 
2084 	/*
2085 	 * Go through our tx list and free mbufs for those
2086 	 * frames that have been uploaded. Note: the 3c905B
2087 	 * sets a special bit in the status word to let us
2088 	 * know that a frame has been downloaded, but the
2089 	 * original 3c900/3c905 adapters don't do that.
2090 	 * Consequently, we have to use a different test if
2091 	 * xl_type != XL_TYPE_905B.
2092 	 */
2093 	while(sc->xl_cdata.xl_tx_head != NULL) {
2094 		cur_tx = sc->xl_cdata.xl_tx_head;
2095 
2096 		if (CSR_READ_4(sc, XL_DOWNLIST_PTR))
2097 			break;
2098 
2099 		sc->xl_cdata.xl_tx_head = cur_tx->xl_next;
2100 		bus_dmamap_unload(sc->xl_tx_mtag, cur_tx->xl_map);
2101 		m_freem(cur_tx->xl_mbuf);
2102 		cur_tx->xl_mbuf = NULL;
2103 		IFNET_STAT_INC(ifp, opackets, 1);
2104 
2105 		cur_tx->xl_next = sc->xl_cdata.xl_tx_free;
2106 		sc->xl_cdata.xl_tx_free = cur_tx;
2107 	}
2108 
2109 	if (sc->xl_cdata.xl_tx_head == NULL) {
2110 		ifq_clr_oactive(&ifp->if_snd);
2111 		sc->xl_cdata.xl_tx_tail = NULL;
2112 	} else {
2113 		if (CSR_READ_4(sc, XL_DMACTL) & XL_DMACTL_DOWN_STALLED ||
2114 			!CSR_READ_4(sc, XL_DOWNLIST_PTR)) {
2115 			CSR_WRITE_4(sc, XL_DOWNLIST_PTR,
2116 				sc->xl_cdata.xl_tx_head->xl_phys);
2117 			CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_UNSTALL);
2118 		}
2119 	}
2120 
2121 	return;
2122 }
2123 
2124 static void
2125 xl_txeof_90xB(struct xl_softc *sc)
2126 {
2127 	struct xl_chain		*cur_tx = NULL;
2128 	struct ifnet		*ifp;
2129 	int			idx;
2130 
2131 	ifp = &sc->arpcom.ac_if;
2132 
2133 	idx = sc->xl_cdata.xl_tx_cons;
2134 	while(idx != sc->xl_cdata.xl_tx_prod) {
2135 
2136 		cur_tx = &sc->xl_cdata.xl_tx_chain[idx];
2137 
2138 		if (!(le32toh(cur_tx->xl_ptr->xl_status) &
2139 		      XL_TXSTAT_DL_COMPLETE))
2140 			break;
2141 
2142 		if (cur_tx->xl_mbuf != NULL) {
2143 			bus_dmamap_unload(sc->xl_tx_mtag, cur_tx->xl_map);
2144 			m_freem(cur_tx->xl_mbuf);
2145 			cur_tx->xl_mbuf = NULL;
2146 		}
2147 
2148 		IFNET_STAT_INC(ifp, opackets, 1);
2149 
2150 		sc->xl_cdata.xl_tx_cnt--;
2151 		XL_INC(idx, XL_TX_LIST_CNT);
2152 		ifp->if_timer = 0;
2153 	}
2154 
2155 	sc->xl_cdata.xl_tx_cons = idx;
2156 
2157 	if (cur_tx != NULL)
2158 		ifq_clr_oactive(&ifp->if_snd);
2159 
2160 	return;
2161 }
2162 
2163 /*
2164  * TX 'end of channel' interrupt handler. Actually, we should
2165  * only get a 'TX complete' interrupt if there's a transmit error,
2166  * so this is really TX error handler.
2167  */
2168 static void
2169 xl_txeoc(struct xl_softc *sc)
2170 {
2171 	struct ifnet *ifp = &sc->arpcom.ac_if;
2172 	u_int8_t		txstat;
2173 
2174 	while((txstat = CSR_READ_1(sc, XL_TX_STATUS))) {
2175 		if (txstat & XL_TXSTATUS_UNDERRUN ||
2176 			txstat & XL_TXSTATUS_JABBER ||
2177 			txstat & XL_TXSTATUS_RECLAIM) {
2178 			if_printf(ifp, "transmission error: %x\n", txstat);
2179 			CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET);
2180 			xl_wait(sc);
2181 			if (sc->xl_type == XL_TYPE_905B) {
2182 				if (sc->xl_cdata.xl_tx_cnt) {
2183 					int			i;
2184 					struct xl_chain		*c;
2185 					i = sc->xl_cdata.xl_tx_cons;
2186 					c = &sc->xl_cdata.xl_tx_chain[i];
2187 					CSR_WRITE_4(sc, XL_DOWNLIST_PTR,
2188 					    c->xl_phys);
2189 					CSR_WRITE_1(sc, XL_DOWN_POLL, 64);
2190 				}
2191 			} else {
2192 				if (sc->xl_cdata.xl_tx_head != NULL)
2193 					CSR_WRITE_4(sc, XL_DOWNLIST_PTR,
2194 					    sc->xl_cdata.xl_tx_head->xl_phys);
2195 			}
2196 			/*
2197 			 * Remember to set this for the
2198 			 * first generation 3c90X chips.
2199 			 */
2200 			CSR_WRITE_1(sc, XL_TX_FREETHRESH, XL_PACKET_SIZE >> 8);
2201 			if (txstat & XL_TXSTATUS_UNDERRUN &&
2202 			    sc->xl_tx_thresh < XL_PACKET_SIZE) {
2203 				sc->xl_tx_thresh += XL_MIN_FRAMELEN;
2204 				if_printf(ifp, "tx underrun, increasing tx start"
2205 				    " threshold to %d bytes\n",
2206 				    sc->xl_tx_thresh);
2207 			}
2208 			CSR_WRITE_2(sc, XL_COMMAND,
2209 			    XL_CMD_TX_SET_START|sc->xl_tx_thresh);
2210 			if (sc->xl_type == XL_TYPE_905B) {
2211 				CSR_WRITE_2(sc, XL_COMMAND,
2212 				XL_CMD_SET_TX_RECLAIM|(XL_PACKET_SIZE >> 4));
2213 			}
2214 			CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_ENABLE);
2215 			CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_UNSTALL);
2216 		} else {
2217 			CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_ENABLE);
2218 			CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_UNSTALL);
2219 		}
2220 		/*
2221 		 * Write an arbitrary byte to the TX_STATUS register
2222 	 	 * to clear this interrupt/error and advance to the next.
2223 		 */
2224 		CSR_WRITE_1(sc, XL_TX_STATUS, 0x01);
2225 	}
2226 
2227 	return;
2228 }
2229 
2230 #ifdef IFPOLL_ENABLE
2231 
2232 static void
2233 xl_start_poll(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2234 {
2235 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2236 	xl_start_body(ifp, 0);
2237 }
2238 
2239 static void
2240 xl_npoll_compat(struct ifnet *ifp, void *arg __unused, int count)
2241 {
2242 	struct xl_softc *sc = ifp->if_softc;
2243 
2244 	ASSERT_SERIALIZED(ifp->if_serializer);
2245 
2246 	if (sc->xl_npoll.ifpc_stcount-- == 0) {
2247 		uint16_t status;
2248 
2249 		sc->xl_npoll.ifpc_stcount = sc->xl_npoll.ifpc_stfrac;
2250 
2251 		/* XXX copy & pasted from xl_intr() */
2252 		status = CSR_READ_2(sc, XL_STATUS);
2253 		if ((status & XL_INTRS) && status != 0xFFFF) {
2254 			CSR_WRITE_2(sc, XL_COMMAND,
2255 			    XL_CMD_INTR_ACK | (status & XL_INTRS));
2256 
2257 			if (status & XL_STAT_TX_COMPLETE) {
2258 				IFNET_STAT_INC(ifp, oerrors, 1);
2259 				xl_txeoc(sc);
2260 			}
2261 
2262 			if (status & XL_STAT_ADFAIL) {
2263 				xl_reset(sc);
2264 				xl_init(sc);
2265 			}
2266 
2267 			if (status & XL_STAT_STATSOFLOW) {
2268 				sc->xl_stats_no_timeout = 1;
2269 				xl_stats_update_serialized(sc);
2270 				sc->xl_stats_no_timeout = 0;
2271 			}
2272 		}
2273 	}
2274 
2275 	xl_rxeof(sc, count);
2276 	if (sc->xl_type == XL_TYPE_905B)
2277 		xl_txeof_90xB(sc);
2278 	else
2279 		xl_txeof(sc);
2280 
2281 	if (!ifq_is_empty(&ifp->if_snd))
2282 		if_devstart(ifp);
2283 }
2284 
2285 static void
2286 xl_npoll(struct ifnet *ifp, struct ifpoll_info *info)
2287 {
2288 	struct xl_softc *sc = ifp->if_softc;
2289 
2290 	ASSERT_SERIALIZED(ifp->if_serializer);
2291 
2292 	if (info != NULL) {
2293 		int cpuid = sc->xl_npoll.ifpc_cpuid;
2294 
2295 		info->ifpi_rx[cpuid].poll_func = xl_npoll_compat;
2296 		info->ifpi_rx[cpuid].arg = NULL;
2297 		info->ifpi_rx[cpuid].serializer = ifp->if_serializer;
2298 
2299 		if (ifp->if_flags & IFF_RUNNING)
2300 			xl_enable_intrs(sc, 0);
2301 		if (sc->xl_type != XL_TYPE_905B)
2302 			ifp->if_start = xl_start_poll;
2303 		ifq_set_cpuid(&ifp->if_snd, cpuid);
2304 	} else {
2305 		if (sc->xl_type != XL_TYPE_905B)
2306 			ifp->if_start = xl_start;
2307 		if (ifp->if_flags & IFF_RUNNING)
2308 			xl_enable_intrs(sc, XL_INTRS);
2309 		ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->xl_irq));
2310 	}
2311 }
2312 
2313 #endif	/* IFPOLL_ENABLE */
2314 
2315 static void
2316 xl_intr(void *arg)
2317 {
2318 	struct xl_softc		*sc;
2319 	struct ifnet		*ifp;
2320 	u_int16_t		status;
2321 
2322 	sc = arg;
2323 	ifp = &sc->arpcom.ac_if;
2324 
2325 	ASSERT_SERIALIZED(ifp->if_serializer);
2326 
2327 	while(((status = CSR_READ_2(sc, XL_STATUS)) & XL_INTRS) &&
2328 	      status != 0xFFFF) {
2329 
2330 		CSR_WRITE_2(sc, XL_COMMAND,
2331 		    XL_CMD_INTR_ACK|(status & XL_INTRS));
2332 
2333 		if (status & XL_STAT_UP_COMPLETE) {
2334 			u_long curpkts, ncurpkts;
2335 
2336 			IFNET_STAT_GET(ifp, ipackets, curpkts);
2337 			xl_rxeof(sc, -1);
2338 			IFNET_STAT_GET(ifp, ipackets, ncurpkts);
2339 
2340 			if (curpkts == ncurpkts) {
2341 				while (xl_rx_resync(sc))
2342 					xl_rxeof(sc, -1);
2343 			}
2344 		}
2345 
2346 		if (status & XL_STAT_DOWN_COMPLETE) {
2347 			if (sc->xl_type == XL_TYPE_905B)
2348 				xl_txeof_90xB(sc);
2349 			else
2350 				xl_txeof(sc);
2351 		}
2352 
2353 		if (status & XL_STAT_TX_COMPLETE) {
2354 			IFNET_STAT_INC(ifp, oerrors, 1);
2355 			xl_txeoc(sc);
2356 		}
2357 
2358 		if (status & XL_STAT_ADFAIL) {
2359 			xl_reset(sc);
2360 			xl_init(sc);
2361 		}
2362 
2363 		if (status & XL_STAT_STATSOFLOW) {
2364 			sc->xl_stats_no_timeout = 1;
2365 			xl_stats_update_serialized(sc);
2366 			sc->xl_stats_no_timeout = 0;
2367 		}
2368 	}
2369 
2370 	if (!ifq_is_empty(&ifp->if_snd))
2371 		if_devstart(ifp);
2372 }
2373 
2374 static void
2375 xl_stats_update(void *xsc)
2376 {
2377 	struct xl_softc	*sc = xsc;
2378 
2379 	lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
2380 	xl_stats_update_serialized(xsc);
2381 	lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
2382 }
2383 
2384 static void
2385 xl_stats_update_serialized(void *xsc)
2386 {
2387 	struct xl_softc		*sc;
2388 	struct ifnet		*ifp;
2389 	struct xl_stats		xl_stats;
2390 	u_int8_t		*p;
2391 	int			i;
2392 	struct mii_data		*mii = NULL;
2393 
2394 	bzero((char *)&xl_stats, sizeof(struct xl_stats));
2395 
2396 	sc = xsc;
2397 	ifp = &sc->arpcom.ac_if;
2398 	if (sc->xl_miibus != NULL)
2399 		mii = device_get_softc(sc->xl_miibus);
2400 
2401 	p = (u_int8_t *)&xl_stats;
2402 
2403 	/* Read all the stats registers. */
2404 	XL_SEL_WIN(6);
2405 
2406 	for (i = 0; i < 16; i++)
2407 		*p++ = CSR_READ_1(sc, XL_W6_CARRIER_LOST + i);
2408 
2409 	IFNET_STAT_INC(ifp, ierrors, xl_stats.xl_rx_overrun);
2410 
2411 	IFNET_STAT_INC(ifp, collisions,
2412 	    xl_stats.xl_tx_multi_collision +
2413 	    xl_stats.xl_tx_single_collision +
2414 	    xl_stats.xl_tx_late_collision);
2415 
2416 	/*
2417 	 * Boomerang and cyclone chips have an extra stats counter
2418 	 * in window 4 (BadSSD). We have to read this too in order
2419 	 * to clear out all the stats registers and avoid a statsoflow
2420 	 * interrupt.
2421 	 */
2422 	XL_SEL_WIN(4);
2423 	CSR_READ_1(sc, XL_W4_BADSSD);
2424 
2425 	if ((mii != NULL) && (!sc->xl_stats_no_timeout))
2426 		mii_tick(mii);
2427 
2428 	XL_SEL_WIN(7);
2429 
2430 	if (!sc->xl_stats_no_timeout)
2431 		callout_reset(&sc->xl_stat_timer, hz, xl_stats_update, sc);
2432 
2433 	return;
2434 }
2435 
2436 /*
2437  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
2438  * pointers to the fragment pointers.
2439  */
2440 static int
2441 xl_encap(struct xl_softc *sc, struct xl_chain *c, struct mbuf *m_head)
2442 {
2443 	int			error, nsegs, i;
2444 	u_int32_t		status;
2445 	bus_dma_segment_t	segs[XL_MAXFRAGS];
2446 	struct xl_list		*l;
2447 
2448 	error = bus_dmamap_load_mbuf_defrag(sc->xl_tx_mtag, c->xl_map, &m_head,
2449 			segs, XL_MAXFRAGS, &nsegs, BUS_DMA_NOWAIT);
2450 	if (error) {
2451 		m_freem(m_head);
2452 		return error;
2453 	}
2454 	bus_dmamap_sync(sc->xl_tx_mtag, c->xl_map, BUS_DMASYNC_PREWRITE);
2455 
2456 	if (sc->xl_type == XL_TYPE_905B) {
2457 		status = XL_TXSTAT_RND_DEFEAT;
2458 		if (m_head->m_pkthdr.csum_flags) {
2459 			if (m_head->m_pkthdr.csum_flags & CSUM_IP)
2460 				status |= XL_TXSTAT_IPCKSUM;
2461 			if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
2462 				status |= XL_TXSTAT_TCPCKSUM;
2463 			if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
2464 				status |= XL_TXSTAT_UDPCKSUM;
2465 		}
2466 	} else {
2467 		status = m_head->m_pkthdr.len;
2468 	}
2469 
2470 	l = c->xl_ptr;
2471 	for (i = 0; i < nsegs; i++) {
2472 		l->xl_frag[i].xl_addr = htole32(segs[i].ds_addr);
2473 		l->xl_frag[i].xl_len = htole32(segs[i].ds_len);
2474 	}
2475 	l->xl_frag[nsegs - 1].xl_len =
2476 		htole32(segs[nsegs - 1].ds_len | XL_LAST_FRAG);
2477 	l->xl_status = htole32(status);
2478 	l->xl_next = 0;
2479 
2480 	c->xl_mbuf = m_head;
2481 
2482 	return(0);
2483 }
2484 
2485 static void
2486 xl_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2487 {
2488 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2489 	ASSERT_SERIALIZED(ifp->if_serializer);
2490 	xl_start_body(ifp, 1);
2491 }
2492 
2493 /*
2494  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
2495  * to the mbuf data regions directly in the transmit lists. We also save a
2496  * copy of the pointers since the transmit list fragment pointers are
2497  * physical addresses.
2498  */
2499 static void
2500 xl_start_body(struct ifnet *ifp, int proc_rx)
2501 {
2502 	struct xl_softc		*sc;
2503 	struct mbuf		*m_head = NULL;
2504 	struct xl_chain		*prev = NULL, *cur_tx = NULL, *start_tx;
2505 	struct xl_chain		*prev_tx;
2506 	u_int32_t		status;
2507 	int			error;
2508 
2509 	sc = ifp->if_softc;
2510 	/*
2511 	 * Check for an available queue slot. If there are none,
2512 	 * punt.
2513 	 */
2514 	if (sc->xl_cdata.xl_tx_free == NULL) {
2515 		xl_txeoc(sc);
2516 		xl_txeof(sc);
2517 		if (sc->xl_cdata.xl_tx_free == NULL) {
2518 			ifq_set_oactive(&ifp->if_snd);
2519 			return;
2520 		}
2521 	}
2522 
2523 	start_tx = sc->xl_cdata.xl_tx_free;
2524 
2525 	while(sc->xl_cdata.xl_tx_free != NULL) {
2526 		m_head = ifq_dequeue(&ifp->if_snd);
2527 		if (m_head == NULL)
2528 			break;
2529 
2530 		/* Pick a descriptor off the free list. */
2531 		prev_tx = cur_tx;
2532 		cur_tx = sc->xl_cdata.xl_tx_free;
2533 
2534 		/* Pack the data into the descriptor. */
2535 		error = xl_encap(sc, cur_tx, m_head);
2536 		if (error) {
2537 			cur_tx = prev_tx;
2538 			continue;
2539 		}
2540 
2541 		sc->xl_cdata.xl_tx_free = cur_tx->xl_next;
2542 		cur_tx->xl_next = NULL;
2543 
2544 		/* Chain it together. */
2545 		if (prev != NULL) {
2546 			prev->xl_next = cur_tx;
2547 			prev->xl_ptr->xl_next = htole32(cur_tx->xl_phys);
2548 		}
2549 		prev = cur_tx;
2550 
2551 		BPF_MTAP(ifp, cur_tx->xl_mbuf);
2552 	}
2553 
2554 	/*
2555 	 * If there are no packets queued, bail.
2556 	 */
2557 	if (cur_tx == NULL)
2558 		return;
2559 
2560 	/*
2561 	 * Place the request for the upload interrupt
2562 	 * in the last descriptor in the chain. This way, if
2563 	 * we're chaining several packets at once, we'll only
2564 	 * get an interupt once for the whole chain rather than
2565 	 * once for each packet.
2566 	 */
2567 	cur_tx->xl_ptr->xl_status = htole32(le32toh(cur_tx->xl_ptr->xl_status) |
2568 	    XL_TXSTAT_DL_INTR);
2569 
2570 	/*
2571 	 * Queue the packets. If the TX channel is clear, update
2572 	 * the downlist pointer register.
2573 	 */
2574 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_STALL);
2575 	xl_wait(sc);
2576 
2577 	if (sc->xl_cdata.xl_tx_head != NULL) {
2578 		sc->xl_cdata.xl_tx_tail->xl_next = start_tx;
2579 		sc->xl_cdata.xl_tx_tail->xl_ptr->xl_next =
2580 		    htole32(start_tx->xl_phys);
2581 		status = sc->xl_cdata.xl_tx_tail->xl_ptr->xl_status;
2582 		sc->xl_cdata.xl_tx_tail->xl_ptr->xl_status =
2583 		    htole32(le32toh(status) & ~XL_TXSTAT_DL_INTR);
2584 		sc->xl_cdata.xl_tx_tail = cur_tx;
2585 	} else {
2586 		sc->xl_cdata.xl_tx_head = start_tx;
2587 		sc->xl_cdata.xl_tx_tail = cur_tx;
2588 	}
2589 
2590 	if (!CSR_READ_4(sc, XL_DOWNLIST_PTR))
2591 		CSR_WRITE_4(sc, XL_DOWNLIST_PTR, start_tx->xl_phys);
2592 
2593 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_UNSTALL);
2594 
2595 	XL_SEL_WIN(7);
2596 
2597 	/*
2598 	 * Set a timeout in case the chip goes out to lunch.
2599 	 */
2600 	ifp->if_timer = 5;
2601 
2602 	if (proc_rx) {
2603 		/*
2604 		 * XXX Under certain conditions, usually on slower machines
2605 		 * where interrupts may be dropped, it's possible for the
2606 		 * adapter to chew up all the buffers in the receive ring
2607 		 * and stall, without us being able to do anything about it.
2608 		 * To guard against this, we need to make a pass over the
2609 		 * RX queue to make sure there aren't any packets pending.
2610 		 * Doing it here means we can flush the receive ring at the
2611 		 * same time the chip is DMAing the transmit descriptors we
2612 		 * just gave it.
2613 		 *
2614 		 * 3Com goes to some lengths to emphasize the Parallel
2615 		 * Tasking (tm) nature of their chips in all their marketing
2616 		 * literature;  we may as well take advantage of it. :)
2617 		 */
2618 		xl_rxeof(sc, -1);
2619 	}
2620 }
2621 
2622 static void
2623 xl_start_90xB(struct ifnet *ifp, struct ifaltq_subque *ifsq)
2624 {
2625 	struct xl_softc		*sc;
2626 	struct mbuf		*m_head = NULL;
2627 	struct xl_chain		*prev = NULL, *cur_tx = NULL, *start_tx;
2628 	struct xl_chain		*prev_tx;
2629 	int			error, idx;
2630 
2631 	ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
2632 	ASSERT_SERIALIZED(ifp->if_serializer);
2633 
2634 	sc = ifp->if_softc;
2635 
2636 	if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
2637 		return;
2638 
2639 	idx = sc->xl_cdata.xl_tx_prod;
2640 	start_tx = &sc->xl_cdata.xl_tx_chain[idx];
2641 
2642 	while (sc->xl_cdata.xl_tx_chain[idx].xl_mbuf == NULL) {
2643 
2644 		if ((XL_TX_LIST_CNT - sc->xl_cdata.xl_tx_cnt) < 3) {
2645 			ifq_set_oactive(&ifp->if_snd);
2646 			break;
2647 		}
2648 
2649 		m_head = ifq_dequeue(&ifp->if_snd);
2650 		if (m_head == NULL)
2651 			break;
2652 
2653 		prev_tx = cur_tx;
2654 		cur_tx = &sc->xl_cdata.xl_tx_chain[idx];
2655 
2656 		/* Pack the data into the descriptor. */
2657 		error = xl_encap(sc, cur_tx, m_head);
2658 		if (error) {
2659 			cur_tx = prev_tx;
2660 			continue;
2661 		}
2662 
2663 		/* Chain it together. */
2664 		if (prev != NULL)
2665 			prev->xl_ptr->xl_next = htole32(cur_tx->xl_phys);
2666 		prev = cur_tx;
2667 
2668 		BPF_MTAP(ifp, cur_tx->xl_mbuf);
2669 
2670 		XL_INC(idx, XL_TX_LIST_CNT);
2671 		sc->xl_cdata.xl_tx_cnt++;
2672 	}
2673 
2674 	/*
2675 	 * If there are no packets queued, bail.
2676 	 */
2677 	if (cur_tx == NULL)
2678 		return;
2679 
2680 	/*
2681 	 * Place the request for the upload interrupt
2682 	 * in the last descriptor in the chain. This way, if
2683 	 * we're chaining several packets at once, we'll only
2684 	 * get an interupt once for the whole chain rather than
2685 	 * once for each packet.
2686 	 */
2687 	cur_tx->xl_ptr->xl_status = htole32(le32toh(cur_tx->xl_ptr->xl_status) |
2688 	    XL_TXSTAT_DL_INTR);
2689 
2690 	/* Start transmission */
2691 	sc->xl_cdata.xl_tx_prod = idx;
2692 	start_tx->xl_prev->xl_ptr->xl_next = htole32(start_tx->xl_phys);
2693 
2694 	/*
2695 	 * Set a timeout in case the chip goes out to lunch.
2696 	 */
2697 	ifp->if_timer = 5;
2698 }
2699 
2700 static void
2701 xl_init(void *xsc)
2702 {
2703 	struct xl_softc		*sc = xsc;
2704 	struct ifnet		*ifp = &sc->arpcom.ac_if;
2705 	int			error, i;
2706 	u_int16_t		rxfilt = 0;
2707 	struct mii_data		*mii = NULL;
2708 
2709 	ASSERT_SERIALIZED(ifp->if_serializer);
2710 
2711 	/*
2712 	 * Cancel pending I/O and free all RX/TX buffers.
2713 	 */
2714 	xl_stop(sc);
2715 
2716 	if (sc->xl_miibus == NULL) {
2717 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_RESET);
2718 		xl_wait(sc);
2719 	}
2720 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET);
2721 	xl_wait(sc);
2722 	DELAY(10000);
2723 
2724 	if (sc->xl_miibus != NULL)
2725 		mii = device_get_softc(sc->xl_miibus);
2726 
2727 	/* Init our MAC address */
2728 	XL_SEL_WIN(2);
2729 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
2730 		CSR_WRITE_1(sc, XL_W2_STATION_ADDR_LO + i,
2731 				sc->arpcom.ac_enaddr[i]);
2732 	}
2733 
2734 	/* Clear the station mask. */
2735 	for (i = 0; i < 3; i++)
2736 		CSR_WRITE_2(sc, XL_W2_STATION_MASK_LO + (i * 2), 0);
2737 #ifdef notdef
2738 	/* Reset TX and RX. */
2739 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_RESET);
2740 	xl_wait(sc);
2741 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET);
2742 	xl_wait(sc);
2743 #endif
2744 	/* Init circular RX list. */
2745 	error = xl_list_rx_init(sc);
2746 	if (error) {
2747 		if_printf(ifp, "initialization of the rx ring failed (%d)\n",
2748 			  error);
2749 		xl_stop(sc);
2750 		return;
2751 	}
2752 
2753 	/* Init TX descriptors. */
2754 	if (sc->xl_type == XL_TYPE_905B)
2755 		xl_list_tx_init_90xB(sc);
2756 	else
2757 		xl_list_tx_init(sc);
2758 
2759 	/*
2760 	 * Set the TX freethresh value.
2761 	 * Note that this has no effect on 3c905B "cyclone"
2762 	 * cards but is required for 3c900/3c905 "boomerang"
2763 	 * cards in order to enable the download engine.
2764 	 */
2765 	CSR_WRITE_1(sc, XL_TX_FREETHRESH, XL_PACKET_SIZE >> 8);
2766 
2767 	/* Set the TX start threshold for best performance. */
2768 	sc->xl_tx_thresh = XL_MIN_FRAMELEN;
2769 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_SET_START|sc->xl_tx_thresh);
2770 
2771 	/*
2772 	 * If this is a 3c905B, also set the tx reclaim threshold.
2773 	 * This helps cut down on the number of tx reclaim errors
2774 	 * that could happen on a busy network. The chip multiplies
2775 	 * the register value by 16 to obtain the actual threshold
2776 	 * in bytes, so we divide by 16 when setting the value here.
2777 	 * The existing threshold value can be examined by reading
2778 	 * the register at offset 9 in window 5.
2779 	 */
2780 	if (sc->xl_type == XL_TYPE_905B) {
2781 		CSR_WRITE_2(sc, XL_COMMAND,
2782 		    XL_CMD_SET_TX_RECLAIM|(XL_PACKET_SIZE >> 4));
2783 	}
2784 
2785 	/* Set RX filter bits. */
2786 	XL_SEL_WIN(5);
2787 	rxfilt = CSR_READ_1(sc, XL_W5_RX_FILTER);
2788 
2789 	/* Set the individual bit to receive frames for this host only. */
2790 	rxfilt |= XL_RXFILTER_INDIVIDUAL;
2791 
2792 	/* If we want promiscuous mode, set the allframes bit. */
2793 	if (ifp->if_flags & IFF_PROMISC) {
2794 		rxfilt |= XL_RXFILTER_ALLFRAMES;
2795 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2796 	} else {
2797 		rxfilt &= ~XL_RXFILTER_ALLFRAMES;
2798 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2799 	}
2800 
2801 	/*
2802 	 * Set capture broadcast bit to capture broadcast frames.
2803 	 */
2804 	if (ifp->if_flags & IFF_BROADCAST) {
2805 		rxfilt |= XL_RXFILTER_BROADCAST;
2806 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2807 	} else {
2808 		rxfilt &= ~XL_RXFILTER_BROADCAST;
2809 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_FILT|rxfilt);
2810 	}
2811 
2812 	/*
2813 	 * Program the multicast filter, if necessary.
2814 	 */
2815 	if (sc->xl_type == XL_TYPE_905B)
2816 		xl_setmulti_hash(sc);
2817 	else
2818 		xl_setmulti(sc);
2819 
2820 	if (sc->xl_type == XL_TYPE_905B) {
2821 		/* Set UP polling interval */
2822 		CSR_WRITE_1(sc, XL_UP_POLL, 64);
2823 	}
2824 
2825 	/*
2826 	 * Load the address of the RX list. We have to
2827 	 * stall the upload engine before we can manipulate
2828 	 * the uplist pointer register, then unstall it when
2829 	 * we're finished. We also have to wait for the
2830 	 * stall command to complete before proceeding.
2831 	 * Note that we have to do this after any RX resets
2832 	 * have completed since the uplist register is cleared
2833 	 * by a reset.
2834 	 */
2835 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_UP_STALL);
2836 	xl_wait(sc);
2837 	CSR_WRITE_4(sc, XL_UPLIST_PTR, sc->xl_ldata.xl_rx_dmaaddr);
2838 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_UP_UNSTALL);
2839 	xl_wait(sc);
2840 
2841 	if (sc->xl_type == XL_TYPE_905B) {
2842 		/* Set DN polling interval */
2843 		CSR_WRITE_1(sc, XL_DOWN_POLL, 64);
2844 
2845 		/* Load the address of the TX list */
2846 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_STALL);
2847 		xl_wait(sc);
2848 		CSR_WRITE_4(sc, XL_DOWNLIST_PTR,
2849 		    sc->xl_cdata.xl_tx_chain[0].xl_phys);
2850 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_DOWN_UNSTALL);
2851 		xl_wait(sc);
2852 	}
2853 
2854 	/*
2855 	 * If the coax transceiver is on, make sure to enable
2856 	 * the DC-DC converter.
2857  	 */
2858 	XL_SEL_WIN(3);
2859 	if (sc->xl_xcvr == XL_XCVR_COAX)
2860 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_START);
2861 	else
2862 		CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
2863 
2864 	/*
2865 	 * increase packet size to allow reception of 802.1q or ISL packets.
2866 	 * For the 3c90x chip, set the 'allow large packets' bit in the MAC
2867 	 * control register. For 3c90xB/C chips, use the RX packet size
2868 	 * register.
2869 	 */
2870 
2871 	if (sc->xl_type == XL_TYPE_905B) {
2872 		CSR_WRITE_2(sc, XL_W3_MAXPKTSIZE, XL_PACKET_SIZE);
2873 	} else {
2874 		u_int8_t macctl;
2875 		macctl = CSR_READ_1(sc, XL_W3_MAC_CTRL);
2876 		macctl |= XL_MACCTRL_ALLOW_LARGE_PACK;
2877 		CSR_WRITE_1(sc, XL_W3_MAC_CTRL, macctl);
2878 	}
2879 
2880 	/* Clear out the stats counters. */
2881 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_DISABLE);
2882 	sc->xl_stats_no_timeout = 1;
2883 	xl_stats_update_serialized(sc);
2884 	sc->xl_stats_no_timeout = 0;
2885 	XL_SEL_WIN(4);
2886 	CSR_WRITE_2(sc, XL_W4_NET_DIAG, XL_NETDIAG_UPPER_BYTES_ENABLE);
2887 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_ENABLE);
2888 
2889 	/*
2890 	 * Enable interrupts.
2891 	 */
2892 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STAT_ENB | XL_INTRS);
2893 #ifdef IFPOLL_ENABLE
2894 	/* Do not enable interrupt if polling(4) is enabled */
2895 	if (ifp->if_flags & IFF_NPOLLING)
2896 		xl_enable_intrs(sc, 0);
2897 	else
2898 #endif
2899 	xl_enable_intrs(sc, XL_INTRS);
2900 
2901 	/* Set the RX early threshold */
2902 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_SET_THRESH|(XL_PACKET_SIZE >>2));
2903 	CSR_WRITE_2(sc, XL_DMACTL, XL_DMACTL_UP_RX_EARLY);
2904 
2905 	/* Enable receiver and transmitter. */
2906 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_ENABLE);
2907 	xl_wait(sc);
2908 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_ENABLE);
2909 	xl_wait(sc);
2910 
2911 	if (mii != NULL)
2912 		mii_mediachg(mii);
2913 
2914 	/* Select window 7 for normal operations. */
2915 	XL_SEL_WIN(7);
2916 
2917 	ifp->if_flags |= IFF_RUNNING;
2918 	ifq_clr_oactive(&ifp->if_snd);
2919 
2920 	callout_reset(&sc->xl_stat_timer, hz, xl_stats_update, sc);
2921 }
2922 
2923 /*
2924  * Set media options.
2925  */
2926 static int
2927 xl_ifmedia_upd(struct ifnet *ifp)
2928 {
2929 	struct xl_softc		*sc;
2930 	struct ifmedia		*ifm = NULL;
2931 	struct mii_data		*mii = NULL;
2932 
2933 	ASSERT_SERIALIZED(ifp->if_serializer);
2934 
2935 	sc = ifp->if_softc;
2936 	if (sc->xl_miibus != NULL)
2937 		mii = device_get_softc(sc->xl_miibus);
2938 	if (mii == NULL)
2939 		ifm = &sc->ifmedia;
2940 	else
2941 		ifm = &mii->mii_media;
2942 
2943 	switch(IFM_SUBTYPE(ifm->ifm_media)) {
2944 	case IFM_100_FX:
2945 	case IFM_10_FL:
2946 	case IFM_10_2:
2947 	case IFM_10_5:
2948 		xl_setmode(sc, ifm->ifm_media);
2949 		return(0);
2950 		break;
2951 	default:
2952 		break;
2953 	}
2954 
2955 	if (sc->xl_media & XL_MEDIAOPT_MII || sc->xl_media & XL_MEDIAOPT_BTX
2956 		|| sc->xl_media & XL_MEDIAOPT_BT4) {
2957 		xl_init(sc);
2958 	} else {
2959 		xl_setmode(sc, ifm->ifm_media);
2960 	}
2961 
2962 	return(0);
2963 }
2964 
2965 /*
2966  * Report current media status.
2967  */
2968 static void
2969 xl_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
2970 {
2971 	struct xl_softc		*sc;
2972 	u_int32_t		icfg;
2973 	struct mii_data		*mii = NULL;
2974 
2975 	ASSERT_SERIALIZED(ifp->if_serializer);
2976 
2977 	sc = ifp->if_softc;
2978 	if (sc->xl_miibus != NULL)
2979 		mii = device_get_softc(sc->xl_miibus);
2980 
2981 	XL_SEL_WIN(3);
2982 	icfg = CSR_READ_4(sc, XL_W3_INTERNAL_CFG) & XL_ICFG_CONNECTOR_MASK;
2983 	icfg >>= XL_ICFG_CONNECTOR_BITS;
2984 
2985 	ifmr->ifm_active = IFM_ETHER;
2986 
2987 	switch(icfg) {
2988 	case XL_XCVR_10BT:
2989 		ifmr->ifm_active = IFM_ETHER|IFM_10_T;
2990 		if (CSR_READ_1(sc, XL_W3_MAC_CTRL) & XL_MACCTRL_DUPLEX)
2991 			ifmr->ifm_active |= IFM_FDX;
2992 		else
2993 			ifmr->ifm_active |= IFM_HDX;
2994 		break;
2995 	case XL_XCVR_AUI:
2996 		if (sc->xl_type == XL_TYPE_905B &&
2997 		    sc->xl_media == XL_MEDIAOPT_10FL) {
2998 			ifmr->ifm_active = IFM_ETHER|IFM_10_FL;
2999 			if (CSR_READ_1(sc, XL_W3_MAC_CTRL) & XL_MACCTRL_DUPLEX)
3000 				ifmr->ifm_active |= IFM_FDX;
3001 			else
3002 				ifmr->ifm_active |= IFM_HDX;
3003 		} else
3004 			ifmr->ifm_active = IFM_ETHER|IFM_10_5;
3005 		break;
3006 	case XL_XCVR_COAX:
3007 		ifmr->ifm_active = IFM_ETHER|IFM_10_2;
3008 		break;
3009 	/*
3010 	 * XXX MII and BTX/AUTO should be separate cases.
3011 	 */
3012 
3013 	case XL_XCVR_100BTX:
3014 	case XL_XCVR_AUTO:
3015 	case XL_XCVR_MII:
3016 		if (mii != NULL) {
3017 			mii_pollstat(mii);
3018 			ifmr->ifm_active = mii->mii_media_active;
3019 			ifmr->ifm_status = mii->mii_media_status;
3020 		}
3021 		break;
3022 	case XL_XCVR_100BFX:
3023 		ifmr->ifm_active = IFM_ETHER|IFM_100_FX;
3024 		break;
3025 	default:
3026 		if_printf(ifp, "unknown XCVR type: %d\n", icfg);
3027 		break;
3028 	}
3029 
3030 	return;
3031 }
3032 
3033 static int
3034 xl_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
3035 {
3036 	struct xl_softc		*sc = ifp->if_softc;
3037 	struct ifreq		*ifr = (struct ifreq *) data;
3038 	int			error = 0;
3039 	struct mii_data		*mii = NULL;
3040 	u_int8_t		rxfilt;
3041 
3042 	ASSERT_SERIALIZED(ifp->if_serializer);
3043 
3044 	switch(command) {
3045 	case SIOCSIFFLAGS:
3046 		XL_SEL_WIN(5);
3047 		rxfilt = CSR_READ_1(sc, XL_W5_RX_FILTER);
3048 		if (ifp->if_flags & IFF_UP) {
3049 			if (ifp->if_flags & IFF_RUNNING &&
3050 			    ifp->if_flags & IFF_PROMISC &&
3051 			    !(sc->xl_if_flags & IFF_PROMISC)) {
3052 				rxfilt |= XL_RXFILTER_ALLFRAMES;
3053 				CSR_WRITE_2(sc, XL_COMMAND,
3054 				    XL_CMD_RX_SET_FILT|rxfilt);
3055 				XL_SEL_WIN(7);
3056 			} else if (ifp->if_flags & IFF_RUNNING &&
3057 			    !(ifp->if_flags & IFF_PROMISC) &&
3058 			    sc->xl_if_flags & IFF_PROMISC) {
3059 				rxfilt &= ~XL_RXFILTER_ALLFRAMES;
3060 				CSR_WRITE_2(sc, XL_COMMAND,
3061 				    XL_CMD_RX_SET_FILT|rxfilt);
3062 				XL_SEL_WIN(7);
3063 			} else
3064 				xl_init(sc);
3065 		} else {
3066 			if (ifp->if_flags & IFF_RUNNING)
3067 				xl_stop(sc);
3068 		}
3069 		sc->xl_if_flags = ifp->if_flags;
3070 		error = 0;
3071 		break;
3072 	case SIOCADDMULTI:
3073 	case SIOCDELMULTI:
3074 		if (sc->xl_type == XL_TYPE_905B)
3075 			xl_setmulti_hash(sc);
3076 		else
3077 			xl_setmulti(sc);
3078 		error = 0;
3079 		break;
3080 	case SIOCGIFMEDIA:
3081 	case SIOCSIFMEDIA:
3082 		if (sc->xl_miibus != NULL)
3083 			mii = device_get_softc(sc->xl_miibus);
3084 		if (mii == NULL)
3085 			error = ifmedia_ioctl(ifp, ifr,
3086 			    &sc->ifmedia, command);
3087 		else
3088 			error = ifmedia_ioctl(ifp, ifr,
3089 			    &mii->mii_media, command);
3090 		break;
3091         case SIOCSIFCAP:
3092 		ifp->if_capenable &= ~IFCAP_HWCSUM;
3093 		ifp->if_capenable |= (ifr->ifr_reqcap & IFCAP_HWCSUM);
3094 		if (ifp->if_capenable & IFCAP_HWCSUM)
3095 			ifp->if_hwassist = XL905B_CSUM_FEATURES;
3096 		else
3097 			ifp->if_hwassist = 0;
3098 		break;
3099 	default:
3100 		error = ether_ioctl(ifp, command, data);
3101 		break;
3102 	}
3103 	return(error);
3104 }
3105 
3106 static void
3107 xl_watchdog(struct ifnet *ifp)
3108 {
3109 	struct xl_softc		*sc;
3110 	u_int16_t		status = 0;
3111 
3112 	ASSERT_SERIALIZED(ifp->if_serializer);
3113 
3114 	sc = ifp->if_softc;
3115 
3116 	IFNET_STAT_INC(ifp, oerrors, 1);
3117 	XL_SEL_WIN(4);
3118 	status = CSR_READ_2(sc, XL_W4_MEDIA_STATUS);
3119 	if_printf(ifp, "watchdog timeout\n");
3120 
3121 	if (status & XL_MEDIASTAT_CARRIER)
3122 		if_printf(ifp, "no carrier - transceiver cable problem?\n");
3123 	xl_txeoc(sc);
3124 	xl_txeof(sc);
3125 	xl_rxeof(sc, -1);
3126 	xl_reset(sc);
3127 	xl_init(sc);
3128 
3129 	if (!ifq_is_empty(&ifp->if_snd))
3130 		if_devstart(ifp);
3131 }
3132 
3133 /*
3134  * Stop the adapter and free any mbufs allocated to the
3135  * RX and TX lists.
3136  */
3137 static void
3138 xl_stop(struct xl_softc *sc)
3139 {
3140 	int		i;
3141 	struct ifnet		*ifp;
3142 
3143 	ifp = &sc->arpcom.ac_if;
3144 	ASSERT_SERIALIZED(ifp->if_serializer);
3145 
3146 	ifp->if_timer = 0;
3147 
3148 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_DISABLE);
3149 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STATS_DISABLE);
3150 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB);
3151 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_DISCARD);
3152 	xl_wait(sc);
3153 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_DISABLE);
3154 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_COAX_STOP);
3155 	DELAY(800);
3156 
3157 #ifdef foo
3158 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_RX_RESET);
3159 	xl_wait(sc);
3160 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_TX_RESET);
3161 	xl_wait(sc);
3162 #endif
3163 
3164 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ACK|XL_STAT_INTLATCH);
3165 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_STAT_ENB|0);
3166 	CSR_WRITE_2(sc, XL_COMMAND, XL_CMD_INTR_ENB|0);
3167 	if (sc->xl_flags & XL_FLAG_FUNCREG)
3168 		bus_space_write_4(sc->xl_ftag, sc->xl_fhandle, 4, 0x8000);
3169 
3170 	/* Stop the stats updater. */
3171 	callout_stop(&sc->xl_stat_timer);
3172 
3173 	/*
3174 	 * Free data in the RX lists.
3175 	 */
3176 	for (i = 0; i < XL_RX_LIST_CNT; i++) {
3177 		if (sc->xl_cdata.xl_rx_chain[i].xl_mbuf != NULL) {
3178 			bus_dmamap_unload(sc->xl_rx_mtag,
3179 			    sc->xl_cdata.xl_rx_chain[i].xl_map);
3180 			m_freem(sc->xl_cdata.xl_rx_chain[i].xl_mbuf);
3181 			sc->xl_cdata.xl_rx_chain[i].xl_mbuf = NULL;
3182 		}
3183 	}
3184 	bzero(sc->xl_ldata.xl_rx_list, XL_RX_LIST_SZ);
3185 
3186 	/*
3187 	 * Free the TX list buffers.
3188 	 */
3189 	for (i = 0; i < XL_TX_LIST_CNT; i++) {
3190 		if (sc->xl_cdata.xl_tx_chain[i].xl_mbuf != NULL) {
3191 			bus_dmamap_unload(sc->xl_tx_mtag,
3192 			    sc->xl_cdata.xl_tx_chain[i].xl_map);
3193 			m_freem(sc->xl_cdata.xl_tx_chain[i].xl_mbuf);
3194 			sc->xl_cdata.xl_tx_chain[i].xl_mbuf = NULL;
3195 		}
3196 	}
3197 	bzero(sc->xl_ldata.xl_tx_list, XL_TX_LIST_SZ);
3198 
3199 	ifp->if_flags &= ~IFF_RUNNING;
3200 	ifq_clr_oactive(&ifp->if_snd);
3201 }
3202 
3203 /*
3204  * Stop all chip I/O so that the kernel's probe routines don't
3205  * get confused by errant DMAs when rebooting.
3206  */
3207 static void
3208 xl_shutdown(device_t dev)
3209 {
3210 	struct xl_softc	*sc = device_get_softc(dev);
3211 
3212 	lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
3213 	xl_reset(sc);
3214 	xl_stop(sc);
3215 	lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
3216 }
3217 
3218 static int
3219 xl_suspend(device_t dev)
3220 {
3221 	struct xl_softc *sc = device_get_softc(dev);
3222 
3223 	lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
3224 	xl_stop(sc);
3225 	lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
3226 
3227 	return(0);
3228 }
3229 
3230 static int
3231 xl_resume(device_t dev)
3232 {
3233 	struct xl_softc		*sc;
3234 	struct ifnet		*ifp;
3235 
3236 	sc = device_get_softc(dev);
3237 	ifp = &sc->arpcom.ac_if;
3238 
3239 	lwkt_serialize_enter(ifp->if_serializer);
3240 	xl_reset(sc);
3241 	if (ifp->if_flags & IFF_UP)
3242 		xl_init(sc);
3243 	lwkt_serialize_exit(ifp->if_serializer);
3244 
3245 	return(0);
3246 }
3247