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