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