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