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