- Embed ether vlan tag in mbuf packet header. Add an mbuf flag to mark that
[dragonfly.git] / sys / dev / netif / stge / if_stge.c
1 /*      $NetBSD: if_stge.c,v 1.32 2005/12/11 12:22:49 christos Exp $    */
2 /*      $FreeBSD: src/sys/dev/stge/if_stge.c,v 1.2 2006/08/12 01:21:36 yongari Exp $    */
3 /*      $DragonFly: src/sys/dev/netif/stge/if_stge.c,v 1.3 2008/03/10 10:47:57 sephe Exp $      */
4
5 /*-
6  * Copyright (c) 2001 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Jason R. Thorpe.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by the NetBSD
23  *      Foundation, Inc. and its contributors.
24  * 4. Neither the name of The NetBSD Foundation nor the names of its
25  *    contributors may be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  */
40
41 /*
42  * Device driver for the Sundance Tech. TC9021 10/100/1000
43  * Ethernet controller.
44  */
45
46 #include "opt_polling.h"
47
48 #include <sys/param.h>
49 #include <sys/bus.h>
50 #include <sys/endian.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/module.h>
55 #include <sys/rman.h>
56 #include <sys/serialize.h>
57 #include <sys/socket.h>
58 #include <sys/sockio.h>
59 #include <sys/sysctl.h>
60
61 #include <net/bpf.h>
62 #include <net/ethernet.h>
63 #include <net/if.h>
64 #include <net/if_arp.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_types.h>
68 #include <net/ifq_var.h>
69 #include <net/vlan/if_vlan_var.h>
70
71 #include <dev/netif/mii_layer/mii.h>
72 #include <dev/netif/mii_layer/miivar.h>
73
74 #include <bus/pci/pcireg.h>
75 #include <bus/pci/pcivar.h>
76
77 #include "if_stgereg.h"
78 #include "if_stgevar.h"
79
80 #define STGE_CSUM_FEATURES      (CSUM_IP | CSUM_TCP | CSUM_UDP)
81
82 /* "device miibus" required.  See GENERIC if you get errors here. */
83 #include "miibus_if.h"
84
85 /*
86  * Devices supported by this driver.
87  */
88 static struct stge_product {
89         uint16_t        stge_vendorid;
90         uint16_t        stge_deviceid;
91         const char      *stge_name;
92 } stge_products[] = {
93         { VENDOR_SUNDANCETI,    DEVICEID_SUNDANCETI_ST1023,
94           "Sundance ST-1023 Gigabit Ethernet" },
95
96         { VENDOR_SUNDANCETI,    DEVICEID_SUNDANCETI_ST2021,
97           "Sundance ST-2021 Gigabit Ethernet" },
98
99         { VENDOR_TAMARACK,      DEVICEID_TAMARACK_TC9021,
100           "Tamarack TC9021 Gigabit Ethernet" },
101
102         { VENDOR_TAMARACK,      DEVICEID_TAMARACK_TC9021_ALT,
103           "Tamarack TC9021 Gigabit Ethernet" },
104
105         /*
106          * The Sundance sample boards use the Sundance vendor ID,
107          * but the Tamarack product ID.
108          */
109         { VENDOR_SUNDANCETI,    DEVICEID_TAMARACK_TC9021,
110           "Sundance TC9021 Gigabit Ethernet" },
111
112         { VENDOR_SUNDANCETI,    DEVICEID_TAMARACK_TC9021_ALT,
113           "Sundance TC9021 Gigabit Ethernet" },
114
115         { VENDOR_DLINK,         DEVICEID_DLINK_DL2000,
116           "D-Link DL-2000 Gigabit Ethernet" },
117
118         { VENDOR_ANTARES,       DEVICEID_ANTARES_TC9021,
119           "Antares Gigabit Ethernet" },
120
121         { 0, 0, NULL }
122 };
123
124 static int      stge_probe(device_t);
125 static int      stge_attach(device_t);
126 static int      stge_detach(device_t);
127 static void     stge_shutdown(device_t);
128 static int      stge_suspend(device_t);
129 static int      stge_resume(device_t);
130
131 static int      stge_encap(struct stge_softc *, struct mbuf **);
132 static void     stge_start(struct ifnet *);
133 static void     stge_watchdog(struct ifnet *);
134 static int      stge_ioctl(struct ifnet *, u_long, caddr_t, struct ucred *);
135 static void     stge_init(void *);
136 static void     stge_vlan_setup(struct stge_softc *);
137 static void     stge_stop(struct stge_softc *);
138 static void     stge_start_tx(struct stge_softc *);
139 static void     stge_start_rx(struct stge_softc *);
140 static void     stge_stop_tx(struct stge_softc *);
141 static void     stge_stop_rx(struct stge_softc *);
142
143 static void     stge_reset(struct stge_softc *, uint32_t);
144 static int      stge_eeprom_wait(struct stge_softc *);
145 static void     stge_read_eeprom(struct stge_softc *, int, uint16_t *);
146 static void     stge_tick(void *);
147 static void     stge_stats_update(struct stge_softc *);
148 static void     stge_set_filter(struct stge_softc *);
149 static void     stge_set_multi(struct stge_softc *);
150
151 static void     stge_link(struct stge_softc *);
152 static void     stge_intr(void *);
153 static __inline int stge_tx_error(struct stge_softc *);
154 static void     stge_txeof(struct stge_softc *);
155 static void     stge_rxeof(struct stge_softc *, int);
156 static __inline void stge_discard_rxbuf(struct stge_softc *, int);
157 static int      stge_newbuf(struct stge_softc *, int, int);
158 #ifndef __i386__
159 static __inline struct mbuf *stge_fixup_rx(struct stge_softc *, struct mbuf *);
160 #endif
161
162 static void     stge_mii_sync(struct stge_softc *);
163 static void     stge_mii_send(struct stge_softc *, uint32_t, int);
164 static int      stge_mii_readreg(struct stge_softc *, struct stge_mii_frame *);
165 static int      stge_mii_writereg(struct stge_softc *, struct stge_mii_frame *);
166 static int      stge_miibus_readreg(device_t, int, int);
167 static int      stge_miibus_writereg(device_t, int, int, int);
168 static void     stge_miibus_statchg(device_t);
169 static int      stge_mediachange(struct ifnet *);
170 static void     stge_mediastatus(struct ifnet *, struct ifmediareq *);
171
172 static void     stge_dmamap_cb(void *, bus_dma_segment_t *, int, int);
173 static void     stge_mbuf_dmamap_cb(void *, bus_dma_segment_t *, int,
174                                     bus_size_t, int);
175 static int      stge_dma_alloc(struct stge_softc *);
176 static void     stge_dma_free(struct stge_softc *);
177 static void     stge_dma_wait(struct stge_softc *);
178 static void     stge_init_tx_ring(struct stge_softc *);
179 static int      stge_init_rx_ring(struct stge_softc *);
180 #ifdef DEVICE_POLLING
181 static void     stge_poll(struct ifnet *, enum poll_cmd, int);
182 #endif
183
184 static int      sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
185 static int      sysctl_hw_stge_rxint_nframe(SYSCTL_HANDLER_ARGS);
186 static int      sysctl_hw_stge_rxint_dmawait(SYSCTL_HANDLER_ARGS);
187
188 static device_method_t stge_methods[] = {
189         /* Device interface */
190         DEVMETHOD(device_probe,         stge_probe),
191         DEVMETHOD(device_attach,        stge_attach),
192         DEVMETHOD(device_detach,        stge_detach),
193         DEVMETHOD(device_shutdown,      stge_shutdown),
194         DEVMETHOD(device_suspend,       stge_suspend),
195         DEVMETHOD(device_resume,        stge_resume),
196
197         /* MII interface */
198         DEVMETHOD(miibus_readreg,       stge_miibus_readreg),
199         DEVMETHOD(miibus_writereg,      stge_miibus_writereg),
200         DEVMETHOD(miibus_statchg,       stge_miibus_statchg),
201
202         { 0, 0 }
203
204 };
205
206 static driver_t stge_driver = {
207         "stge",
208         stge_methods,
209         sizeof(struct stge_softc)
210 };
211
212 static devclass_t stge_devclass;
213
214 DECLARE_DUMMY_MODULE(if_stge);
215 MODULE_DEPEND(if_stge, miibus, 1, 1, 1);
216 DRIVER_MODULE(if_stge, pci, stge_driver, stge_devclass, 0, 0);
217 DRIVER_MODULE(miibus, stge, miibus_driver, miibus_devclass, 0, 0);
218
219 #define MII_SET(x)      \
220         CSR_WRITE_1(sc, STGE_PhyCtrl, CSR_READ_1(sc, STGE_PhyCtrl) | (x))
221 #define MII_CLR(x)      \
222         CSR_WRITE_1(sc, STGE_PhyCtrl, CSR_READ_1(sc, STGE_PhyCtrl) & ~(x))
223
224 /*
225  * Sync the PHYs by setting data bit and strobing the clock 32 times.
226  */
227 static void
228 stge_mii_sync(struct stge_softc *sc)
229 {
230         int i;
231
232         MII_SET(PC_MgmtDir | PC_MgmtData);
233
234         for (i = 0; i < 32; i++) {
235                 MII_SET(PC_MgmtClk);
236                 DELAY(1);
237                 MII_CLR(PC_MgmtClk);
238                 DELAY(1);
239         }
240 }
241
242 /*
243  * Clock a series of bits through the MII.
244  */
245 static void
246 stge_mii_send(struct stge_softc *sc, uint32_t bits, int cnt)
247 {
248         int i;
249
250         MII_CLR(PC_MgmtClk);
251
252         for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
253                 if (bits & i)
254                         MII_SET(PC_MgmtData);
255                 else
256                         MII_CLR(PC_MgmtData);
257                 DELAY(1);
258                 MII_CLR(PC_MgmtClk);
259                 DELAY(1);
260                 MII_SET(PC_MgmtClk);
261         }
262 }
263
264 /*
265  * Read an PHY register through the MII.
266  */
267 static int
268 stge_mii_readreg(struct stge_softc *sc, struct stge_mii_frame *frame)
269 {
270         int i, ack;
271
272         /*
273          * Set up frame for RX.
274          */
275         frame->mii_stdelim = STGE_MII_STARTDELIM;
276         frame->mii_opcode = STGE_MII_READOP;
277         frame->mii_turnaround = 0;
278         frame->mii_data = 0;
279
280         CSR_WRITE_1(sc, STGE_PhyCtrl, 0 | sc->sc_PhyCtrl);
281         /*
282          * Turn on data xmit.
283          */
284         MII_SET(PC_MgmtDir);
285
286         stge_mii_sync(sc);
287
288         /*
289          * Send command/address info.
290          */
291         stge_mii_send(sc, frame->mii_stdelim, 2);
292         stge_mii_send(sc, frame->mii_opcode, 2);
293         stge_mii_send(sc, frame->mii_phyaddr, 5);
294         stge_mii_send(sc, frame->mii_regaddr, 5);
295
296         /* Turn off xmit. */
297         MII_CLR(PC_MgmtDir);
298
299         /* Idle bit */
300         MII_CLR((PC_MgmtClk | PC_MgmtData));
301         DELAY(1);
302         MII_SET(PC_MgmtClk);
303         DELAY(1);
304
305         /* Check for ack */
306         MII_CLR(PC_MgmtClk);
307         DELAY(1);
308         ack = CSR_READ_1(sc, STGE_PhyCtrl) & PC_MgmtData;
309         MII_SET(PC_MgmtClk);
310         DELAY(1);
311
312         /*
313          * Now try reading data bits. If the ack failed, we still
314          * need to clock through 16 cycles to keep the PHY(s) in sync.
315          */
316         if (ack) {
317                 for(i = 0; i < 16; i++) {
318                         MII_CLR(PC_MgmtClk);
319                         DELAY(1);
320                         MII_SET(PC_MgmtClk);
321                         DELAY(1);
322                 }
323                 goto fail;
324         }
325
326         for (i = 0x8000; i; i >>= 1) {
327                 MII_CLR(PC_MgmtClk);
328                 DELAY(1);
329                 if (!ack) {
330                         if (CSR_READ_1(sc, STGE_PhyCtrl) & PC_MgmtData)
331                                 frame->mii_data |= i;
332                         DELAY(1);
333                 }
334                 MII_SET(PC_MgmtClk);
335                 DELAY(1);
336         }
337
338 fail:
339         MII_CLR(PC_MgmtClk);
340         DELAY(1);
341         MII_SET(PC_MgmtClk);
342         DELAY(1);
343
344         if (ack)
345                 return(1);
346         return(0);
347 }
348
349 /*
350  * Write to a PHY register through the MII.
351  */
352 static int
353 stge_mii_writereg(struct stge_softc *sc, struct stge_mii_frame *frame)
354 {
355
356         /*
357          * Set up frame for TX.
358          */
359         frame->mii_stdelim = STGE_MII_STARTDELIM;
360         frame->mii_opcode = STGE_MII_WRITEOP;
361         frame->mii_turnaround = STGE_MII_TURNAROUND;
362
363         /*
364          * Turn on data output.
365          */
366         MII_SET(PC_MgmtDir);
367
368         stge_mii_sync(sc);
369
370         stge_mii_send(sc, frame->mii_stdelim, 2);
371         stge_mii_send(sc, frame->mii_opcode, 2);
372         stge_mii_send(sc, frame->mii_phyaddr, 5);
373         stge_mii_send(sc, frame->mii_regaddr, 5);
374         stge_mii_send(sc, frame->mii_turnaround, 2);
375         stge_mii_send(sc, frame->mii_data, 16);
376
377         /* Idle bit. */
378         MII_SET(PC_MgmtClk);
379         DELAY(1);
380         MII_CLR(PC_MgmtClk);
381         DELAY(1);
382
383         /*
384          * Turn off xmit.
385          */
386         MII_CLR(PC_MgmtDir);
387
388         return(0);
389 }
390
391 /*
392  * sc_miibus_readreg:   [mii interface function]
393  *
394  *      Read a PHY register on the MII of the TC9021.
395  */
396 static int
397 stge_miibus_readreg(device_t dev, int phy, int reg)
398 {
399         struct stge_softc *sc;
400         struct stge_mii_frame frame;
401         int error;
402
403         sc = device_get_softc(dev);
404
405         if (reg == STGE_PhyCtrl) {
406                 /* XXX allow ip1000phy read STGE_PhyCtrl register. */
407                 error = CSR_READ_1(sc, STGE_PhyCtrl);
408                 return (error);
409         }
410         bzero(&frame, sizeof(frame));
411         frame.mii_phyaddr = phy;
412         frame.mii_regaddr = reg;
413
414         error = stge_mii_readreg(sc, &frame);
415
416         if (error != 0) {
417                 /* Don't show errors for PHY probe request */
418                 if (reg != 1)
419                         device_printf(sc->sc_dev, "phy read fail\n");
420                 return (0);
421         }
422         return (frame.mii_data);
423 }
424
425 /*
426  * stge_miibus_writereg:        [mii interface function]
427  *
428  *      Write a PHY register on the MII of the TC9021.
429  */
430 static int
431 stge_miibus_writereg(device_t dev, int phy, int reg, int val)
432 {
433         struct stge_softc *sc;
434         struct stge_mii_frame frame;
435         int error;
436
437         sc = device_get_softc(dev);
438
439         bzero(&frame, sizeof(frame));
440         frame.mii_phyaddr = phy;
441         frame.mii_regaddr = reg;
442         frame.mii_data = val;
443
444         error = stge_mii_writereg(sc, &frame);
445
446         if (error != 0)
447                 device_printf(sc->sc_dev, "phy write fail\n");
448         return (0);
449 }
450
451 /*
452  * stge_miibus_statchg: [mii interface function]
453  *
454  *      Callback from MII layer when media changes.
455  */
456 static void
457 stge_miibus_statchg(device_t dev)
458 {
459         struct stge_softc *sc;
460         struct mii_data *mii;
461
462         sc = device_get_softc(dev);
463         mii = device_get_softc(sc->sc_miibus);
464
465         if (IFM_SUBTYPE(mii->mii_media_active) == IFM_NONE)
466                 return;
467
468         sc->sc_MACCtrl = 0;
469         if (((mii->mii_media_active & IFM_GMASK) & IFM_FDX) != 0)
470                 sc->sc_MACCtrl |= MC_DuplexSelect;
471         if (((mii->mii_media_active & IFM_GMASK) & IFM_FLAG0) != 0)
472                 sc->sc_MACCtrl |= MC_RxFlowControlEnable;
473         if (((mii->mii_media_active & IFM_GMASK) & IFM_FLAG1) != 0)
474                 sc->sc_MACCtrl |= MC_TxFlowControlEnable;
475
476         stge_link(sc);
477 }
478
479 /*
480  * stge_mediastatus:    [ifmedia interface function]
481  *
482  *      Get the current interface media status.
483  */
484 static void
485 stge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
486 {
487         struct stge_softc *sc;
488         struct mii_data *mii;
489
490         sc = ifp->if_softc;
491         mii = device_get_softc(sc->sc_miibus);
492
493         mii_pollstat(mii);
494         ifmr->ifm_status = mii->mii_media_status;
495         ifmr->ifm_active = mii->mii_media_active;
496 }
497
498 /*
499  * stge_mediachange:    [ifmedia interface function]
500  *
501  *      Set hardware to newly-selected media.
502  */
503 static int
504 stge_mediachange(struct ifnet *ifp)
505 {
506         struct stge_softc *sc;
507         struct mii_data *mii;
508
509         sc = ifp->if_softc;
510         mii = device_get_softc(sc->sc_miibus);
511         mii_mediachg(mii);
512
513         return (0);
514 }
515
516 static int
517 stge_eeprom_wait(struct stge_softc *sc)
518 {
519         int i;
520
521         for (i = 0; i < STGE_TIMEOUT; i++) {
522                 DELAY(1000);
523                 if ((CSR_READ_2(sc, STGE_EepromCtrl) & EC_EepromBusy) == 0)
524                         return (0);
525         }
526         return (1);
527 }
528
529 /*
530  * stge_read_eeprom:
531  *
532  *      Read data from the serial EEPROM.
533  */
534 static void
535 stge_read_eeprom(struct stge_softc *sc, int offset, uint16_t *data)
536 {
537
538         if (stge_eeprom_wait(sc))
539                 device_printf(sc->sc_dev, "EEPROM failed to come ready\n");
540
541         CSR_WRITE_2(sc, STGE_EepromCtrl,
542             EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_RR));
543         if (stge_eeprom_wait(sc))
544                 device_printf(sc->sc_dev, "EEPROM read timed out\n");
545         *data = CSR_READ_2(sc, STGE_EepromData);
546 }
547
548
549 static int
550 stge_probe(device_t dev)
551 {
552         struct stge_product *sp;
553         uint16_t vendor, devid;
554
555         vendor = pci_get_vendor(dev);
556         devid = pci_get_device(dev);
557         
558         for (sp = stge_products; sp->stge_name != NULL; sp++) {
559                 if (vendor == sp->stge_vendorid &&
560                     devid == sp->stge_deviceid) {
561                         device_set_desc(dev, sp->stge_name);
562                         return (0);
563                 }
564         }
565
566         return (ENXIO);
567 }
568
569 static int
570 stge_attach(device_t dev)
571 {
572         struct stge_softc *sc;
573         struct ifnet *ifp;
574         uint8_t enaddr[ETHER_ADDR_LEN];
575         int error, i;
576         uint16_t cmd;
577         uint32_t val;
578
579         error = 0;
580         sc = device_get_softc(dev);
581         sc->sc_dev = dev;
582         ifp = &sc->arpcom.ac_if;
583
584         if_initname(ifp, device_get_name(dev), device_get_unit(dev));
585
586         callout_init(&sc->sc_tick_ch);
587
588 #ifndef BURN_BRIDGES
589         /*
590          * Handle power management nonsense.
591          */
592         if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
593                 uint32_t iobase, membase, irq;
594
595                 /* Save important PCI config data. */
596                 iobase = pci_read_config(dev, STGE_PCIR_LOIO, 4);
597                 membase = pci_read_config(dev, STGE_PCIR_LOMEM, 4);
598                 irq = pci_read_config(dev, PCIR_INTLINE, 4);
599
600                 /* Reset the power state. */
601                 device_printf(dev, "chip is in D%d power mode "
602                               "-- setting to D0\n", pci_get_powerstate(dev));
603
604                 pci_set_powerstate(dev, PCI_POWERSTATE_D0);
605
606                 /* Restore PCI config data. */
607                 pci_write_config(dev, STGE_PCIR_LOIO, iobase, 4);
608                 pci_write_config(dev, STGE_PCIR_LOMEM, membase, 4);
609                 pci_write_config(dev, PCIR_INTLINE, irq, 4);
610         }
611 #endif
612
613         /*
614          * Map the device.
615          */
616         pci_enable_busmaster(dev);
617         cmd = pci_read_config(dev, PCIR_COMMAND, 2);
618         val = pci_read_config(dev, STGE_PCIR_LOMEM, 4);
619
620         if ((val & 0x01) != 0) {
621                 sc->sc_res_rid = STGE_PCIR_LOMEM;
622                 sc->sc_res_type = SYS_RES_MEMORY;
623         } else {
624                 sc->sc_res_rid = STGE_PCIR_LOIO;
625                 sc->sc_res_type = SYS_RES_IOPORT;
626
627                 val = pci_read_config(dev, sc->sc_res_rid, 4);
628                 if ((val & 0x01) == 0) {
629                         device_printf(dev, "couldn't locate IO BAR\n");
630                         return ENXIO;
631                 }
632         }
633
634         sc->sc_res = bus_alloc_resource_any(dev, sc->sc_res_type,
635                                             &sc->sc_res_rid, RF_ACTIVE);
636         if (sc->sc_res == NULL) {
637                 device_printf(dev, "couldn't allocate resource\n");
638                 return ENXIO;
639         }
640         sc->sc_btag = rman_get_bustag(sc->sc_res);
641         sc->sc_bhandle = rman_get_bushandle(sc->sc_res);
642
643         sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ,
644                                             &sc->sc_irq_rid,
645                                             RF_ACTIVE | RF_SHAREABLE);
646         if (sc->sc_irq == NULL) {
647                 device_printf(dev, "couldn't allocate IRQ\n");
648                 error = ENXIO;
649                 goto fail;
650         }
651
652         sc->sc_rev = pci_get_revid(dev);
653
654         sc->sc_rxint_nframe = STGE_RXINT_NFRAME_DEFAULT;
655         sc->sc_rxint_dmawait = STGE_RXINT_DMAWAIT_DEFAULT;
656
657         sysctl_ctx_init(&sc->sc_sysctl_ctx);
658         sc->sc_sysctl_tree = SYSCTL_ADD_NODE(&sc->sc_sysctl_ctx,
659                                              SYSCTL_STATIC_CHILDREN(_hw),
660                                              OID_AUTO,
661                                              device_get_nameunit(dev),
662                                              CTLFLAG_RD, 0, "");
663         if (sc->sc_sysctl_tree == NULL) {
664                 device_printf(dev, "can't add sysctl node\n");
665                 error = ENXIO;
666                 goto fail;
667         }
668
669         SYSCTL_ADD_PROC(&sc->sc_sysctl_ctx,
670             SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
671             "rxint_nframe", CTLTYPE_INT|CTLFLAG_RW, &sc->sc_rxint_nframe, 0,
672             sysctl_hw_stge_rxint_nframe, "I", "stge rx interrupt nframe");
673
674         SYSCTL_ADD_PROC(&sc->sc_sysctl_ctx,
675             SYSCTL_CHILDREN(sc->sc_sysctl_tree), OID_AUTO,
676             "rxint_dmawait", CTLTYPE_INT|CTLFLAG_RW, &sc->sc_rxint_dmawait, 0,
677             sysctl_hw_stge_rxint_dmawait, "I", "stge rx interrupt dmawait");
678
679         if ((error = stge_dma_alloc(sc) != 0))
680                 goto fail;
681
682         /*
683          * Determine if we're copper or fiber.  It affects how we
684          * reset the card.
685          */
686         if (CSR_READ_4(sc, STGE_AsicCtrl) & AC_PhyMedia)
687                 sc->sc_usefiber = 1;
688         else
689                 sc->sc_usefiber = 0;
690
691         /* Load LED configuration from EEPROM. */
692         stge_read_eeprom(sc, STGE_EEPROM_LEDMode, &sc->sc_led);
693
694         /*
695          * Reset the chip to a known state.
696          */
697         stge_reset(sc, STGE_RESET_FULL);
698
699         /*
700          * Reading the station address from the EEPROM doesn't seem
701          * to work, at least on my sample boards.  Instead, since
702          * the reset sequence does AutoInit, read it from the station
703          * address registers. For Sundance 1023 you can only read it
704          * from EEPROM.
705          */
706         if (pci_get_device(dev) != DEVICEID_SUNDANCETI_ST1023) {
707                 uint16_t v;
708
709                 v = CSR_READ_2(sc, STGE_StationAddress0);
710                 enaddr[0] = v & 0xff;
711                 enaddr[1] = v >> 8;
712                 v = CSR_READ_2(sc, STGE_StationAddress1);
713                 enaddr[2] = v & 0xff;
714                 enaddr[3] = v >> 8;
715                 v = CSR_READ_2(sc, STGE_StationAddress2);
716                 enaddr[4] = v & 0xff;
717                 enaddr[5] = v >> 8;
718                 sc->sc_stge1023 = 0;
719         } else {
720                 uint16_t myaddr[ETHER_ADDR_LEN / 2];
721                 for (i = 0; i <ETHER_ADDR_LEN / 2; i++) {
722                         stge_read_eeprom(sc, STGE_EEPROM_StationAddress0 + i,
723                             &myaddr[i]);
724                         myaddr[i] = le16toh(myaddr[i]);
725                 }
726                 bcopy(myaddr, enaddr, sizeof(enaddr));
727                 sc->sc_stge1023 = 1;
728         }
729
730         ifp->if_softc = sc;
731         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
732         ifp->if_ioctl = stge_ioctl;
733         ifp->if_start = stge_start;
734         ifp->if_watchdog = stge_watchdog;
735         ifp->if_init = stge_init;
736 #ifdef DEVICE_POLLING
737         ifp->if_poll = stge_poll;
738 #endif
739         ifp->if_mtu = ETHERMTU;
740         ifq_set_maxlen(&ifp->if_snd, STGE_TX_RING_CNT - 1);
741         ifq_set_ready(&ifp->if_snd);
742         /* Revision B3 and earlier chips have checksum bug. */
743         if (sc->sc_rev >= 0x0c) {
744                 ifp->if_hwassist = STGE_CSUM_FEATURES;
745                 ifp->if_capabilities = IFCAP_HWCSUM;
746         } else {
747                 ifp->if_hwassist = 0;
748                 ifp->if_capabilities = 0;
749         }
750         ifp->if_capenable = ifp->if_capabilities;
751
752         /*
753          * Read some important bits from the PhyCtrl register.
754          */
755         sc->sc_PhyCtrl = CSR_READ_1(sc, STGE_PhyCtrl) &
756             (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
757
758         /* Set up MII bus. */
759         if ((error = mii_phy_probe(sc->sc_dev, &sc->sc_miibus, stge_mediachange,
760             stge_mediastatus)) != 0) {
761                 device_printf(sc->sc_dev, "no PHY found!\n");
762                 goto fail;
763         }
764
765         ether_ifattach(ifp, enaddr, NULL);
766
767         /* VLAN capability setup */
768         ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
769 #ifdef notyet
770         if (sc->sc_rev >= 0x0c)
771                 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
772 #endif
773         ifp->if_capenable = ifp->if_capabilities;
774
775         /*
776          * Tell the upper layer(s) we support long frames.
777          * Must appear after the call to ether_ifattach() because
778          * ether_ifattach() sets ifi_hdrlen to the default value.
779          */
780         ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
781
782         /*
783          * The manual recommends disabling early transmit, so we
784          * do.  It's disabled anyway, if using IP checksumming,
785          * since the entire packet must be in the FIFO in order
786          * for the chip to perform the checksum.
787          */
788         sc->sc_txthresh = 0x0fff;
789
790         /*
791          * Disable MWI if the PCI layer tells us to.
792          */
793         sc->sc_DMACtrl = 0;
794         if ((cmd & PCIM_CMD_MWRICEN) == 0)
795                 sc->sc_DMACtrl |= DMAC_MWIDisable;
796
797         /*
798          * Hookup IRQ
799          */
800         error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE, stge_intr, sc,
801                                &sc->sc_ih, ifp->if_serializer);
802         if (error != 0) {
803                 ether_ifdetach(ifp);
804                 device_printf(sc->sc_dev, "couldn't set up IRQ\n");
805                 goto fail;
806         }
807
808 fail:
809         if (error != 0)
810                 stge_detach(dev);
811
812         return (error);
813 }
814
815 static int
816 stge_detach(device_t dev)
817 {
818         struct stge_softc *sc = device_get_softc(dev);
819         struct ifnet *ifp = &sc->arpcom.ac_if;
820
821         if (device_is_attached(dev)) {
822                 lwkt_serialize_enter(ifp->if_serializer);
823                 /* XXX */
824                 sc->sc_detach = 1;
825                 stge_stop(sc);
826                 bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
827                 lwkt_serialize_exit(ifp->if_serializer);
828
829                 ether_ifdetach(ifp);
830         }
831
832         if (sc->sc_sysctl_tree != NULL)
833                 sysctl_ctx_free(&sc->sc_sysctl_ctx);
834
835         if (sc->sc_miibus != NULL)
836                 device_delete_child(dev, sc->sc_miibus);
837         bus_generic_detach(dev);
838
839         stge_dma_free(sc);
840
841         if (sc->sc_irq != NULL) {
842                 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irq_rid,
843                                      sc->sc_irq);
844         }
845         if (sc->sc_res != NULL) {
846                 bus_release_resource(dev, sc->sc_res_type, sc->sc_res_rid,
847                                      sc->sc_res);
848         }
849
850         return (0);
851 }
852
853 struct stge_dmamap_arg {
854         bus_addr_t      stge_busaddr;
855 };
856
857 static void
858 stge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
859 {
860         struct stge_dmamap_arg *ctx;
861
862         if (error != 0)
863                 return;
864
865         KASSERT(nseg == 1, ("too many segments %d\n", nseg));
866
867         ctx = (struct stge_dmamap_arg *)arg;
868         ctx->stge_busaddr = segs[0].ds_addr;
869 }
870
871 struct stge_mbuf_dmamap_arg {
872         int                     nsegs;
873         bus_dma_segment_t       *segs;
874 };
875
876 static void
877 stge_mbuf_dmamap_cb(void *xarg, bus_dma_segment_t *segs, int nsegs,
878                     bus_size_t mapsz __unused, int error)
879 {
880         struct stge_mbuf_dmamap_arg *arg = xarg;
881         int i;
882
883         if (error) {
884                 arg->nsegs = 0;
885                 return;
886         }
887
888         KASSERT(nsegs <= arg->nsegs,
889                 ("too many segments(%d), should be <= %d\n",
890                  nsegs, arg->nsegs));
891
892         arg->nsegs = nsegs;
893         for (i = 0; i < nsegs; ++i)
894                 arg->segs[i] = segs[i];
895 }
896
897 static int
898 stge_dma_alloc(struct stge_softc *sc)
899 {
900         struct stge_dmamap_arg ctx;
901         struct stge_txdesc *txd;
902         struct stge_rxdesc *rxd;
903         int error, i;
904
905         /* create parent tag. */
906         error = bus_dma_tag_create(NULL,        /* parent */
907                     1, 0,                       /* algnmnt, boundary */
908                     STGE_DMA_MAXADDR,           /* lowaddr */
909                     BUS_SPACE_MAXADDR,          /* highaddr */
910                     NULL, NULL,                 /* filter, filterarg */
911                     BUS_SPACE_MAXSIZE_32BIT,    /* maxsize */
912                     0,                          /* nsegments */
913                     BUS_SPACE_MAXSIZE_32BIT,    /* maxsegsize */
914                     0,                          /* flags */
915                     &sc->sc_cdata.stge_parent_tag);
916         if (error != 0) {
917                 device_printf(sc->sc_dev, "failed to create parent DMA tag\n");
918                 goto fail;
919         }
920         /* create tag for Tx ring. */
921         error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
922                     STGE_RING_ALIGN, 0,         /* algnmnt, boundary */
923                     BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
924                     BUS_SPACE_MAXADDR,          /* highaddr */
925                     NULL, NULL,                 /* filter, filterarg */
926                     STGE_TX_RING_SZ,            /* maxsize */
927                     1,                          /* nsegments */
928                     STGE_TX_RING_SZ,            /* maxsegsize */
929                     0,                          /* flags */
930                     &sc->sc_cdata.stge_tx_ring_tag);
931         if (error != 0) {
932                 device_printf(sc->sc_dev,
933                     "failed to allocate Tx ring DMA tag\n");
934                 goto fail;
935         }
936
937         /* create tag for Rx ring. */
938         error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
939                     STGE_RING_ALIGN, 0,         /* algnmnt, boundary */
940                     BUS_SPACE_MAXADDR_32BIT,    /* lowaddr */
941                     BUS_SPACE_MAXADDR,          /* highaddr */
942                     NULL, NULL,                 /* filter, filterarg */
943                     STGE_RX_RING_SZ,            /* maxsize */
944                     1,                          /* nsegments */
945                     STGE_RX_RING_SZ,            /* maxsegsize */
946                     0,                          /* flags */
947                     &sc->sc_cdata.stge_rx_ring_tag);
948         if (error != 0) {
949                 device_printf(sc->sc_dev,
950                     "failed to allocate Rx ring DMA tag\n");
951                 goto fail;
952         }
953
954         /* create tag for Tx buffers. */
955         error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
956                     1, 0,                       /* algnmnt, boundary */
957                     BUS_SPACE_MAXADDR,          /* lowaddr */
958                     BUS_SPACE_MAXADDR,          /* highaddr */
959                     NULL, NULL,                 /* filter, filterarg */
960                     MCLBYTES * STGE_MAXTXSEGS,  /* maxsize */
961                     STGE_MAXTXSEGS,             /* nsegments */
962                     MCLBYTES,                   /* maxsegsize */
963                     0,                          /* flags */
964                     &sc->sc_cdata.stge_tx_tag);
965         if (error != 0) {
966                 device_printf(sc->sc_dev, "failed to allocate Tx DMA tag\n");
967                 goto fail;
968         }
969
970         /* create tag for Rx buffers. */
971         error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
972                     1, 0,                       /* algnmnt, boundary */
973                     BUS_SPACE_MAXADDR,          /* lowaddr */
974                     BUS_SPACE_MAXADDR,          /* highaddr */
975                     NULL, NULL,                 /* filter, filterarg */
976                     MCLBYTES,                   /* maxsize */
977                     1,                          /* nsegments */
978                     MCLBYTES,                   /* maxsegsize */
979                     0,                          /* flags */
980                     &sc->sc_cdata.stge_rx_tag);
981         if (error != 0) {
982                 device_printf(sc->sc_dev, "failed to allocate Rx DMA tag\n");
983                 goto fail;
984         }
985
986         /* allocate DMA'able memory and load the DMA map for Tx ring. */
987         error = bus_dmamem_alloc(sc->sc_cdata.stge_tx_ring_tag,
988             (void **)&sc->sc_rdata.stge_tx_ring, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
989             &sc->sc_cdata.stge_tx_ring_map);
990         if (error != 0) {
991                 device_printf(sc->sc_dev,
992                     "failed to allocate DMA'able memory for Tx ring\n");
993                 goto fail;
994         }
995
996         ctx.stge_busaddr = 0;
997         error = bus_dmamap_load(sc->sc_cdata.stge_tx_ring_tag,
998             sc->sc_cdata.stge_tx_ring_map, sc->sc_rdata.stge_tx_ring,
999             STGE_TX_RING_SZ, stge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
1000         if (error != 0 || ctx.stge_busaddr == 0) {
1001                 device_printf(sc->sc_dev,
1002                     "failed to load DMA'able memory for Tx ring\n");
1003                 goto fail;
1004         }
1005         sc->sc_rdata.stge_tx_ring_paddr = ctx.stge_busaddr;
1006
1007         /* allocate DMA'able memory and load the DMA map for Rx ring. */
1008         error = bus_dmamem_alloc(sc->sc_cdata.stge_rx_ring_tag,
1009             (void **)&sc->sc_rdata.stge_rx_ring, BUS_DMA_NOWAIT | BUS_DMA_ZERO,
1010             &sc->sc_cdata.stge_rx_ring_map);
1011         if (error != 0) {
1012                 device_printf(sc->sc_dev,
1013                     "failed to allocate DMA'able memory for Rx ring\n");
1014                 goto fail;
1015         }
1016
1017         ctx.stge_busaddr = 0;
1018         error = bus_dmamap_load(sc->sc_cdata.stge_rx_ring_tag,
1019             sc->sc_cdata.stge_rx_ring_map, sc->sc_rdata.stge_rx_ring,
1020             STGE_RX_RING_SZ, stge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
1021         if (error != 0 || ctx.stge_busaddr == 0) {
1022                 device_printf(sc->sc_dev,
1023                     "failed to load DMA'able memory for Rx ring\n");
1024                 goto fail;
1025         }
1026         sc->sc_rdata.stge_rx_ring_paddr = ctx.stge_busaddr;
1027
1028         /* create DMA maps for Tx buffers. */
1029         for (i = 0; i < STGE_TX_RING_CNT; i++) {
1030                 txd = &sc->sc_cdata.stge_txdesc[i];
1031                 txd->tx_m = NULL;
1032                 txd->tx_dmamap = 0;
1033                 error = bus_dmamap_create(sc->sc_cdata.stge_tx_tag, 0,
1034                     &txd->tx_dmamap);
1035                 if (error != 0) {
1036                         device_printf(sc->sc_dev,
1037                             "failed to create Tx dmamap\n");
1038                         goto fail;
1039                 }
1040         }
1041         /* create DMA maps for Rx buffers. */
1042         if ((error = bus_dmamap_create(sc->sc_cdata.stge_rx_tag, 0,
1043             &sc->sc_cdata.stge_rx_sparemap)) != 0) {
1044                 device_printf(sc->sc_dev, "failed to create spare Rx dmamap\n");
1045                 goto fail;
1046         }
1047         for (i = 0; i < STGE_RX_RING_CNT; i++) {
1048                 rxd = &sc->sc_cdata.stge_rxdesc[i];
1049                 rxd->rx_m = NULL;
1050                 rxd->rx_dmamap = 0;
1051                 error = bus_dmamap_create(sc->sc_cdata.stge_rx_tag, 0,
1052                     &rxd->rx_dmamap);
1053                 if (error != 0) {
1054                         device_printf(sc->sc_dev,
1055                             "failed to create Rx dmamap\n");
1056                         goto fail;
1057                 }
1058         }
1059
1060 fail:
1061         return (error);
1062 }
1063
1064 static void
1065 stge_dma_free(struct stge_softc *sc)
1066 {
1067         struct stge_txdesc *txd;
1068         struct stge_rxdesc *rxd;
1069         int i;
1070
1071         /* Tx ring */
1072         if (sc->sc_cdata.stge_tx_ring_tag) {
1073                 if (sc->sc_cdata.stge_tx_ring_map)
1074                         bus_dmamap_unload(sc->sc_cdata.stge_tx_ring_tag,
1075                             sc->sc_cdata.stge_tx_ring_map);
1076                 if (sc->sc_cdata.stge_tx_ring_map &&
1077                     sc->sc_rdata.stge_tx_ring)
1078                         bus_dmamem_free(sc->sc_cdata.stge_tx_ring_tag,
1079                             sc->sc_rdata.stge_tx_ring,
1080                             sc->sc_cdata.stge_tx_ring_map);
1081                 sc->sc_rdata.stge_tx_ring = NULL;
1082                 sc->sc_cdata.stge_tx_ring_map = 0;
1083                 bus_dma_tag_destroy(sc->sc_cdata.stge_tx_ring_tag);
1084                 sc->sc_cdata.stge_tx_ring_tag = NULL;
1085         }
1086         /* Rx ring */
1087         if (sc->sc_cdata.stge_rx_ring_tag) {
1088                 if (sc->sc_cdata.stge_rx_ring_map)
1089                         bus_dmamap_unload(sc->sc_cdata.stge_rx_ring_tag,
1090                             sc->sc_cdata.stge_rx_ring_map);
1091                 if (sc->sc_cdata.stge_rx_ring_map &&
1092                     sc->sc_rdata.stge_rx_ring)
1093                         bus_dmamem_free(sc->sc_cdata.stge_rx_ring_tag,
1094                             sc->sc_rdata.stge_rx_ring,
1095                             sc->sc_cdata.stge_rx_ring_map);
1096                 sc->sc_rdata.stge_rx_ring = NULL;
1097                 sc->sc_cdata.stge_rx_ring_map = 0;
1098                 bus_dma_tag_destroy(sc->sc_cdata.stge_rx_ring_tag);
1099                 sc->sc_cdata.stge_rx_ring_tag = NULL;
1100         }
1101         /* Tx buffers */
1102         if (sc->sc_cdata.stge_tx_tag) {
1103                 for (i = 0; i < STGE_TX_RING_CNT; i++) {
1104                         txd = &sc->sc_cdata.stge_txdesc[i];
1105                         if (txd->tx_dmamap) {
1106                                 bus_dmamap_destroy(sc->sc_cdata.stge_tx_tag,
1107                                     txd->tx_dmamap);
1108                                 txd->tx_dmamap = 0;
1109                         }
1110                 }
1111                 bus_dma_tag_destroy(sc->sc_cdata.stge_tx_tag);
1112                 sc->sc_cdata.stge_tx_tag = NULL;
1113         }
1114         /* Rx buffers */
1115         if (sc->sc_cdata.stge_rx_tag) {
1116                 for (i = 0; i < STGE_RX_RING_CNT; i++) {
1117                         rxd = &sc->sc_cdata.stge_rxdesc[i];
1118                         if (rxd->rx_dmamap) {
1119                                 bus_dmamap_destroy(sc->sc_cdata.stge_rx_tag,
1120                                     rxd->rx_dmamap);
1121                                 rxd->rx_dmamap = 0;
1122                         }
1123                 }
1124                 if (sc->sc_cdata.stge_rx_sparemap) {
1125                         bus_dmamap_destroy(sc->sc_cdata.stge_rx_tag,
1126                             sc->sc_cdata.stge_rx_sparemap);
1127                         sc->sc_cdata.stge_rx_sparemap = 0;
1128                 }
1129                 bus_dma_tag_destroy(sc->sc_cdata.stge_rx_tag);
1130                 sc->sc_cdata.stge_rx_tag = NULL;
1131         }
1132
1133         if (sc->sc_cdata.stge_parent_tag) {
1134                 bus_dma_tag_destroy(sc->sc_cdata.stge_parent_tag);
1135                 sc->sc_cdata.stge_parent_tag = NULL;
1136         }
1137 }
1138
1139 /*
1140  * stge_shutdown:
1141  *
1142  *      Make sure the interface is stopped at reboot time.
1143  */
1144 static void
1145 stge_shutdown(device_t dev)
1146 {
1147         struct stge_softc *sc = device_get_softc(dev);
1148         struct ifnet *ifp = &sc->arpcom.ac_if;
1149
1150         lwkt_serialize_enter(ifp->if_serializer);
1151         stge_stop(sc);
1152         lwkt_serialize_exit(ifp->if_serializer);
1153 }
1154
1155 static int
1156 stge_suspend(device_t dev)
1157 {
1158         struct stge_softc *sc = device_get_softc(dev);
1159         struct ifnet *ifp = &sc->arpcom.ac_if;
1160
1161         lwkt_serialize_enter(ifp->if_serializer);
1162         stge_stop(sc);
1163         sc->sc_suspended = 1;
1164         lwkt_serialize_exit(ifp->if_serializer);
1165
1166         return (0);
1167 }
1168
1169 static int
1170 stge_resume(device_t dev)
1171 {
1172         struct stge_softc *sc = device_get_softc(dev);
1173         struct ifnet *ifp = &sc->arpcom.ac_if;
1174
1175         lwkt_serialize_enter(ifp->if_serializer);
1176         if (ifp->if_flags & IFF_UP)
1177                 stge_init(sc);
1178         sc->sc_suspended = 0;
1179         lwkt_serialize_exit(ifp->if_serializer);
1180
1181         return (0);
1182 }
1183
1184 static void
1185 stge_dma_wait(struct stge_softc *sc)
1186 {
1187         int i;
1188
1189         for (i = 0; i < STGE_TIMEOUT; i++) {
1190                 DELAY(2);
1191                 if ((CSR_READ_4(sc, STGE_DMACtrl) & DMAC_TxDMAInProg) == 0)
1192                         break;
1193         }
1194
1195         if (i == STGE_TIMEOUT)
1196                 device_printf(sc->sc_dev, "DMA wait timed out\n");
1197 }
1198
1199 static int
1200 stge_encap(struct stge_softc *sc, struct mbuf **m_head)
1201 {
1202         struct stge_txdesc *txd;
1203         struct stge_tfd *tfd;
1204         struct mbuf *m;
1205         struct stge_mbuf_dmamap_arg arg;
1206         bus_dma_segment_t txsegs[STGE_MAXTXSEGS];
1207         int error, i, si;
1208         uint64_t csum_flags, tfc;
1209
1210         if ((txd = STAILQ_FIRST(&sc->sc_cdata.stge_txfreeq)) == NULL)
1211                 return (ENOBUFS);
1212
1213         arg.nsegs = STGE_MAXTXSEGS;
1214         arg.segs = txsegs;
1215         error =  bus_dmamap_load_mbuf(sc->sc_cdata.stge_tx_tag,
1216                                       txd->tx_dmamap, *m_head,
1217                                       stge_mbuf_dmamap_cb, &arg,
1218                                       BUS_DMA_NOWAIT);
1219         if (error == EFBIG) {
1220                 m = m_defrag(*m_head, MB_DONTWAIT);
1221                 if (m == NULL) {
1222                         m_freem(*m_head);
1223                         *m_head = NULL;
1224                         return (ENOMEM);
1225                 }
1226                 *m_head = m;
1227                 error =  bus_dmamap_load_mbuf(sc->sc_cdata.stge_tx_tag,
1228                                               txd->tx_dmamap, *m_head,
1229                                               stge_mbuf_dmamap_cb, &arg,
1230                                               BUS_DMA_NOWAIT);
1231                 if (error != 0) {
1232                         m_freem(*m_head);
1233                         *m_head = NULL;
1234                         return (error);
1235                 }
1236         } else if (error != 0)
1237                 return (error);
1238         if (arg.nsegs == 0) {
1239                 m_freem(*m_head);
1240                 *m_head = NULL;
1241                 return (EIO);
1242         }
1243
1244         m = *m_head;
1245         csum_flags = 0;
1246         if ((m->m_pkthdr.csum_flags & STGE_CSUM_FEATURES) != 0) {
1247                 if (m->m_pkthdr.csum_flags & CSUM_IP)
1248                         csum_flags |= TFD_IPChecksumEnable;
1249                 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1250                         csum_flags |= TFD_TCPChecksumEnable;
1251                 else if (m->m_pkthdr.csum_flags & CSUM_UDP)
1252                         csum_flags |= TFD_UDPChecksumEnable;
1253         }
1254
1255         si = sc->sc_cdata.stge_tx_prod;
1256         tfd = &sc->sc_rdata.stge_tx_ring[si];
1257         for (i = 0; i < arg.nsegs; i++) {
1258                 tfd->tfd_frags[i].frag_word0 =
1259                     htole64(FRAG_ADDR(txsegs[i].ds_addr) |
1260                     FRAG_LEN(txsegs[i].ds_len));
1261         }
1262         sc->sc_cdata.stge_tx_cnt++;
1263
1264         tfc = TFD_FrameId(si) | TFD_WordAlign(TFD_WordAlign_disable) |
1265             TFD_FragCount(arg.nsegs) | csum_flags;
1266         if (sc->sc_cdata.stge_tx_cnt >= STGE_TX_HIWAT)
1267                 tfc |= TFD_TxDMAIndicate;
1268
1269         /* Update producer index. */
1270         sc->sc_cdata.stge_tx_prod = (si + 1) % STGE_TX_RING_CNT;
1271
1272         /* Check if we have a VLAN tag to insert. */
1273         if (m->m_flags & M_VLANTAG)
1274                 tfc |= TFD_VLANTagInsert | TFD_VID(m->m_pkthdr.ether_vlantag);
1275         tfd->tfd_control = htole64(tfc);
1276
1277         /* Update Tx Queue. */
1278         STAILQ_REMOVE_HEAD(&sc->sc_cdata.stge_txfreeq, tx_q);
1279         STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txbusyq, txd, tx_q);
1280         txd->tx_m = m;
1281
1282         /* Sync descriptors. */
1283         bus_dmamap_sync(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap,
1284             BUS_DMASYNC_PREWRITE);
1285         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1286             sc->sc_cdata.stge_tx_ring_map, BUS_DMASYNC_PREWRITE);
1287
1288         return (0);
1289 }
1290
1291 /*
1292  * stge_start:          [ifnet interface function]
1293  *
1294  *      Start packet transmission on the interface.
1295  */
1296 static void
1297 stge_start(struct ifnet *ifp)
1298 {
1299         struct stge_softc *sc;
1300         struct mbuf *m_head;
1301         int enq;
1302
1303         sc = ifp->if_softc;
1304
1305         ASSERT_SERIALIZED(ifp->if_serializer);
1306
1307         if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) !=
1308             IFF_RUNNING)
1309                 return;
1310
1311         for (enq = 0; !ifq_is_empty(&ifp->if_snd); ) {
1312                 if (sc->sc_cdata.stge_tx_cnt >= STGE_TX_HIWAT) {
1313                         ifp->if_flags |= IFF_OACTIVE;
1314                         break;
1315                 }
1316
1317                 m_head = ifq_dequeue(&ifp->if_snd, NULL);
1318                 if (m_head == NULL)
1319                         break;
1320                 /*
1321                  * Pack the data into the transmit ring. If we
1322                  * don't have room, set the OACTIVE flag and wait
1323                  * for the NIC to drain the ring.
1324                  */
1325                 if (stge_encap(sc, &m_head)) {
1326                         if (m_head == NULL)
1327                                 break;
1328                         ifp->if_flags |= IFF_OACTIVE;
1329                         break;
1330                 }
1331
1332                 enq++;
1333                 /*
1334                  * If there's a BPF listener, bounce a copy of this frame
1335                  * to him.
1336                  */
1337                 BPF_MTAP(ifp, m_head);
1338         }
1339
1340         if (enq > 0) {
1341                 /* Transmit */
1342                 CSR_WRITE_4(sc, STGE_DMACtrl, DMAC_TxDMAPollNow);
1343
1344                 /* Set a timeout in case the chip goes out to lunch. */
1345                 ifp->if_timer = 5;
1346         }
1347 }
1348
1349 /*
1350  * stge_watchdog:       [ifnet interface function]
1351  *
1352  *      Watchdog timer handler.
1353  */
1354 static void
1355 stge_watchdog(struct ifnet *ifp)
1356 {
1357         ASSERT_SERIALIZED(ifp->if_serializer);
1358
1359         if_printf(ifp, "device timeout\n");
1360         ifp->if_oerrors++;
1361         stge_init(ifp->if_softc);
1362 }
1363
1364 /*
1365  * stge_ioctl:          [ifnet interface function]
1366  *
1367  *      Handle control requests from the operator.
1368  */
1369 static int
1370 stge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1371 {
1372         struct stge_softc *sc;
1373         struct ifreq *ifr;
1374         struct mii_data *mii;
1375         int error, mask;
1376
1377         ASSERT_SERIALIZED(ifp->if_serializer);
1378
1379         sc = ifp->if_softc;
1380         ifr = (struct ifreq *)data;
1381         error = 0;
1382         switch (cmd) {
1383         case SIOCSIFMTU:
1384                 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > STGE_JUMBO_MTU)
1385                         error = EINVAL;
1386                 else if (ifp->if_mtu != ifr->ifr_mtu) {
1387                         ifp->if_mtu = ifr->ifr_mtu;
1388                         stge_init(sc);
1389                 }
1390                 break;
1391         case SIOCSIFFLAGS:
1392                 if ((ifp->if_flags & IFF_UP) != 0) {
1393                         if ((ifp->if_flags & IFF_RUNNING) != 0) {
1394                                 if (((ifp->if_flags ^ sc->sc_if_flags)
1395                                     & IFF_PROMISC) != 0)
1396                                         stge_set_filter(sc);
1397                         } else {
1398                                 if (sc->sc_detach == 0)
1399                                         stge_init(sc);
1400                         }
1401                 } else {
1402                         if ((ifp->if_flags & IFF_RUNNING) != 0)
1403                                 stge_stop(sc);
1404                 }
1405                 sc->sc_if_flags = ifp->if_flags;
1406                 break;
1407         case SIOCADDMULTI:
1408         case SIOCDELMULTI:
1409                 if ((ifp->if_flags & IFF_RUNNING) != 0)
1410                         stge_set_multi(sc);
1411                 break;
1412         case SIOCSIFMEDIA:
1413         case SIOCGIFMEDIA:
1414                 mii = device_get_softc(sc->sc_miibus);
1415                 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1416                 break;
1417         case SIOCSIFCAP:
1418                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1419                 if ((mask & IFCAP_HWCSUM) != 0) {
1420                         ifp->if_capenable ^= IFCAP_HWCSUM;
1421                         if ((IFCAP_HWCSUM & ifp->if_capenable) != 0 &&
1422                             (IFCAP_HWCSUM & ifp->if_capabilities) != 0)
1423                                 ifp->if_hwassist = STGE_CSUM_FEATURES;
1424                         else
1425                                 ifp->if_hwassist = 0;
1426                 }
1427                 if ((mask & IFCAP_VLAN_HWTAGGING) != 0) {
1428                         ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1429                         if (ifp->if_flags & IFF_RUNNING)
1430                                 stge_vlan_setup(sc);
1431                 }
1432 #if 0
1433                 VLAN_CAPABILITIES(ifp);
1434 #endif
1435                 break;
1436         default:
1437                 error = ether_ioctl(ifp, cmd, data);
1438                 break;
1439         }
1440
1441         return (error);
1442 }
1443
1444 static void
1445 stge_link(struct stge_softc *sc)
1446 {
1447         uint32_t v, ac;
1448         int i;
1449
1450         /*
1451          * Update STGE_MACCtrl register depending on link status.
1452          * (duplex, flow control etc)
1453          */
1454         v = ac = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
1455         v &= ~(MC_DuplexSelect|MC_RxFlowControlEnable|MC_TxFlowControlEnable);
1456         v |= sc->sc_MACCtrl;
1457         CSR_WRITE_4(sc, STGE_MACCtrl, v);
1458         if (((ac ^ sc->sc_MACCtrl) & MC_DuplexSelect) != 0) {
1459                 /* Duplex setting changed, reset Tx/Rx functions. */
1460                 ac = CSR_READ_4(sc, STGE_AsicCtrl);
1461                 ac |= AC_TxReset | AC_RxReset;
1462                 CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1463                 for (i = 0; i < STGE_TIMEOUT; i++) {
1464                         DELAY(100);
1465                         if ((CSR_READ_4(sc, STGE_AsicCtrl) & AC_ResetBusy) == 0)
1466                                 break;
1467                 }
1468                 if (i == STGE_TIMEOUT)
1469                         device_printf(sc->sc_dev, "reset failed to complete\n");
1470         }
1471 }
1472
1473 static __inline int
1474 stge_tx_error(struct stge_softc *sc)
1475 {
1476         uint32_t txstat;
1477         int error;
1478
1479         for (error = 0;;) {
1480                 txstat = CSR_READ_4(sc, STGE_TxStatus);
1481                 if ((txstat & TS_TxComplete) == 0)
1482                         break;
1483                 /* Tx underrun */
1484                 if ((txstat & TS_TxUnderrun) != 0) {
1485                         /*
1486                          * XXX
1487                          * There should be a more better way to recover
1488                          * from Tx underrun instead of a full reset.
1489                          */
1490                         if (sc->sc_nerr++ < STGE_MAXERR)
1491                                 device_printf(sc->sc_dev, "Tx underrun, "
1492                                     "resetting...\n");
1493                         if (sc->sc_nerr == STGE_MAXERR)
1494                                 device_printf(sc->sc_dev, "too many errors; "
1495                                     "not reporting any more\n");
1496                         error = -1;
1497                         break;
1498                 }
1499                 /* Maximum/Late collisions, Re-enable Tx MAC. */
1500                 if ((txstat & (TS_MaxCollisions|TS_LateCollision)) != 0)
1501                         CSR_WRITE_4(sc, STGE_MACCtrl,
1502                             (CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK) |
1503                             MC_TxEnable);
1504         }
1505
1506         return (error);
1507 }
1508
1509 /*
1510  * stge_intr:
1511  *
1512  *      Interrupt service routine.
1513  */
1514 static void
1515 stge_intr(void *arg)
1516 {
1517         struct stge_softc *sc = arg;
1518         struct ifnet *ifp = &sc->arpcom.ac_if;
1519         int reinit;
1520         uint16_t status;
1521
1522         ASSERT_SERIALIZED(ifp->if_serializer);
1523
1524         status = CSR_READ_2(sc, STGE_IntStatus);
1525         if (sc->sc_suspended || (status & IS_InterruptStatus) == 0)
1526                 return;
1527
1528         /* Disable interrupts. */
1529         for (reinit = 0;;) {
1530                 status = CSR_READ_2(sc, STGE_IntStatusAck);
1531                 status &= sc->sc_IntEnable;
1532                 if (status == 0)
1533                         break;
1534                 /* Host interface errors. */
1535                 if ((status & IS_HostError) != 0) {
1536                         device_printf(sc->sc_dev,
1537                             "Host interface error, resetting...\n");
1538                         reinit = 1;
1539                         goto force_init;
1540                 }
1541
1542                 /* Receive interrupts. */
1543                 if ((status & IS_RxDMAComplete) != 0) {
1544                         stge_rxeof(sc, -1);
1545                         if ((status & IS_RFDListEnd) != 0)
1546                                 CSR_WRITE_4(sc, STGE_DMACtrl,
1547                                     DMAC_RxDMAPollNow);
1548                 }
1549
1550                 /* Transmit interrupts. */
1551                 if ((status & (IS_TxDMAComplete | IS_TxComplete)) != 0)
1552                         stge_txeof(sc);
1553
1554                 /* Transmission errors.*/
1555                 if ((status & IS_TxComplete) != 0) {
1556                         if ((reinit = stge_tx_error(sc)) != 0)
1557                                 break;
1558                 }
1559         }
1560
1561 force_init:
1562         if (reinit != 0)
1563                 stge_init(sc);
1564
1565         /* Re-enable interrupts. */
1566         CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
1567
1568         /* Try to get more packets going. */
1569         if (!ifq_is_empty(&ifp->if_snd))
1570                 ifp->if_start(ifp);
1571 }
1572
1573 /*
1574  * stge_txeof:
1575  *
1576  *      Helper; handle transmit interrupts.
1577  */
1578 static void
1579 stge_txeof(struct stge_softc *sc)
1580 {
1581         struct ifnet *ifp = &sc->arpcom.ac_if;
1582         struct stge_txdesc *txd;
1583         uint64_t control;
1584         int cons;
1585
1586         txd = STAILQ_FIRST(&sc->sc_cdata.stge_txbusyq);
1587         if (txd == NULL)
1588                 return;
1589         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1590             sc->sc_cdata.stge_tx_ring_map, BUS_DMASYNC_POSTREAD);
1591
1592         /*
1593          * Go through our Tx list and free mbufs for those
1594          * frames which have been transmitted.
1595          */
1596         for (cons = sc->sc_cdata.stge_tx_cons;;
1597             cons = (cons + 1) % STGE_TX_RING_CNT) {
1598                 if (sc->sc_cdata.stge_tx_cnt <= 0)
1599                         break;
1600                 control = le64toh(sc->sc_rdata.stge_tx_ring[cons].tfd_control);
1601                 if ((control & TFD_TFDDone) == 0)
1602                         break;
1603                 sc->sc_cdata.stge_tx_cnt--;
1604                 ifp->if_flags &= ~IFF_OACTIVE;
1605
1606                 bus_dmamap_sync(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap,
1607                     BUS_DMASYNC_POSTWRITE);
1608                 bus_dmamap_unload(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap);
1609
1610                 /* Output counter is updated with statistics register */
1611                 m_freem(txd->tx_m);
1612                 txd->tx_m = NULL;
1613                 STAILQ_REMOVE_HEAD(&sc->sc_cdata.stge_txbusyq, tx_q);
1614                 STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txfreeq, txd, tx_q);
1615                 txd = STAILQ_FIRST(&sc->sc_cdata.stge_txbusyq);
1616         }
1617         sc->sc_cdata.stge_tx_cons = cons;
1618         if (sc->sc_cdata.stge_tx_cnt == 0)
1619                 ifp->if_timer = 0;
1620
1621         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1622             sc->sc_cdata.stge_tx_ring_map,
1623             BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1624 }
1625
1626 static __inline void
1627 stge_discard_rxbuf(struct stge_softc *sc, int idx)
1628 {
1629         struct stge_rfd *rfd;
1630
1631         rfd = &sc->sc_rdata.stge_rx_ring[idx];
1632         rfd->rfd_status = 0;
1633 }
1634
1635 #ifndef __i386__
1636 /*
1637  * It seems that TC9021's DMA engine has alignment restrictions in
1638  * DMA scatter operations. The first DMA segment has no address
1639  * alignment restrictins but the rest should be aligned on 4(?) bytes
1640  * boundary. Otherwise it would corrupt random memory. Since we don't
1641  * know which one is used for the first segment in advance we simply
1642  * don't align at all.
1643  * To avoid copying over an entire frame to align, we allocate a new
1644  * mbuf and copy ethernet header to the new mbuf. The new mbuf is
1645  * prepended into the existing mbuf chain.
1646  */
1647 static __inline struct mbuf *
1648 stge_fixup_rx(struct stge_softc *sc, struct mbuf *m)
1649 {
1650         struct mbuf *n;
1651
1652         n = NULL;
1653         if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
1654                 bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
1655                 m->m_data += ETHER_HDR_LEN;
1656                 n = m;
1657         } else {
1658                 MGETHDR(n, MB_DONTWAIT, MT_DATA);
1659                 if (n != NULL) {
1660                         bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
1661                         m->m_data += ETHER_HDR_LEN;
1662                         m->m_len -= ETHER_HDR_LEN;
1663                         n->m_len = ETHER_HDR_LEN;
1664                         M_MOVE_PKTHDR(n, m);
1665                         n->m_next = m;
1666                 } else
1667                         m_freem(m);
1668         }
1669
1670         return (n);
1671 }
1672 #endif
1673
1674 /*
1675  * stge_rxeof:
1676  *
1677  *      Helper; handle receive interrupts.
1678  */
1679 static void
1680 stge_rxeof(struct stge_softc *sc, int count)
1681 {
1682         struct ifnet *ifp = &sc->arpcom.ac_if;
1683         struct stge_rxdesc *rxd;
1684         struct mbuf *mp, *m;
1685         uint64_t status64;
1686         uint32_t status;
1687         int cons, prog;
1688
1689         bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
1690             sc->sc_cdata.stge_rx_ring_map, BUS_DMASYNC_POSTREAD);
1691
1692         prog = 0;
1693         for (cons = sc->sc_cdata.stge_rx_cons; prog < STGE_RX_RING_CNT;
1694             prog++, cons = (cons + 1) % STGE_RX_RING_CNT) {
1695 #ifdef DEVICE_POLLING
1696                 if (count >= 0 && count-- == 0)
1697                         break;
1698 #endif
1699
1700                 status64 = le64toh(sc->sc_rdata.stge_rx_ring[cons].rfd_status);
1701                 status = RFD_RxStatus(status64);
1702                 if ((status & RFD_RFDDone) == 0)
1703                         break;
1704
1705                 prog++;
1706                 rxd = &sc->sc_cdata.stge_rxdesc[cons];
1707                 mp = rxd->rx_m;
1708
1709                 /*
1710                  * If the packet had an error, drop it.  Note we count
1711                  * the error later in the periodic stats update.
1712                  */
1713                 if ((status & RFD_FrameEnd) != 0 && (status &
1714                     (RFD_RxFIFOOverrun | RFD_RxRuntFrame |
1715                     RFD_RxAlignmentError | RFD_RxFCSError |
1716                     RFD_RxLengthError)) != 0) {
1717                         stge_discard_rxbuf(sc, cons);
1718                         if (sc->sc_cdata.stge_rxhead != NULL) {
1719                                 m_freem(sc->sc_cdata.stge_rxhead);
1720                                 STGE_RXCHAIN_RESET(sc);
1721                         }
1722                         continue;
1723                 }
1724                 /*
1725                  * Add a new receive buffer to the ring.
1726                  */
1727                 if (stge_newbuf(sc, cons, 0) != 0) {
1728                         ifp->if_iqdrops++;
1729                         stge_discard_rxbuf(sc, cons);
1730                         if (sc->sc_cdata.stge_rxhead != NULL) {
1731                                 m_freem(sc->sc_cdata.stge_rxhead);
1732                                 STGE_RXCHAIN_RESET(sc);
1733                         }
1734                         continue;
1735                 }
1736
1737                 if ((status & RFD_FrameEnd) != 0)
1738                         mp->m_len = RFD_RxDMAFrameLen(status) -
1739                             sc->sc_cdata.stge_rxlen;
1740                 sc->sc_cdata.stge_rxlen += mp->m_len;
1741
1742                 /* Chain mbufs. */
1743                 if (sc->sc_cdata.stge_rxhead == NULL) {
1744                         sc->sc_cdata.stge_rxhead = mp;
1745                         sc->sc_cdata.stge_rxtail = mp;
1746                 } else {
1747                         mp->m_flags &= ~M_PKTHDR;
1748                         sc->sc_cdata.stge_rxtail->m_next = mp;
1749                         sc->sc_cdata.stge_rxtail = mp;
1750                 }
1751
1752                 if ((status & RFD_FrameEnd) != 0) {
1753                         m = sc->sc_cdata.stge_rxhead;
1754                         m->m_pkthdr.rcvif = ifp;
1755                         m->m_pkthdr.len = sc->sc_cdata.stge_rxlen;
1756
1757                         if (m->m_pkthdr.len > sc->sc_if_framesize) {
1758                                 m_freem(m);
1759                                 STGE_RXCHAIN_RESET(sc);
1760                                 continue;
1761                         }
1762                         /*
1763                          * Set the incoming checksum information for
1764                          * the packet.
1765                          */
1766                         if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1767                                 if ((status & RFD_IPDetected) != 0) {
1768                                         m->m_pkthdr.csum_flags |=
1769                                                 CSUM_IP_CHECKED;
1770                                         if ((status & RFD_IPError) == 0)
1771                                                 m->m_pkthdr.csum_flags |=
1772                                                     CSUM_IP_VALID;
1773                                 }
1774                                 if (((status & RFD_TCPDetected) != 0 &&
1775                                     (status & RFD_TCPError) == 0) ||
1776                                     ((status & RFD_UDPDetected) != 0 &&
1777                                     (status & RFD_UDPError) == 0)) {
1778                                         m->m_pkthdr.csum_flags |=
1779                                             (CSUM_DATA_VALID |
1780                                              CSUM_PSEUDO_HDR |
1781                                              CSUM_FRAG_NOT_CHECKED);
1782                                         m->m_pkthdr.csum_data = 0xffff;
1783                                 }
1784                         }
1785
1786 #ifndef __i386__
1787                         if (sc->sc_if_framesize > (MCLBYTES - ETHER_ALIGN)) {
1788                                 if ((m = stge_fixup_rx(sc, m)) == NULL) {
1789                                         STGE_RXCHAIN_RESET(sc);
1790                                         continue;
1791                                 }
1792                         }
1793 #endif
1794
1795                         /* Check for VLAN tagged packets. */
1796                         if ((status & RFD_VLANDetected) != 0 &&
1797                             (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1798                                 VLAN_INPUT_TAG(m, RFD_TCI(status64));
1799                         } else {
1800                                 /* Pass it on. */
1801                                 ifp->if_input(ifp, m);
1802                         }
1803
1804                         STGE_RXCHAIN_RESET(sc);
1805                 }
1806         }
1807
1808         if (prog > 0) {
1809                 /* Update the consumer index. */
1810                 sc->sc_cdata.stge_rx_cons = cons;
1811                 bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
1812                     sc->sc_cdata.stge_rx_ring_map,
1813                     BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1814         }
1815 }
1816
1817 #ifdef DEVICE_POLLING
1818 static void
1819 stge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1820 {
1821         struct stge_softc *sc;
1822         uint16_t status;
1823
1824         sc = ifp->if_softc;
1825
1826         switch (cmd) {
1827         case POLL_REGISTER:
1828                 CSR_WRITE_2(sc, STGE_IntEnable, 0);
1829                 break;
1830         case POLL_DEREGISTER:
1831                 CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
1832                 break;
1833         case POLL_ONLY:
1834         case POLL_AND_CHECK_STATUS:
1835                 sc->sc_cdata.stge_rxcycles = count;
1836                 stge_rxeof(sc, count);
1837                 stge_txeof(sc);
1838
1839                 if (cmd == POLL_AND_CHECK_STATUS) {
1840                         status = CSR_READ_2(sc, STGE_IntStatus);
1841                         status &= sc->sc_IntEnable;
1842                         if (status != 0) {
1843                                 if (status & IS_HostError) {
1844                                         device_printf(sc->sc_dev,
1845                                         "Host interface error, "
1846                                         "resetting...\n");
1847                                         stge_init(sc);
1848                                 }
1849                                 if ((status & IS_TxComplete) != 0 &&
1850                                     stge_tx_error(sc) != 0)
1851                                         stge_init(sc);
1852                         }
1853
1854                 }
1855
1856                 if (!ifq_is_empty(&ifp->if_snd))
1857                         ifp->if_start(ifp);
1858         }
1859 }
1860 #endif  /* DEVICE_POLLING */
1861
1862 /*
1863  * stge_tick:
1864  *
1865  *      One second timer, used to tick the MII.
1866  */
1867 static void
1868 stge_tick(void *arg)
1869 {
1870         struct stge_softc *sc = arg;
1871         struct ifnet *ifp = &sc->arpcom.ac_if;
1872         struct mii_data *mii;
1873
1874         lwkt_serialize_enter(ifp->if_serializer);
1875
1876         mii = device_get_softc(sc->sc_miibus);
1877         mii_tick(mii);
1878
1879         /* Update statistics counters. */
1880         stge_stats_update(sc);
1881
1882         /*
1883          * Relcaim any pending Tx descriptors to release mbufs in a
1884          * timely manner as we don't generate Tx completion interrupts
1885          * for every frame. This limits the delay to a maximum of one
1886          * second.
1887          */
1888         if (sc->sc_cdata.stge_tx_cnt != 0)
1889                 stge_txeof(sc);
1890
1891         callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
1892
1893         lwkt_serialize_exit(ifp->if_serializer);
1894 }
1895
1896 /*
1897  * stge_stats_update:
1898  *
1899  *      Read the TC9021 statistics counters.
1900  */
1901 static void
1902 stge_stats_update(struct stge_softc *sc)
1903 {
1904         struct ifnet *ifp = &sc->arpcom.ac_if;
1905
1906         CSR_READ_4(sc,STGE_OctetRcvOk);
1907
1908         ifp->if_ipackets += CSR_READ_4(sc, STGE_FramesRcvdOk);
1909
1910         ifp->if_ierrors += CSR_READ_2(sc, STGE_FramesLostRxErrors);
1911
1912         CSR_READ_4(sc, STGE_OctetXmtdOk);
1913
1914         ifp->if_opackets += CSR_READ_4(sc, STGE_FramesXmtdOk);
1915
1916         ifp->if_collisions +=
1917             CSR_READ_4(sc, STGE_LateCollisions) +
1918             CSR_READ_4(sc, STGE_MultiColFrames) +
1919             CSR_READ_4(sc, STGE_SingleColFrames);
1920
1921         ifp->if_oerrors +=
1922             CSR_READ_2(sc, STGE_FramesAbortXSColls) +
1923             CSR_READ_2(sc, STGE_FramesWEXDeferal);
1924 }
1925
1926 /*
1927  * stge_reset:
1928  *
1929  *      Perform a soft reset on the TC9021.
1930  */
1931 static void
1932 stge_reset(struct stge_softc *sc, uint32_t how)
1933 {
1934         uint32_t ac;
1935         uint8_t v;
1936         int i, dv;
1937
1938         dv = 5000;
1939         ac = CSR_READ_4(sc, STGE_AsicCtrl);
1940         switch (how) {
1941         case STGE_RESET_TX:
1942                 ac |= AC_TxReset | AC_FIFO;
1943                 dv = 100;
1944                 break;
1945         case STGE_RESET_RX:
1946                 ac |= AC_RxReset | AC_FIFO;
1947                 dv = 100;
1948                 break;
1949         case STGE_RESET_FULL:
1950         default:
1951                 /*
1952                  * Only assert RstOut if we're fiber.  We need GMII clocks
1953                  * to be present in order for the reset to complete on fiber
1954                  * cards.
1955                  */
1956                 ac |= AC_GlobalReset | AC_RxReset | AC_TxReset |
1957                     AC_DMA | AC_FIFO | AC_Network | AC_Host | AC_AutoInit |
1958                     (sc->sc_usefiber ? AC_RstOut : 0);
1959                 break;
1960         }
1961
1962         CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1963
1964         /* Account for reset problem at 10Mbps. */
1965         DELAY(dv);
1966
1967         for (i = 0; i < STGE_TIMEOUT; i++) {
1968                 if ((CSR_READ_4(sc, STGE_AsicCtrl) & AC_ResetBusy) == 0)
1969                         break;
1970                 DELAY(dv);
1971         }
1972
1973         if (i == STGE_TIMEOUT)
1974                 device_printf(sc->sc_dev, "reset failed to complete\n");
1975
1976         /* Set LED, from Linux IPG driver. */
1977         ac = CSR_READ_4(sc, STGE_AsicCtrl);
1978         ac &= ~(AC_LEDMode | AC_LEDSpeed | AC_LEDModeBit1);
1979         if ((sc->sc_led & 0x01) != 0)
1980                 ac |= AC_LEDMode;
1981         if ((sc->sc_led & 0x03) != 0)
1982                 ac |= AC_LEDModeBit1;
1983         if ((sc->sc_led & 0x08) != 0)
1984                 ac |= AC_LEDSpeed;
1985         CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1986
1987         /* Set PHY, from Linux IPG driver */
1988         v = CSR_READ_1(sc, STGE_PhySet);
1989         v &= ~(PS_MemLenb9b | PS_MemLen | PS_NonCompdet);
1990         v |= ((sc->sc_led & 0x70) >> 4);
1991         CSR_WRITE_1(sc, STGE_PhySet, v);
1992 }
1993
1994 /*
1995  * stge_init:           [ ifnet interface function ]
1996  *
1997  *      Initialize the interface.
1998  */
1999 static void
2000 stge_init(void *xsc)
2001 {
2002         struct stge_softc *sc = xsc;
2003         struct ifnet *ifp = &sc->arpcom.ac_if;
2004         struct mii_data *mii;
2005         uint16_t eaddr[3];
2006         uint32_t v;
2007         int error;
2008
2009         ASSERT_SERIALIZED(ifp->if_serializer);
2010
2011         mii = device_get_softc(sc->sc_miibus);
2012
2013         /*
2014          * Cancel any pending I/O.
2015          */
2016         stge_stop(sc);
2017
2018         /* Init descriptors. */
2019         error = stge_init_rx_ring(sc);
2020         if (error != 0) {
2021                 device_printf(sc->sc_dev,
2022                     "initialization failed: no memory for rx buffers\n");
2023                 stge_stop(sc);
2024                 goto out;
2025         }
2026         stge_init_tx_ring(sc);
2027
2028         /* Set the station address. */
2029         bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2030         CSR_WRITE_2(sc, STGE_StationAddress0, htole16(eaddr[0]));
2031         CSR_WRITE_2(sc, STGE_StationAddress1, htole16(eaddr[1]));
2032         CSR_WRITE_2(sc, STGE_StationAddress2, htole16(eaddr[2]));
2033
2034         /*
2035          * Set the statistics masks.  Disable all the RMON stats,
2036          * and disable selected stats in the non-RMON stats registers.
2037          */
2038         CSR_WRITE_4(sc, STGE_RMONStatisticsMask, 0xffffffff);
2039         CSR_WRITE_4(sc, STGE_StatisticsMask,
2040             (1U << 1) | (1U << 2) | (1U << 3) | (1U << 4) | (1U << 5) |
2041             (1U << 6) | (1U << 7) | (1U << 8) | (1U << 9) | (1U << 10) |
2042             (1U << 13) | (1U << 14) | (1U << 15) | (1U << 19) | (1U << 20) |
2043             (1U << 21));
2044
2045         /* Set up the receive filter. */
2046         stge_set_filter(sc);
2047         /* Program multicast filter. */
2048         stge_set_multi(sc);
2049
2050         /*
2051          * Give the transmit and receive ring to the chip.
2052          */
2053         CSR_WRITE_4(sc, STGE_TFDListPtrHi,
2054             STGE_ADDR_HI(STGE_TX_RING_ADDR(sc, 0)));
2055         CSR_WRITE_4(sc, STGE_TFDListPtrLo,
2056             STGE_ADDR_LO(STGE_TX_RING_ADDR(sc, 0)));
2057
2058         CSR_WRITE_4(sc, STGE_RFDListPtrHi,
2059             STGE_ADDR_HI(STGE_RX_RING_ADDR(sc, 0)));
2060         CSR_WRITE_4(sc, STGE_RFDListPtrLo,
2061             STGE_ADDR_LO(STGE_RX_RING_ADDR(sc, 0)));
2062
2063         /*
2064          * Initialize the Tx auto-poll period.  It's OK to make this number
2065          * large (255 is the max, but we use 127) -- we explicitly kick the
2066          * transmit engine when there's actually a packet.
2067          */
2068         CSR_WRITE_1(sc, STGE_TxDMAPollPeriod, 127);
2069
2070         /* ..and the Rx auto-poll period. */
2071         CSR_WRITE_1(sc, STGE_RxDMAPollPeriod, 1);
2072
2073         /* Initialize the Tx start threshold. */
2074         CSR_WRITE_2(sc, STGE_TxStartThresh, sc->sc_txthresh);
2075
2076         /* Rx DMA thresholds, from Linux */
2077         CSR_WRITE_1(sc, STGE_RxDMABurstThresh, 0x30);
2078         CSR_WRITE_1(sc, STGE_RxDMAUrgentThresh, 0x30);
2079
2080         /* Rx early threhold, from Linux */
2081         CSR_WRITE_2(sc, STGE_RxEarlyThresh, 0x7ff);
2082
2083         /* Tx DMA thresholds, from Linux */
2084         CSR_WRITE_1(sc, STGE_TxDMABurstThresh, 0x30);
2085         CSR_WRITE_1(sc, STGE_TxDMAUrgentThresh, 0x04);
2086
2087         /*
2088          * Initialize the Rx DMA interrupt control register.  We
2089          * request an interrupt after every incoming packet, but
2090          * defer it for sc_rxint_dmawait us. When the number of
2091          * interrupts pending reaches STGE_RXINT_NFRAME, we stop
2092          * deferring the interrupt, and signal it immediately.
2093          */
2094         CSR_WRITE_4(sc, STGE_RxDMAIntCtrl,
2095             RDIC_RxFrameCount(sc->sc_rxint_nframe) |
2096             RDIC_RxDMAWaitTime(STGE_RXINT_USECS2TICK(sc->sc_rxint_dmawait)));
2097
2098         /*
2099          * Initialize the interrupt mask.
2100          */
2101         sc->sc_IntEnable = IS_HostError | IS_TxComplete |
2102             IS_TxDMAComplete | IS_RxDMAComplete | IS_RFDListEnd;
2103 #ifdef DEVICE_POLLING
2104         /* Disable interrupts if we are polling. */
2105         if (ifp->if_flags & IFF_POLLING)
2106                 CSR_WRITE_2(sc, STGE_IntEnable, 0);
2107         else
2108 #endif
2109         CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
2110
2111         /*
2112          * Configure the DMA engine.
2113          * XXX Should auto-tune TxBurstLimit.
2114          */
2115         CSR_WRITE_4(sc, STGE_DMACtrl, sc->sc_DMACtrl | DMAC_TxBurstLimit(3));
2116
2117         /*
2118          * Send a PAUSE frame when we reach 29,696 bytes in the Rx
2119          * FIFO, and send an un-PAUSE frame when we reach 3056 bytes
2120          * in the Rx FIFO.
2121          */
2122         CSR_WRITE_2(sc, STGE_FlowOnTresh, 29696 / 16);
2123         CSR_WRITE_2(sc, STGE_FlowOffThresh, 3056 / 16);
2124
2125         /*
2126          * Set the maximum frame size.
2127          */
2128         sc->sc_if_framesize = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
2129         CSR_WRITE_2(sc, STGE_MaxFrameSize, sc->sc_if_framesize);
2130
2131         /*
2132          * Initialize MacCtrl -- do it before setting the media,
2133          * as setting the media will actually program the register.
2134          *
2135          * Note: We have to poke the IFS value before poking
2136          * anything else.
2137          */
2138         /* Tx/Rx MAC should be disabled before programming IFS.*/
2139         CSR_WRITE_4(sc, STGE_MACCtrl, MC_IFSSelect(MC_IFS96bit));
2140
2141         stge_vlan_setup(sc);
2142
2143         if (sc->sc_rev >= 6) {          /* >= B.2 */
2144                 /* Multi-frag frame bug work-around. */
2145                 CSR_WRITE_2(sc, STGE_DebugCtrl,
2146                     CSR_READ_2(sc, STGE_DebugCtrl) | 0x0200);
2147
2148                 /* Tx Poll Now bug work-around. */
2149                 CSR_WRITE_2(sc, STGE_DebugCtrl,
2150                     CSR_READ_2(sc, STGE_DebugCtrl) | 0x0010);
2151                 /* Tx Poll Now bug work-around. */
2152                 CSR_WRITE_2(sc, STGE_DebugCtrl,
2153                     CSR_READ_2(sc, STGE_DebugCtrl) | 0x0020);
2154         }
2155
2156         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2157         v |= MC_StatisticsEnable | MC_TxEnable | MC_RxEnable;
2158         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2159         /*
2160          * It seems that transmitting frames without checking the state of
2161          * Rx/Tx MAC wedge the hardware.
2162          */
2163         stge_start_tx(sc);
2164         stge_start_rx(sc);
2165
2166         /*
2167          * Set the current media.
2168          */
2169         mii_mediachg(mii);
2170
2171         /*
2172          * Start the one second MII clock.
2173          */
2174         callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
2175
2176         /*
2177          * ...all done!
2178          */
2179         ifp->if_flags |= IFF_RUNNING;
2180         ifp->if_flags &= ~IFF_OACTIVE;
2181
2182  out:
2183         if (error != 0)
2184                 device_printf(sc->sc_dev, "interface not running\n");
2185 }
2186
2187 static void
2188 stge_vlan_setup(struct stge_softc *sc)
2189 {
2190         struct ifnet *ifp = &sc->arpcom.ac_if;
2191         uint32_t v;
2192
2193         /*
2194          * The NIC always copy a VLAN tag regardless of STGE_MACCtrl
2195          * MC_AutoVLANuntagging bit.
2196          * MC_AutoVLANtagging bit selects which VLAN source to use
2197          * between STGE_VLANTag and TFC. However TFC TFD_VLANTagInsert
2198          * bit has priority over MC_AutoVLANtagging bit. So we always
2199          * use TFC instead of STGE_VLANTag register.
2200          */
2201         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2202         if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
2203                 v |= MC_AutoVLANuntagging;
2204         else
2205                 v &= ~MC_AutoVLANuntagging;
2206         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2207 }
2208
2209 /*
2210  *      Stop transmission on the interface.
2211  */
2212 static void
2213 stge_stop(struct stge_softc *sc)
2214 {
2215         struct ifnet *ifp = &sc->arpcom.ac_if;
2216         struct stge_txdesc *txd;
2217         struct stge_rxdesc *rxd;
2218         uint32_t v;
2219         int i;
2220
2221         ASSERT_SERIALIZED(ifp->if_serializer);
2222
2223         /*
2224          * Stop the one second clock.
2225          */
2226         callout_stop(&sc->sc_tick_ch);
2227
2228         /*
2229          * Reset the chip to a known state.
2230          */
2231         stge_reset(sc, STGE_RESET_FULL);
2232
2233         /*
2234          * Disable interrupts.
2235          */
2236         CSR_WRITE_2(sc, STGE_IntEnable, 0);
2237
2238         /*
2239          * Stop receiver, transmitter, and stats update.
2240          */
2241         stge_stop_rx(sc);
2242         stge_stop_tx(sc);
2243         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2244         v |= MC_StatisticsDisable;
2245         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2246
2247         /*
2248          * Stop the transmit and receive DMA.
2249          */
2250         stge_dma_wait(sc);
2251         CSR_WRITE_4(sc, STGE_TFDListPtrHi, 0);
2252         CSR_WRITE_4(sc, STGE_TFDListPtrLo, 0);
2253         CSR_WRITE_4(sc, STGE_RFDListPtrHi, 0);
2254         CSR_WRITE_4(sc, STGE_RFDListPtrLo, 0);
2255
2256         /*
2257          * Free RX and TX mbufs still in the queues.
2258          */
2259         for (i = 0; i < STGE_RX_RING_CNT; i++) {
2260                 rxd = &sc->sc_cdata.stge_rxdesc[i];
2261                 if (rxd->rx_m != NULL) {
2262                         bus_dmamap_sync(sc->sc_cdata.stge_rx_tag,
2263                             rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2264                         bus_dmamap_unload(sc->sc_cdata.stge_rx_tag,
2265                             rxd->rx_dmamap);
2266                         m_freem(rxd->rx_m);
2267                         rxd->rx_m = NULL;
2268                 }
2269         }
2270         for (i = 0; i < STGE_TX_RING_CNT; i++) {
2271                 txd = &sc->sc_cdata.stge_txdesc[i];
2272                 if (txd->tx_m != NULL) {
2273                         bus_dmamap_sync(sc->sc_cdata.stge_tx_tag,
2274                             txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2275                         bus_dmamap_unload(sc->sc_cdata.stge_tx_tag,
2276                             txd->tx_dmamap);
2277                         m_freem(txd->tx_m);
2278                         txd->tx_m = NULL;
2279                 }
2280         }
2281
2282         /*
2283          * Mark the interface down and cancel the watchdog timer.
2284          */
2285         ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2286         ifp->if_timer = 0;
2287 }
2288
2289 static void
2290 stge_start_tx(struct stge_softc *sc)
2291 {
2292         uint32_t v;
2293         int i;
2294
2295         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2296         if ((v & MC_TxEnabled) != 0)
2297                 return;
2298         v |= MC_TxEnable;
2299         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2300         CSR_WRITE_1(sc, STGE_TxDMAPollPeriod, 127);
2301         for (i = STGE_TIMEOUT; i > 0; i--) {
2302                 DELAY(10);
2303                 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2304                 if ((v & MC_TxEnabled) != 0)
2305                         break;
2306         }
2307         if (i == 0)
2308                 device_printf(sc->sc_dev, "Starting Tx MAC timed out\n");
2309 }
2310
2311 static void
2312 stge_start_rx(struct stge_softc *sc)
2313 {
2314         uint32_t v;
2315         int i;
2316
2317         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2318         if ((v & MC_RxEnabled) != 0)
2319                 return;
2320         v |= MC_RxEnable;
2321         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2322         CSR_WRITE_1(sc, STGE_RxDMAPollPeriod, 1);
2323         for (i = STGE_TIMEOUT; i > 0; i--) {
2324                 DELAY(10);
2325                 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2326                 if ((v & MC_RxEnabled) != 0)
2327                         break;
2328         }
2329         if (i == 0)
2330                 device_printf(sc->sc_dev, "Starting Rx MAC timed out\n");
2331 }
2332
2333 static void
2334 stge_stop_tx(struct stge_softc *sc)
2335 {
2336         uint32_t v;
2337         int i;
2338
2339         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2340         if ((v & MC_TxEnabled) == 0)
2341                 return;
2342         v |= MC_TxDisable;
2343         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2344         for (i = STGE_TIMEOUT; i > 0; i--) {
2345                 DELAY(10);
2346                 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2347                 if ((v & MC_TxEnabled) == 0)
2348                         break;
2349         }
2350         if (i == 0)
2351                 device_printf(sc->sc_dev, "Stopping Tx MAC timed out\n");
2352 }
2353
2354 static void
2355 stge_stop_rx(struct stge_softc *sc)
2356 {
2357         uint32_t v;
2358         int i;
2359
2360         v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2361         if ((v & MC_RxEnabled) == 0)
2362                 return;
2363         v |= MC_RxDisable;
2364         CSR_WRITE_4(sc, STGE_MACCtrl, v);
2365         for (i = STGE_TIMEOUT; i > 0; i--) {
2366                 DELAY(10);
2367                 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2368                 if ((v & MC_RxEnabled) == 0)
2369                         break;
2370         }
2371         if (i == 0)
2372                 device_printf(sc->sc_dev, "Stopping Rx MAC timed out\n");
2373 }
2374
2375 static void
2376 stge_init_tx_ring(struct stge_softc *sc)
2377 {
2378         struct stge_ring_data *rd;
2379         struct stge_txdesc *txd;
2380         bus_addr_t addr;
2381         int i;
2382
2383         STAILQ_INIT(&sc->sc_cdata.stge_txfreeq);
2384         STAILQ_INIT(&sc->sc_cdata.stge_txbusyq);
2385
2386         sc->sc_cdata.stge_tx_prod = 0;
2387         sc->sc_cdata.stge_tx_cons = 0;
2388         sc->sc_cdata.stge_tx_cnt = 0;
2389
2390         rd = &sc->sc_rdata;
2391         bzero(rd->stge_tx_ring, STGE_TX_RING_SZ);
2392         for (i = 0; i < STGE_TX_RING_CNT; i++) {
2393                 if (i == (STGE_TX_RING_CNT - 1))
2394                         addr = STGE_TX_RING_ADDR(sc, 0);
2395                 else
2396                         addr = STGE_TX_RING_ADDR(sc, i + 1);
2397                 rd->stge_tx_ring[i].tfd_next = htole64(addr);
2398                 rd->stge_tx_ring[i].tfd_control = htole64(TFD_TFDDone);
2399                 txd = &sc->sc_cdata.stge_txdesc[i];
2400                 STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txfreeq, txd, tx_q);
2401         }
2402
2403         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
2404             sc->sc_cdata.stge_tx_ring_map, BUS_DMASYNC_PREWRITE);
2405 }
2406
2407 static int
2408 stge_init_rx_ring(struct stge_softc *sc)
2409 {
2410         struct stge_ring_data *rd;
2411         bus_addr_t addr;
2412         int i;
2413
2414         sc->sc_cdata.stge_rx_cons = 0;
2415         STGE_RXCHAIN_RESET(sc);
2416
2417         rd = &sc->sc_rdata;
2418         bzero(rd->stge_rx_ring, STGE_RX_RING_SZ);
2419         for (i = 0; i < STGE_RX_RING_CNT; i++) {
2420                 if (stge_newbuf(sc, i, 1) != 0)
2421                         return (ENOBUFS);
2422                 if (i == (STGE_RX_RING_CNT - 1))
2423                         addr = STGE_RX_RING_ADDR(sc, 0);
2424                 else
2425                         addr = STGE_RX_RING_ADDR(sc, i + 1);
2426                 rd->stge_rx_ring[i].rfd_next = htole64(addr);
2427                 rd->stge_rx_ring[i].rfd_status = 0;
2428         }
2429
2430         bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
2431             sc->sc_cdata.stge_rx_ring_map, BUS_DMASYNC_PREWRITE);
2432
2433         return (0);
2434 }
2435
2436 /*
2437  * stge_newbuf:
2438  *
2439  *      Add a receive buffer to the indicated descriptor.
2440  */
2441 static int
2442 stge_newbuf(struct stge_softc *sc, int idx, int waitok)
2443 {
2444         struct stge_rxdesc *rxd;
2445         struct stge_rfd *rfd;
2446         struct mbuf *m;
2447         struct stge_mbuf_dmamap_arg arg;
2448         bus_dma_segment_t segs[1];
2449         bus_dmamap_t map;
2450
2451         m = m_getcl(waitok ? MB_WAIT : MB_DONTWAIT, MT_DATA, M_PKTHDR);
2452         if (m == NULL)
2453                 return (ENOBUFS);
2454         m->m_len = m->m_pkthdr.len = MCLBYTES;
2455         /*
2456          * The hardware requires 4bytes aligned DMA address when JUMBO
2457          * frame is used.
2458          */
2459         if (sc->sc_if_framesize <= (MCLBYTES - ETHER_ALIGN))
2460                 m_adj(m, ETHER_ALIGN);
2461
2462         arg.segs = segs;
2463         arg.nsegs = 1;
2464         if (bus_dmamap_load_mbuf(sc->sc_cdata.stge_rx_tag,
2465             sc->sc_cdata.stge_rx_sparemap, m, stge_mbuf_dmamap_cb, &arg,
2466             waitok ? BUS_DMA_WAITOK : BUS_DMA_NOWAIT) != 0) {
2467                 m_freem(m);
2468                 return (ENOBUFS);
2469         }
2470
2471         rxd = &sc->sc_cdata.stge_rxdesc[idx];
2472         if (rxd->rx_m != NULL) {
2473                 bus_dmamap_sync(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap,
2474                     BUS_DMASYNC_POSTREAD);
2475                 bus_dmamap_unload(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap);
2476         }
2477         map = rxd->rx_dmamap;
2478         rxd->rx_dmamap = sc->sc_cdata.stge_rx_sparemap;
2479         sc->sc_cdata.stge_rx_sparemap = map;
2480         bus_dmamap_sync(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap,
2481             BUS_DMASYNC_PREREAD);
2482         rxd->rx_m = m;
2483
2484         rfd = &sc->sc_rdata.stge_rx_ring[idx];
2485         rfd->rfd_frag.frag_word0 =
2486             htole64(FRAG_ADDR(segs[0].ds_addr) | FRAG_LEN(segs[0].ds_len));
2487         rfd->rfd_status = 0;
2488
2489         return (0);
2490 }
2491
2492 /*
2493  * stge_set_filter:
2494  *
2495  *      Set up the receive filter.
2496  */
2497 static void
2498 stge_set_filter(struct stge_softc *sc)
2499 {
2500         struct ifnet *ifp = &sc->arpcom.ac_if;
2501         uint16_t mode;
2502
2503         mode = CSR_READ_2(sc, STGE_ReceiveMode);
2504         mode |= RM_ReceiveUnicast;
2505         if ((ifp->if_flags & IFF_BROADCAST) != 0)
2506                 mode |= RM_ReceiveBroadcast;
2507         else
2508                 mode &= ~RM_ReceiveBroadcast;
2509         if ((ifp->if_flags & IFF_PROMISC) != 0)
2510                 mode |= RM_ReceiveAllFrames;
2511         else
2512                 mode &= ~RM_ReceiveAllFrames;
2513
2514         CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2515 }
2516
2517 static void
2518 stge_set_multi(struct stge_softc *sc)
2519 {
2520         struct ifnet *ifp = &sc->arpcom.ac_if;
2521         struct ifmultiaddr *ifma;
2522         uint32_t crc;
2523         uint32_t mchash[2];
2524         uint16_t mode;
2525         int count;
2526
2527         mode = CSR_READ_2(sc, STGE_ReceiveMode);
2528         if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
2529                 if ((ifp->if_flags & IFF_PROMISC) != 0)
2530                         mode |= RM_ReceiveAllFrames;
2531                 else if ((ifp->if_flags & IFF_ALLMULTI) != 0)
2532                         mode |= RM_ReceiveMulticast;
2533                 CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2534                 return;
2535         }
2536
2537         /* clear existing filters. */
2538         CSR_WRITE_4(sc, STGE_HashTable0, 0);
2539         CSR_WRITE_4(sc, STGE_HashTable1, 0);
2540
2541         /*
2542          * Set up the multicast address filter by passing all multicast
2543          * addresses through a CRC generator, and then using the low-order
2544          * 6 bits as an index into the 64 bit multicast hash table.  The
2545          * high order bits select the register, while the rest of the bits
2546          * select the bit within the register.
2547          */
2548
2549         bzero(mchash, sizeof(mchash));
2550
2551         count = 0;
2552         LIST_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
2553                 if (ifma->ifma_addr->sa_family != AF_LINK)
2554                         continue;
2555                 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
2556                     ifma->ifma_addr), ETHER_ADDR_LEN);
2557
2558                 /* Just want the 6 least significant bits. */
2559                 crc &= 0x3f;
2560
2561                 /* Set the corresponding bit in the hash table. */
2562                 mchash[crc >> 5] |= 1 << (crc & 0x1f);
2563                 count++;
2564         }
2565
2566         mode &= ~(RM_ReceiveMulticast | RM_ReceiveAllFrames);
2567         if (count > 0)
2568                 mode |= RM_ReceiveMulticastHash;
2569         else
2570                 mode &= ~RM_ReceiveMulticastHash;
2571
2572         CSR_WRITE_4(sc, STGE_HashTable0, mchash[0]);
2573         CSR_WRITE_4(sc, STGE_HashTable1, mchash[1]);
2574         CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2575 }
2576
2577 static int
2578 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2579 {
2580         int error, value;
2581
2582         if (!arg1)
2583                 return (EINVAL);
2584         value = *(int *)arg1;
2585         error = sysctl_handle_int(oidp, &value, 0, req);
2586         if (error || !req->newptr)
2587                 return (error);
2588         if (value < low || value > high)
2589                 return (EINVAL);
2590         *(int *)arg1 = value;
2591
2592         return (0);
2593 }
2594
2595 static int
2596 sysctl_hw_stge_rxint_nframe(SYSCTL_HANDLER_ARGS)
2597 {
2598         return (sysctl_int_range(oidp, arg1, arg2, req,
2599             STGE_RXINT_NFRAME_MIN, STGE_RXINT_NFRAME_MAX));
2600 }
2601
2602 static int
2603 sysctl_hw_stge_rxint_dmawait(SYSCTL_HANDLER_ARGS)
2604 {
2605         return (sysctl_int_range(oidp, arg1, arg2, req,
2606             STGE_RXINT_DMAWAIT_MIN, STGE_RXINT_DMAWAIT_MAX));
2607 }