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