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