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