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