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