1 /* $NetBSD: if_stge.c,v 1.32 2005/12/11 12:22:49 christos Exp $ */
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Device driver for the Sundance Tech. TC9021 10/100/1000
34 * Ethernet controller.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #ifdef HAVE_KERNEL_OPTION_HEADERS
41 #include "opt_device_polling.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/endian.h>
48 #include <sys/malloc.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
51 #include <sys/socket.h>
52 #include <sys/sockio.h>
53 #include <sys/sysctl.h>
54 #include <sys/taskqueue.h>
57 #include <net/ethernet.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #include <net/if_types.h>
62 #include <net/if_vlan_var.h>
64 #include <machine/bus.h>
65 #include <machine/resource.h>
69 #include <dev/mii/mii.h>
70 #include <dev/mii/miivar.h>
72 #include <dev/pci/pcireg.h>
73 #include <dev/pci/pcivar.h>
75 #include <dev/stge/if_stgereg.h>
77 #define STGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP)
79 MODULE_DEPEND(stge, pci, 1, 1, 1);
80 MODULE_DEPEND(stge, ether, 1, 1, 1);
81 MODULE_DEPEND(stge, miibus, 1, 1, 1);
83 /* "device miibus" required. See GENERIC if you get errors here. */
84 #include "miibus_if.h"
87 * Devices supported by this driver.
89 static struct stge_product {
90 uint16_t stge_vendorid;
91 uint16_t stge_deviceid;
92 const char *stge_name;
94 { VENDOR_SUNDANCETI, DEVICEID_SUNDANCETI_ST1023,
95 "Sundance ST-1023 Gigabit Ethernet" },
97 { VENDOR_SUNDANCETI, DEVICEID_SUNDANCETI_ST2021,
98 "Sundance ST-2021 Gigabit Ethernet" },
100 { VENDOR_TAMARACK, DEVICEID_TAMARACK_TC9021,
101 "Tamarack TC9021 Gigabit Ethernet" },
103 { VENDOR_TAMARACK, DEVICEID_TAMARACK_TC9021_ALT,
104 "Tamarack TC9021 Gigabit Ethernet" },
107 * The Sundance sample boards use the Sundance vendor ID,
108 * but the Tamarack product ID.
110 { VENDOR_SUNDANCETI, DEVICEID_TAMARACK_TC9021,
111 "Sundance TC9021 Gigabit Ethernet" },
113 { VENDOR_SUNDANCETI, DEVICEID_TAMARACK_TC9021_ALT,
114 "Sundance TC9021 Gigabit Ethernet" },
116 { VENDOR_DLINK, DEVICEID_DLINK_DL4000,
117 "D-Link DL-4000 Gigabit Ethernet" },
119 { VENDOR_ANTARES, DEVICEID_ANTARES_TC9021,
120 "Antares Gigabit Ethernet" }
123 static int stge_probe(device_t);
124 static int stge_attach(device_t);
125 static int stge_detach(device_t);
126 static int stge_shutdown(device_t);
127 static int stge_suspend(device_t);
128 static int stge_resume(device_t);
130 static int stge_encap(struct stge_softc *, struct mbuf **);
131 static void stge_start(struct ifnet *);
132 static void stge_start_locked(struct ifnet *);
133 static void stge_watchdog(struct stge_softc *);
134 static int stge_ioctl(struct ifnet *, u_long, caddr_t);
135 static void stge_init(void *);
136 static void stge_init_locked(struct stge_softc *);
137 static void stge_vlan_setup(struct stge_softc *);
138 static void stge_stop(struct stge_softc *);
139 static void stge_start_tx(struct stge_softc *);
140 static void stge_start_rx(struct stge_softc *);
141 static void stge_stop_tx(struct stge_softc *);
142 static void stge_stop_rx(struct stge_softc *);
144 static void stge_reset(struct stge_softc *, uint32_t);
145 static int stge_eeprom_wait(struct stge_softc *);
146 static void stge_read_eeprom(struct stge_softc *, int, uint16_t *);
147 static void stge_tick(void *);
148 static void stge_stats_update(struct stge_softc *);
149 static void stge_set_filter(struct stge_softc *);
150 static void stge_set_multi(struct stge_softc *);
152 static void stge_link_task(void *, int);
153 static void stge_intr(void *);
154 static __inline int stge_tx_error(struct stge_softc *);
155 static void stge_txeof(struct stge_softc *);
156 static int stge_rxeof(struct stge_softc *);
157 static __inline void stge_discard_rxbuf(struct stge_softc *, int);
158 static int stge_newbuf(struct stge_softc *, int);
159 #ifndef __NO_STRICT_ALIGNMENT
160 static __inline struct mbuf *stge_fixup_rx(struct stge_softc *, struct mbuf *);
163 static void stge_mii_sync(struct stge_softc *);
164 static void stge_mii_send(struct stge_softc *, uint32_t, int);
165 static int stge_mii_readreg(struct stge_softc *, struct stge_mii_frame *);
166 static int stge_mii_writereg(struct stge_softc *, struct stge_mii_frame *);
167 static int stge_miibus_readreg(device_t, int, int);
168 static int stge_miibus_writereg(device_t, int, int, int);
169 static void stge_miibus_statchg(device_t);
170 static int stge_mediachange(struct ifnet *);
171 static void stge_mediastatus(struct ifnet *, struct ifmediareq *);
173 static void stge_dmamap_cb(void *, bus_dma_segment_t *, int, int);
174 static int stge_dma_alloc(struct stge_softc *);
175 static void stge_dma_free(struct stge_softc *);
176 static void stge_dma_wait(struct stge_softc *);
177 static void stge_init_tx_ring(struct stge_softc *);
178 static int stge_init_rx_ring(struct stge_softc *);
179 #ifdef DEVICE_POLLING
180 static int stge_poll(struct ifnet *, enum poll_cmd, int);
183 static void stge_setwol(struct stge_softc *);
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);
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),
198 DEVMETHOD(miibus_readreg, stge_miibus_readreg),
199 DEVMETHOD(miibus_writereg, stge_miibus_writereg),
200 DEVMETHOD(miibus_statchg, stge_miibus_statchg),
206 static driver_t stge_driver = {
209 sizeof(struct stge_softc)
212 static devclass_t stge_devclass;
214 DRIVER_MODULE(stge, pci, stge_driver, stge_devclass, 0, 0);
215 DRIVER_MODULE(miibus, stge, miibus_driver, miibus_devclass, 0, 0);
217 static struct resource_spec stge_res_spec_io[] = {
218 { SYS_RES_IOPORT, PCIR_BAR(0), RF_ACTIVE },
219 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
223 static struct resource_spec stge_res_spec_mem[] = {
224 { SYS_RES_MEMORY, PCIR_BAR(1), RF_ACTIVE },
225 { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
230 CSR_WRITE_1(sc, STGE_PhyCtrl, CSR_READ_1(sc, STGE_PhyCtrl) | (x))
232 CSR_WRITE_1(sc, STGE_PhyCtrl, CSR_READ_1(sc, STGE_PhyCtrl) & ~(x))
235 * Sync the PHYs by setting data bit and strobing the clock 32 times.
238 stge_mii_sync(struct stge_softc *sc)
242 MII_SET(PC_MgmtDir | PC_MgmtData);
244 for (i = 0; i < 32; i++) {
253 * Clock a series of bits through the MII.
256 stge_mii_send(struct stge_softc *sc, uint32_t bits, int cnt)
262 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
264 MII_SET(PC_MgmtData);
266 MII_CLR(PC_MgmtData);
275 * Read an PHY register through the MII.
278 stge_mii_readreg(struct stge_softc *sc, struct stge_mii_frame *frame)
283 * Set up frame for RX.
285 frame->mii_stdelim = STGE_MII_STARTDELIM;
286 frame->mii_opcode = STGE_MII_READOP;
287 frame->mii_turnaround = 0;
290 CSR_WRITE_1(sc, STGE_PhyCtrl, 0 | sc->sc_PhyCtrl);
299 * Send command/address info.
301 stge_mii_send(sc, frame->mii_stdelim, 2);
302 stge_mii_send(sc, frame->mii_opcode, 2);
303 stge_mii_send(sc, frame->mii_phyaddr, 5);
304 stge_mii_send(sc, frame->mii_regaddr, 5);
310 MII_CLR((PC_MgmtClk | PC_MgmtData));
318 ack = CSR_READ_1(sc, STGE_PhyCtrl) & PC_MgmtData;
323 * Now try reading data bits. If the ack failed, we still
324 * need to clock through 16 cycles to keep the PHY(s) in sync.
327 for(i = 0; i < 16; i++) {
336 for (i = 0x8000; i; i >>= 1) {
340 if (CSR_READ_1(sc, STGE_PhyCtrl) & PC_MgmtData)
341 frame->mii_data |= i;
360 * Write to a PHY register through the MII.
363 stge_mii_writereg(struct stge_softc *sc, struct stge_mii_frame *frame)
367 * Set up frame for TX.
369 frame->mii_stdelim = STGE_MII_STARTDELIM;
370 frame->mii_opcode = STGE_MII_WRITEOP;
371 frame->mii_turnaround = STGE_MII_TURNAROUND;
374 * Turn on data output.
380 stge_mii_send(sc, frame->mii_stdelim, 2);
381 stge_mii_send(sc, frame->mii_opcode, 2);
382 stge_mii_send(sc, frame->mii_phyaddr, 5);
383 stge_mii_send(sc, frame->mii_regaddr, 5);
384 stge_mii_send(sc, frame->mii_turnaround, 2);
385 stge_mii_send(sc, frame->mii_data, 16);
402 * sc_miibus_readreg: [mii interface function]
404 * Read a PHY register on the MII of the TC9021.
407 stge_miibus_readreg(device_t dev, int phy, int reg)
409 struct stge_softc *sc;
410 struct stge_mii_frame frame;
413 sc = device_get_softc(dev);
415 if (reg == STGE_PhyCtrl) {
416 /* XXX allow ip1000phy read STGE_PhyCtrl register. */
418 error = CSR_READ_1(sc, STGE_PhyCtrl);
422 bzero(&frame, sizeof(frame));
423 frame.mii_phyaddr = phy;
424 frame.mii_regaddr = reg;
427 error = stge_mii_readreg(sc, &frame);
431 /* Don't show errors for PHY probe request */
433 device_printf(sc->sc_dev, "phy read fail\n");
436 return (frame.mii_data);
440 * stge_miibus_writereg: [mii interface function]
442 * Write a PHY register on the MII of the TC9021.
445 stge_miibus_writereg(device_t dev, int phy, int reg, int val)
447 struct stge_softc *sc;
448 struct stge_mii_frame frame;
451 sc = device_get_softc(dev);
453 bzero(&frame, sizeof(frame));
454 frame.mii_phyaddr = phy;
455 frame.mii_regaddr = reg;
456 frame.mii_data = val;
459 error = stge_mii_writereg(sc, &frame);
463 device_printf(sc->sc_dev, "phy write fail\n");
468 * stge_miibus_statchg: [mii interface function]
470 * Callback from MII layer when media changes.
473 stge_miibus_statchg(device_t dev)
475 struct stge_softc *sc;
477 sc = device_get_softc(dev);
478 taskqueue_enqueue(taskqueue_swi, &sc->sc_link_task);
482 * stge_mediastatus: [ifmedia interface function]
484 * Get the current interface media status.
487 stge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
489 struct stge_softc *sc;
490 struct mii_data *mii;
493 mii = device_get_softc(sc->sc_miibus);
496 ifmr->ifm_status = mii->mii_media_status;
497 ifmr->ifm_active = mii->mii_media_active;
501 * stge_mediachange: [ifmedia interface function]
503 * Set hardware to newly-selected media.
506 stge_mediachange(struct ifnet *ifp)
508 struct stge_softc *sc;
509 struct mii_data *mii;
512 mii = device_get_softc(sc->sc_miibus);
519 stge_eeprom_wait(struct stge_softc *sc)
523 for (i = 0; i < STGE_TIMEOUT; i++) {
525 if ((CSR_READ_2(sc, STGE_EepromCtrl) & EC_EepromBusy) == 0)
534 * Read data from the serial EEPROM.
537 stge_read_eeprom(struct stge_softc *sc, int offset, uint16_t *data)
540 if (stge_eeprom_wait(sc))
541 device_printf(sc->sc_dev, "EEPROM failed to come ready\n");
543 CSR_WRITE_2(sc, STGE_EepromCtrl,
544 EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_RR));
545 if (stge_eeprom_wait(sc))
546 device_printf(sc->sc_dev, "EEPROM read timed out\n");
547 *data = CSR_READ_2(sc, STGE_EepromData);
552 stge_probe(device_t dev)
554 struct stge_product *sp;
556 uint16_t vendor, devid;
558 vendor = pci_get_vendor(dev);
559 devid = pci_get_device(dev);
561 for (i = 0; i < sizeof(stge_products)/sizeof(stge_products[0]);
563 if (vendor == sp->stge_vendorid &&
564 devid == sp->stge_deviceid) {
565 device_set_desc(dev, sp->stge_name);
566 return (BUS_PROBE_DEFAULT);
574 stge_attach(device_t dev)
576 struct stge_softc *sc;
578 uint8_t enaddr[ETHER_ADDR_LEN];
584 sc = device_get_softc(dev);
587 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
589 mtx_init(&sc->sc_mii_mtx, "stge_mii_mutex", NULL, MTX_DEF);
590 callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
591 TASK_INIT(&sc->sc_link_task, 0, stge_link_task, sc);
596 pci_enable_busmaster(dev);
597 cmd = pci_read_config(dev, PCIR_COMMAND, 2);
598 val = pci_read_config(dev, PCIR_BAR(1), 4);
599 if ((val & 0x01) != 0)
600 sc->sc_spec = stge_res_spec_mem;
602 val = pci_read_config(dev, PCIR_BAR(0), 4);
603 if ((val & 0x01) == 0) {
604 device_printf(sc->sc_dev, "couldn't locate IO BAR\n");
608 sc->sc_spec = stge_res_spec_io;
610 error = bus_alloc_resources(dev, sc->sc_spec, sc->sc_res);
612 device_printf(dev, "couldn't allocate %s resources\n",
613 sc->sc_spec == stge_res_spec_mem ? "memory" : "I/O");
616 sc->sc_rev = pci_get_revid(dev);
618 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
619 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
620 "rxint_nframe", CTLTYPE_INT|CTLFLAG_RW, &sc->sc_rxint_nframe, 0,
621 sysctl_hw_stge_rxint_nframe, "I", "stge rx interrupt nframe");
623 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
624 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
625 "rxint_dmawait", CTLTYPE_INT|CTLFLAG_RW, &sc->sc_rxint_dmawait, 0,
626 sysctl_hw_stge_rxint_dmawait, "I", "stge rx interrupt dmawait");
628 /* Pull in device tunables. */
629 sc->sc_rxint_nframe = STGE_RXINT_NFRAME_DEFAULT;
630 error = resource_int_value(device_get_name(dev), device_get_unit(dev),
631 "rxint_nframe", &sc->sc_rxint_nframe);
633 if (sc->sc_rxint_nframe < STGE_RXINT_NFRAME_MIN ||
634 sc->sc_rxint_nframe > STGE_RXINT_NFRAME_MAX) {
635 device_printf(dev, "rxint_nframe value out of range; "
636 "using default: %d\n", STGE_RXINT_NFRAME_DEFAULT);
637 sc->sc_rxint_nframe = STGE_RXINT_NFRAME_DEFAULT;
641 sc->sc_rxint_dmawait = STGE_RXINT_DMAWAIT_DEFAULT;
642 error = resource_int_value(device_get_name(dev), device_get_unit(dev),
643 "rxint_dmawait", &sc->sc_rxint_dmawait);
645 if (sc->sc_rxint_dmawait < STGE_RXINT_DMAWAIT_MIN ||
646 sc->sc_rxint_dmawait > STGE_RXINT_DMAWAIT_MAX) {
647 device_printf(dev, "rxint_dmawait value out of range; "
648 "using default: %d\n", STGE_RXINT_DMAWAIT_DEFAULT);
649 sc->sc_rxint_dmawait = STGE_RXINT_DMAWAIT_DEFAULT;
653 if ((error = stge_dma_alloc(sc) != 0))
657 * Determine if we're copper or fiber. It affects how we
660 if (CSR_READ_4(sc, STGE_AsicCtrl) & AC_PhyMedia)
665 /* Load LED configuration from EEPROM. */
666 stge_read_eeprom(sc, STGE_EEPROM_LEDMode, &sc->sc_led);
669 * Reset the chip to a known state.
672 stge_reset(sc, STGE_RESET_FULL);
676 * Reading the station address from the EEPROM doesn't seem
677 * to work, at least on my sample boards. Instead, since
678 * the reset sequence does AutoInit, read it from the station
679 * address registers. For Sundance 1023 you can only read it
682 if (pci_get_device(dev) != DEVICEID_SUNDANCETI_ST1023) {
685 v = CSR_READ_2(sc, STGE_StationAddress0);
686 enaddr[0] = v & 0xff;
688 v = CSR_READ_2(sc, STGE_StationAddress1);
689 enaddr[2] = v & 0xff;
691 v = CSR_READ_2(sc, STGE_StationAddress2);
692 enaddr[4] = v & 0xff;
696 uint16_t myaddr[ETHER_ADDR_LEN / 2];
697 for (i = 0; i <ETHER_ADDR_LEN / 2; i++) {
698 stge_read_eeprom(sc, STGE_EEPROM_StationAddress0 + i,
700 myaddr[i] = le16toh(myaddr[i]);
702 bcopy(myaddr, enaddr, sizeof(enaddr));
706 ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
708 device_printf(sc->sc_dev, "failed to if_alloc()\n");
714 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
715 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
716 ifp->if_ioctl = stge_ioctl;
717 ifp->if_start = stge_start;
718 ifp->if_init = stge_init;
719 ifp->if_mtu = ETHERMTU;
720 ifp->if_snd.ifq_drv_maxlen = STGE_TX_RING_CNT - 1;
721 IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
722 IFQ_SET_READY(&ifp->if_snd);
723 /* Revision B3 and earlier chips have checksum bug. */
724 if (sc->sc_rev >= 0x0c) {
725 ifp->if_hwassist = STGE_CSUM_FEATURES;
726 ifp->if_capabilities = IFCAP_HWCSUM;
728 ifp->if_hwassist = 0;
729 ifp->if_capabilities = 0;
731 ifp->if_capabilities |= IFCAP_WOL_MAGIC;
732 ifp->if_capenable = ifp->if_capabilities;
735 * Read some important bits from the PhyCtrl register.
737 sc->sc_PhyCtrl = CSR_READ_1(sc, STGE_PhyCtrl) &
738 (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
740 /* Set up MII bus. */
741 flags = MIIF_DOPAUSE;
742 if (sc->sc_rev >= 0x40 && sc->sc_rev <= 0x4e)
743 flags |= MIIF_MACPRIV0;
744 error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp, stge_mediachange,
745 stge_mediastatus, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
748 device_printf(sc->sc_dev, "attaching PHYs failed\n");
752 ether_ifattach(ifp, enaddr);
754 /* VLAN capability setup */
755 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
756 if (sc->sc_rev >= 0x0c)
757 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
758 ifp->if_capenable = ifp->if_capabilities;
759 #ifdef DEVICE_POLLING
760 ifp->if_capabilities |= IFCAP_POLLING;
763 * Tell the upper layer(s) we support long frames.
764 * Must appear after the call to ether_ifattach() because
765 * ether_ifattach() sets ifi_hdrlen to the default value.
767 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
770 * The manual recommends disabling early transmit, so we
771 * do. It's disabled anyway, if using IP checksumming,
772 * since the entire packet must be in the FIFO in order
773 * for the chip to perform the checksum.
775 sc->sc_txthresh = 0x0fff;
778 * Disable MWI if the PCI layer tells us to.
781 if ((cmd & PCIM_CMD_MWRICEN) == 0)
782 sc->sc_DMACtrl |= DMAC_MWIDisable;
787 error = bus_setup_intr(dev, sc->sc_res[1], INTR_TYPE_NET | INTR_MPSAFE,
788 NULL, stge_intr, sc, &sc->sc_ih);
791 device_printf(sc->sc_dev, "couldn't set up IRQ\n");
804 stge_detach(device_t dev)
806 struct stge_softc *sc;
809 sc = device_get_softc(dev);
812 #ifdef DEVICE_POLLING
813 if (ifp && ifp->if_capenable & IFCAP_POLLING)
814 ether_poll_deregister(ifp);
816 if (device_is_attached(dev)) {
822 callout_drain(&sc->sc_tick_ch);
823 taskqueue_drain(taskqueue_swi, &sc->sc_link_task);
827 if (sc->sc_miibus != NULL) {
828 device_delete_child(dev, sc->sc_miibus);
829 sc->sc_miibus = NULL;
831 bus_generic_detach(dev);
840 bus_teardown_intr(dev, sc->sc_res[1], sc->sc_ih);
843 bus_release_resources(dev, sc->sc_spec, sc->sc_res);
845 mtx_destroy(&sc->sc_mii_mtx);
846 mtx_destroy(&sc->sc_mtx);
851 struct stge_dmamap_arg {
852 bus_addr_t stge_busaddr;
856 stge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
858 struct stge_dmamap_arg *ctx;
863 ctx = (struct stge_dmamap_arg *)arg;
864 ctx->stge_busaddr = segs[0].ds_addr;
868 stge_dma_alloc(struct stge_softc *sc)
870 struct stge_dmamap_arg ctx;
871 struct stge_txdesc *txd;
872 struct stge_rxdesc *rxd;
875 /* create parent tag. */
876 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),/* parent */
877 1, 0, /* algnmnt, boundary */
878 STGE_DMA_MAXADDR, /* lowaddr */
879 BUS_SPACE_MAXADDR, /* highaddr */
880 NULL, NULL, /* filter, filterarg */
881 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
883 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
885 NULL, NULL, /* lockfunc, lockarg */
886 &sc->sc_cdata.stge_parent_tag);
888 device_printf(sc->sc_dev, "failed to create parent DMA tag\n");
891 /* create tag for Tx ring. */
892 error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
893 STGE_RING_ALIGN, 0, /* algnmnt, boundary */
894 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
895 BUS_SPACE_MAXADDR, /* highaddr */
896 NULL, NULL, /* filter, filterarg */
897 STGE_TX_RING_SZ, /* maxsize */
899 STGE_TX_RING_SZ, /* maxsegsize */
901 NULL, NULL, /* lockfunc, lockarg */
902 &sc->sc_cdata.stge_tx_ring_tag);
904 device_printf(sc->sc_dev,
905 "failed to allocate Tx ring DMA tag\n");
909 /* create tag for Rx ring. */
910 error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
911 STGE_RING_ALIGN, 0, /* algnmnt, boundary */
912 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
913 BUS_SPACE_MAXADDR, /* highaddr */
914 NULL, NULL, /* filter, filterarg */
915 STGE_RX_RING_SZ, /* maxsize */
917 STGE_RX_RING_SZ, /* maxsegsize */
919 NULL, NULL, /* lockfunc, lockarg */
920 &sc->sc_cdata.stge_rx_ring_tag);
922 device_printf(sc->sc_dev,
923 "failed to allocate Rx ring DMA tag\n");
927 /* create tag for Tx buffers. */
928 error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
929 1, 0, /* algnmnt, boundary */
930 BUS_SPACE_MAXADDR, /* lowaddr */
931 BUS_SPACE_MAXADDR, /* highaddr */
932 NULL, NULL, /* filter, filterarg */
933 MCLBYTES * STGE_MAXTXSEGS, /* maxsize */
934 STGE_MAXTXSEGS, /* nsegments */
935 MCLBYTES, /* maxsegsize */
937 NULL, NULL, /* lockfunc, lockarg */
938 &sc->sc_cdata.stge_tx_tag);
940 device_printf(sc->sc_dev, "failed to allocate Tx DMA tag\n");
944 /* create tag for Rx buffers. */
945 error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
946 1, 0, /* algnmnt, boundary */
947 BUS_SPACE_MAXADDR, /* lowaddr */
948 BUS_SPACE_MAXADDR, /* highaddr */
949 NULL, NULL, /* filter, filterarg */
950 MCLBYTES, /* maxsize */
952 MCLBYTES, /* maxsegsize */
954 NULL, NULL, /* lockfunc, lockarg */
955 &sc->sc_cdata.stge_rx_tag);
957 device_printf(sc->sc_dev, "failed to allocate Rx DMA tag\n");
961 /* allocate DMA'able memory and load the DMA map for Tx ring. */
962 error = bus_dmamem_alloc(sc->sc_cdata.stge_tx_ring_tag,
963 (void **)&sc->sc_rdata.stge_tx_ring, BUS_DMA_NOWAIT |
964 BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sc_cdata.stge_tx_ring_map);
966 device_printf(sc->sc_dev,
967 "failed to allocate DMA'able memory for Tx ring\n");
971 ctx.stge_busaddr = 0;
972 error = bus_dmamap_load(sc->sc_cdata.stge_tx_ring_tag,
973 sc->sc_cdata.stge_tx_ring_map, sc->sc_rdata.stge_tx_ring,
974 STGE_TX_RING_SZ, stge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
975 if (error != 0 || ctx.stge_busaddr == 0) {
976 device_printf(sc->sc_dev,
977 "failed to load DMA'able memory for Tx ring\n");
980 sc->sc_rdata.stge_tx_ring_paddr = ctx.stge_busaddr;
982 /* allocate DMA'able memory and load the DMA map for Rx ring. */
983 error = bus_dmamem_alloc(sc->sc_cdata.stge_rx_ring_tag,
984 (void **)&sc->sc_rdata.stge_rx_ring, BUS_DMA_NOWAIT |
985 BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sc_cdata.stge_rx_ring_map);
987 device_printf(sc->sc_dev,
988 "failed to allocate DMA'able memory for Rx ring\n");
992 ctx.stge_busaddr = 0;
993 error = bus_dmamap_load(sc->sc_cdata.stge_rx_ring_tag,
994 sc->sc_cdata.stge_rx_ring_map, sc->sc_rdata.stge_rx_ring,
995 STGE_RX_RING_SZ, stge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
996 if (error != 0 || ctx.stge_busaddr == 0) {
997 device_printf(sc->sc_dev,
998 "failed to load DMA'able memory for Rx ring\n");
1001 sc->sc_rdata.stge_rx_ring_paddr = ctx.stge_busaddr;
1003 /* create DMA maps for Tx buffers. */
1004 for (i = 0; i < STGE_TX_RING_CNT; i++) {
1005 txd = &sc->sc_cdata.stge_txdesc[i];
1008 error = bus_dmamap_create(sc->sc_cdata.stge_tx_tag, 0,
1011 device_printf(sc->sc_dev,
1012 "failed to create Tx dmamap\n");
1016 /* create DMA maps for Rx buffers. */
1017 if ((error = bus_dmamap_create(sc->sc_cdata.stge_rx_tag, 0,
1018 &sc->sc_cdata.stge_rx_sparemap)) != 0) {
1019 device_printf(sc->sc_dev, "failed to create spare Rx dmamap\n");
1022 for (i = 0; i < STGE_RX_RING_CNT; i++) {
1023 rxd = &sc->sc_cdata.stge_rxdesc[i];
1026 error = bus_dmamap_create(sc->sc_cdata.stge_rx_tag, 0,
1029 device_printf(sc->sc_dev,
1030 "failed to create Rx dmamap\n");
1040 stge_dma_free(struct stge_softc *sc)
1042 struct stge_txdesc *txd;
1043 struct stge_rxdesc *rxd;
1047 if (sc->sc_cdata.stge_tx_ring_tag) {
1048 if (sc->sc_cdata.stge_tx_ring_map)
1049 bus_dmamap_unload(sc->sc_cdata.stge_tx_ring_tag,
1050 sc->sc_cdata.stge_tx_ring_map);
1051 if (sc->sc_cdata.stge_tx_ring_map &&
1052 sc->sc_rdata.stge_tx_ring)
1053 bus_dmamem_free(sc->sc_cdata.stge_tx_ring_tag,
1054 sc->sc_rdata.stge_tx_ring,
1055 sc->sc_cdata.stge_tx_ring_map);
1056 sc->sc_rdata.stge_tx_ring = NULL;
1057 sc->sc_cdata.stge_tx_ring_map = 0;
1058 bus_dma_tag_destroy(sc->sc_cdata.stge_tx_ring_tag);
1059 sc->sc_cdata.stge_tx_ring_tag = NULL;
1062 if (sc->sc_cdata.stge_rx_ring_tag) {
1063 if (sc->sc_cdata.stge_rx_ring_map)
1064 bus_dmamap_unload(sc->sc_cdata.stge_rx_ring_tag,
1065 sc->sc_cdata.stge_rx_ring_map);
1066 if (sc->sc_cdata.stge_rx_ring_map &&
1067 sc->sc_rdata.stge_rx_ring)
1068 bus_dmamem_free(sc->sc_cdata.stge_rx_ring_tag,
1069 sc->sc_rdata.stge_rx_ring,
1070 sc->sc_cdata.stge_rx_ring_map);
1071 sc->sc_rdata.stge_rx_ring = NULL;
1072 sc->sc_cdata.stge_rx_ring_map = 0;
1073 bus_dma_tag_destroy(sc->sc_cdata.stge_rx_ring_tag);
1074 sc->sc_cdata.stge_rx_ring_tag = NULL;
1077 if (sc->sc_cdata.stge_tx_tag) {
1078 for (i = 0; i < STGE_TX_RING_CNT; i++) {
1079 txd = &sc->sc_cdata.stge_txdesc[i];
1080 if (txd->tx_dmamap) {
1081 bus_dmamap_destroy(sc->sc_cdata.stge_tx_tag,
1086 bus_dma_tag_destroy(sc->sc_cdata.stge_tx_tag);
1087 sc->sc_cdata.stge_tx_tag = NULL;
1090 if (sc->sc_cdata.stge_rx_tag) {
1091 for (i = 0; i < STGE_RX_RING_CNT; i++) {
1092 rxd = &sc->sc_cdata.stge_rxdesc[i];
1093 if (rxd->rx_dmamap) {
1094 bus_dmamap_destroy(sc->sc_cdata.stge_rx_tag,
1099 if (sc->sc_cdata.stge_rx_sparemap) {
1100 bus_dmamap_destroy(sc->sc_cdata.stge_rx_tag,
1101 sc->sc_cdata.stge_rx_sparemap);
1102 sc->sc_cdata.stge_rx_sparemap = 0;
1104 bus_dma_tag_destroy(sc->sc_cdata.stge_rx_tag);
1105 sc->sc_cdata.stge_rx_tag = NULL;
1108 if (sc->sc_cdata.stge_parent_tag) {
1109 bus_dma_tag_destroy(sc->sc_cdata.stge_parent_tag);
1110 sc->sc_cdata.stge_parent_tag = NULL;
1117 * Make sure the interface is stopped at reboot time.
1120 stge_shutdown(device_t dev)
1123 return (stge_suspend(dev));
1127 stge_setwol(struct stge_softc *sc)
1132 STGE_LOCK_ASSERT(sc);
1135 v = CSR_READ_1(sc, STGE_WakeEvent);
1136 /* Disable all WOL bits. */
1137 v &= ~(WE_WakePktEnable | WE_MagicPktEnable | WE_LinkEventEnable |
1138 WE_WakeOnLanEnable);
1139 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
1140 v |= WE_MagicPktEnable | WE_WakeOnLanEnable;
1141 CSR_WRITE_1(sc, STGE_WakeEvent, v);
1142 /* Reset Tx and prevent transmission. */
1143 CSR_WRITE_4(sc, STGE_AsicCtrl,
1144 CSR_READ_4(sc, STGE_AsicCtrl) | AC_TxReset);
1146 * TC9021 automatically reset link speed to 100Mbps when it's put
1147 * into sleep so there is no need to try to resetting link speed.
1152 stge_suspend(device_t dev)
1154 struct stge_softc *sc;
1156 sc = device_get_softc(dev);
1160 sc->sc_suspended = 1;
1168 stge_resume(device_t dev)
1170 struct stge_softc *sc;
1174 sc = device_get_softc(dev);
1178 * Clear WOL bits, so special frames wouldn't interfere
1179 * normal Rx operation anymore.
1181 v = CSR_READ_1(sc, STGE_WakeEvent);
1182 v &= ~(WE_WakePktEnable | WE_MagicPktEnable | WE_LinkEventEnable |
1183 WE_WakeOnLanEnable);
1184 CSR_WRITE_1(sc, STGE_WakeEvent, v);
1186 if (ifp->if_flags & IFF_UP)
1187 stge_init_locked(sc);
1189 sc->sc_suspended = 0;
1196 stge_dma_wait(struct stge_softc *sc)
1200 for (i = 0; i < STGE_TIMEOUT; i++) {
1202 if ((CSR_READ_4(sc, STGE_DMACtrl) & DMAC_TxDMAInProg) == 0)
1206 if (i == STGE_TIMEOUT)
1207 device_printf(sc->sc_dev, "DMA wait timed out\n");
1211 stge_encap(struct stge_softc *sc, struct mbuf **m_head)
1213 struct stge_txdesc *txd;
1214 struct stge_tfd *tfd;
1216 bus_dma_segment_t txsegs[STGE_MAXTXSEGS];
1217 int error, i, nsegs, si;
1218 uint64_t csum_flags, tfc;
1220 STGE_LOCK_ASSERT(sc);
1222 if ((txd = STAILQ_FIRST(&sc->sc_cdata.stge_txfreeq)) == NULL)
1225 error = bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_tx_tag,
1226 txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1227 if (error == EFBIG) {
1228 m = m_collapse(*m_head, M_DONTWAIT, STGE_MAXTXSEGS);
1235 error = bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_tx_tag,
1236 txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1242 } else if (error != 0)
1252 if ((m->m_pkthdr.csum_flags & STGE_CSUM_FEATURES) != 0) {
1253 if (m->m_pkthdr.csum_flags & CSUM_IP)
1254 csum_flags |= TFD_IPChecksumEnable;
1255 if (m->m_pkthdr.csum_flags & CSUM_TCP)
1256 csum_flags |= TFD_TCPChecksumEnable;
1257 else if (m->m_pkthdr.csum_flags & CSUM_UDP)
1258 csum_flags |= TFD_UDPChecksumEnable;
1261 si = sc->sc_cdata.stge_tx_prod;
1262 tfd = &sc->sc_rdata.stge_tx_ring[si];
1263 for (i = 0; i < nsegs; i++)
1264 tfd->tfd_frags[i].frag_word0 =
1265 htole64(FRAG_ADDR(txsegs[i].ds_addr) |
1266 FRAG_LEN(txsegs[i].ds_len));
1267 sc->sc_cdata.stge_tx_cnt++;
1269 tfc = TFD_FrameId(si) | TFD_WordAlign(TFD_WordAlign_disable) |
1270 TFD_FragCount(nsegs) | csum_flags;
1271 if (sc->sc_cdata.stge_tx_cnt >= STGE_TX_HIWAT)
1272 tfc |= TFD_TxDMAIndicate;
1274 /* Update producer index. */
1275 sc->sc_cdata.stge_tx_prod = (si + 1) % STGE_TX_RING_CNT;
1277 /* Check if we have a VLAN tag to insert. */
1278 if (m->m_flags & M_VLANTAG)
1279 tfc |= (TFD_VLANTagInsert | TFD_VID(m->m_pkthdr.ether_vtag));
1280 tfd->tfd_control = htole64(tfc);
1282 /* Update Tx Queue. */
1283 STAILQ_REMOVE_HEAD(&sc->sc_cdata.stge_txfreeq, tx_q);
1284 STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txbusyq, txd, tx_q);
1287 /* Sync descriptors. */
1288 bus_dmamap_sync(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap,
1289 BUS_DMASYNC_PREWRITE);
1290 bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1291 sc->sc_cdata.stge_tx_ring_map,
1292 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1298 * stge_start: [ifnet interface function]
1300 * Start packet transmission on the interface.
1303 stge_start(struct ifnet *ifp)
1305 struct stge_softc *sc;
1309 stge_start_locked(ifp);
1314 stge_start_locked(struct ifnet *ifp)
1316 struct stge_softc *sc;
1317 struct mbuf *m_head;
1322 STGE_LOCK_ASSERT(sc);
1324 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
1325 IFF_DRV_RUNNING || sc->sc_link == 0)
1328 for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
1329 if (sc->sc_cdata.stge_tx_cnt >= STGE_TX_HIWAT) {
1330 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1334 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1338 * Pack the data into the transmit ring. If we
1339 * don't have room, set the OACTIVE flag and wait
1340 * for the NIC to drain the ring.
1342 if (stge_encap(sc, &m_head)) {
1345 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1346 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1352 * If there's a BPF listener, bounce a copy of this frame
1355 ETHER_BPF_MTAP(ifp, m_head);
1360 CSR_WRITE_4(sc, STGE_DMACtrl, DMAC_TxDMAPollNow);
1362 /* Set a timeout in case the chip goes out to lunch. */
1363 sc->sc_watchdog_timer = 5;
1370 * Watchdog timer handler.
1373 stge_watchdog(struct stge_softc *sc)
1377 STGE_LOCK_ASSERT(sc);
1379 if (sc->sc_watchdog_timer == 0 || --sc->sc_watchdog_timer)
1383 if_printf(sc->sc_ifp, "device timeout\n");
1385 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1386 stge_init_locked(sc);
1387 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1388 stge_start_locked(ifp);
1392 * stge_ioctl: [ifnet interface function]
1394 * Handle control requests from the operator.
1397 stge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1399 struct stge_softc *sc;
1401 struct mii_data *mii;
1405 ifr = (struct ifreq *)data;
1409 if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > STGE_JUMBO_MTU)
1411 else if (ifp->if_mtu != ifr->ifr_mtu) {
1412 ifp->if_mtu = ifr->ifr_mtu;
1414 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1415 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1416 stge_init_locked(sc);
1423 if ((ifp->if_flags & IFF_UP) != 0) {
1424 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1425 if (((ifp->if_flags ^ sc->sc_if_flags)
1426 & IFF_PROMISC) != 0)
1427 stge_set_filter(sc);
1429 if (sc->sc_detach == 0)
1430 stge_init_locked(sc);
1433 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1436 sc->sc_if_flags = ifp->if_flags;
1442 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1448 mii = device_get_softc(sc->sc_miibus);
1449 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1452 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1453 #ifdef DEVICE_POLLING
1454 if ((mask & IFCAP_POLLING) != 0) {
1455 if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
1456 error = ether_poll_register(stge_poll, ifp);
1460 CSR_WRITE_2(sc, STGE_IntEnable, 0);
1461 ifp->if_capenable |= IFCAP_POLLING;
1464 error = ether_poll_deregister(ifp);
1468 CSR_WRITE_2(sc, STGE_IntEnable,
1470 ifp->if_capenable &= ~IFCAP_POLLING;
1475 if ((mask & IFCAP_HWCSUM) != 0) {
1476 ifp->if_capenable ^= IFCAP_HWCSUM;
1477 if ((IFCAP_HWCSUM & ifp->if_capenable) != 0 &&
1478 (IFCAP_HWCSUM & ifp->if_capabilities) != 0)
1479 ifp->if_hwassist = STGE_CSUM_FEATURES;
1481 ifp->if_hwassist = 0;
1483 if ((mask & IFCAP_WOL) != 0 &&
1484 (ifp->if_capabilities & IFCAP_WOL) != 0) {
1485 if ((mask & IFCAP_WOL_MAGIC) != 0)
1486 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
1488 if ((mask & IFCAP_VLAN_HWTAGGING) != 0) {
1489 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1490 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1492 stge_vlan_setup(sc);
1496 VLAN_CAPABILITIES(ifp);
1499 error = ether_ioctl(ifp, cmd, data);
1507 stge_link_task(void *arg, int pending)
1509 struct stge_softc *sc;
1510 struct mii_data *mii;
1514 sc = (struct stge_softc *)arg;
1517 mii = device_get_softc(sc->sc_miibus);
1518 if (mii->mii_media_status & IFM_ACTIVE) {
1519 if (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
1525 if (((mii->mii_media_active & IFM_GMASK) & IFM_FDX) != 0)
1526 sc->sc_MACCtrl |= MC_DuplexSelect;
1527 if (((mii->mii_media_active & IFM_GMASK) & IFM_ETH_RXPAUSE) != 0)
1528 sc->sc_MACCtrl |= MC_RxFlowControlEnable;
1529 if (((mii->mii_media_active & IFM_GMASK) & IFM_ETH_TXPAUSE) != 0)
1530 sc->sc_MACCtrl |= MC_TxFlowControlEnable;
1532 * Update STGE_MACCtrl register depending on link status.
1533 * (duplex, flow control etc)
1535 v = ac = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
1536 v &= ~(MC_DuplexSelect|MC_RxFlowControlEnable|MC_TxFlowControlEnable);
1537 v |= sc->sc_MACCtrl;
1538 CSR_WRITE_4(sc, STGE_MACCtrl, v);
1539 if (((ac ^ sc->sc_MACCtrl) & MC_DuplexSelect) != 0) {
1540 /* Duplex setting changed, reset Tx/Rx functions. */
1541 ac = CSR_READ_4(sc, STGE_AsicCtrl);
1542 ac |= AC_TxReset | AC_RxReset;
1543 CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1544 for (i = 0; i < STGE_TIMEOUT; i++) {
1546 if ((CSR_READ_4(sc, STGE_AsicCtrl) & AC_ResetBusy) == 0)
1549 if (i == STGE_TIMEOUT)
1550 device_printf(sc->sc_dev, "reset failed to complete\n");
1556 stge_tx_error(struct stge_softc *sc)
1562 txstat = CSR_READ_4(sc, STGE_TxStatus);
1563 if ((txstat & TS_TxComplete) == 0)
1566 if ((txstat & TS_TxUnderrun) != 0) {
1569 * There should be a more better way to recover
1570 * from Tx underrun instead of a full reset.
1572 if (sc->sc_nerr++ < STGE_MAXERR)
1573 device_printf(sc->sc_dev, "Tx underrun, "
1575 if (sc->sc_nerr == STGE_MAXERR)
1576 device_printf(sc->sc_dev, "too many errors; "
1577 "not reporting any more\n");
1581 /* Maximum/Late collisions, Re-enable Tx MAC. */
1582 if ((txstat & (TS_MaxCollisions|TS_LateCollision)) != 0)
1583 CSR_WRITE_4(sc, STGE_MACCtrl,
1584 (CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK) |
1594 * Interrupt service routine.
1597 stge_intr(void *arg)
1599 struct stge_softc *sc;
1604 sc = (struct stge_softc *)arg;
1609 #ifdef DEVICE_POLLING
1610 if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1613 status = CSR_READ_2(sc, STGE_IntStatus);
1614 if (sc->sc_suspended || (status & IS_InterruptStatus) == 0)
1617 /* Disable interrupts. */
1618 for (reinit = 0;;) {
1619 status = CSR_READ_2(sc, STGE_IntStatusAck);
1620 status &= sc->sc_IntEnable;
1623 /* Host interface errors. */
1624 if ((status & IS_HostError) != 0) {
1625 device_printf(sc->sc_dev,
1626 "Host interface error, resetting...\n");
1631 /* Receive interrupts. */
1632 if ((status & IS_RxDMAComplete) != 0) {
1634 if ((status & IS_RFDListEnd) != 0)
1635 CSR_WRITE_4(sc, STGE_DMACtrl,
1639 /* Transmit interrupts. */
1640 if ((status & (IS_TxDMAComplete | IS_TxComplete)) != 0)
1643 /* Transmission errors.*/
1644 if ((status & IS_TxComplete) != 0) {
1645 if ((reinit = stge_tx_error(sc)) != 0)
1652 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1653 stge_init_locked(sc);
1656 /* Re-enable interrupts. */
1657 CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
1659 /* Try to get more packets going. */
1660 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1661 stge_start_locked(ifp);
1670 * Helper; handle transmit interrupts.
1673 stge_txeof(struct stge_softc *sc)
1676 struct stge_txdesc *txd;
1680 STGE_LOCK_ASSERT(sc);
1684 txd = STAILQ_FIRST(&sc->sc_cdata.stge_txbusyq);
1687 bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1688 sc->sc_cdata.stge_tx_ring_map, BUS_DMASYNC_POSTREAD);
1691 * Go through our Tx list and free mbufs for those
1692 * frames which have been transmitted.
1694 for (cons = sc->sc_cdata.stge_tx_cons;;
1695 cons = (cons + 1) % STGE_TX_RING_CNT) {
1696 if (sc->sc_cdata.stge_tx_cnt <= 0)
1698 control = le64toh(sc->sc_rdata.stge_tx_ring[cons].tfd_control);
1699 if ((control & TFD_TFDDone) == 0)
1701 sc->sc_cdata.stge_tx_cnt--;
1702 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1704 bus_dmamap_sync(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap,
1705 BUS_DMASYNC_POSTWRITE);
1706 bus_dmamap_unload(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap);
1708 /* Output counter is updated with statistics register */
1711 STAILQ_REMOVE_HEAD(&sc->sc_cdata.stge_txbusyq, tx_q);
1712 STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txfreeq, txd, tx_q);
1713 txd = STAILQ_FIRST(&sc->sc_cdata.stge_txbusyq);
1715 sc->sc_cdata.stge_tx_cons = cons;
1716 if (sc->sc_cdata.stge_tx_cnt == 0)
1717 sc->sc_watchdog_timer = 0;
1719 bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1720 sc->sc_cdata.stge_tx_ring_map,
1721 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1724 static __inline void
1725 stge_discard_rxbuf(struct stge_softc *sc, int idx)
1727 struct stge_rfd *rfd;
1729 rfd = &sc->sc_rdata.stge_rx_ring[idx];
1730 rfd->rfd_status = 0;
1733 #ifndef __NO_STRICT_ALIGNMENT
1735 * It seems that TC9021's DMA engine has alignment restrictions in
1736 * DMA scatter operations. The first DMA segment has no address
1737 * alignment restrictins but the rest should be aligned on 4(?) bytes
1738 * boundary. Otherwise it would corrupt random memory. Since we don't
1739 * know which one is used for the first segment in advance we simply
1740 * don't align at all.
1741 * To avoid copying over an entire frame to align, we allocate a new
1742 * mbuf and copy ethernet header to the new mbuf. The new mbuf is
1743 * prepended into the existing mbuf chain.
1745 static __inline struct mbuf *
1746 stge_fixup_rx(struct stge_softc *sc, struct mbuf *m)
1751 if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
1752 bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
1753 m->m_data += ETHER_HDR_LEN;
1756 MGETHDR(n, M_DONTWAIT, MT_DATA);
1758 bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
1759 m->m_data += ETHER_HDR_LEN;
1760 m->m_len -= ETHER_HDR_LEN;
1761 n->m_len = ETHER_HDR_LEN;
1762 M_MOVE_PKTHDR(n, m);
1775 * Helper; handle receive interrupts.
1778 stge_rxeof(struct stge_softc *sc)
1781 struct stge_rxdesc *rxd;
1782 struct mbuf *mp, *m;
1785 int cons, prog, rx_npkts;
1787 STGE_LOCK_ASSERT(sc);
1792 bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
1793 sc->sc_cdata.stge_rx_ring_map, BUS_DMASYNC_POSTREAD);
1796 for (cons = sc->sc_cdata.stge_rx_cons; prog < STGE_RX_RING_CNT;
1797 prog++, cons = (cons + 1) % STGE_RX_RING_CNT) {
1798 status64 = le64toh(sc->sc_rdata.stge_rx_ring[cons].rfd_status);
1799 status = RFD_RxStatus(status64);
1800 if ((status & RFD_RFDDone) == 0)
1802 #ifdef DEVICE_POLLING
1803 if (ifp->if_capenable & IFCAP_POLLING) {
1804 if (sc->sc_cdata.stge_rxcycles <= 0)
1806 sc->sc_cdata.stge_rxcycles--;
1810 rxd = &sc->sc_cdata.stge_rxdesc[cons];
1814 * If the packet had an error, drop it. Note we count
1815 * the error later in the periodic stats update.
1817 if ((status & RFD_FrameEnd) != 0 && (status &
1818 (RFD_RxFIFOOverrun | RFD_RxRuntFrame |
1819 RFD_RxAlignmentError | RFD_RxFCSError |
1820 RFD_RxLengthError)) != 0) {
1821 stge_discard_rxbuf(sc, cons);
1822 if (sc->sc_cdata.stge_rxhead != NULL) {
1823 m_freem(sc->sc_cdata.stge_rxhead);
1824 STGE_RXCHAIN_RESET(sc);
1829 * Add a new receive buffer to the ring.
1831 if (stge_newbuf(sc, cons) != 0) {
1833 stge_discard_rxbuf(sc, cons);
1834 if (sc->sc_cdata.stge_rxhead != NULL) {
1835 m_freem(sc->sc_cdata.stge_rxhead);
1836 STGE_RXCHAIN_RESET(sc);
1841 if ((status & RFD_FrameEnd) != 0)
1842 mp->m_len = RFD_RxDMAFrameLen(status) -
1843 sc->sc_cdata.stge_rxlen;
1844 sc->sc_cdata.stge_rxlen += mp->m_len;
1847 if (sc->sc_cdata.stge_rxhead == NULL) {
1848 sc->sc_cdata.stge_rxhead = mp;
1849 sc->sc_cdata.stge_rxtail = mp;
1851 mp->m_flags &= ~M_PKTHDR;
1852 sc->sc_cdata.stge_rxtail->m_next = mp;
1853 sc->sc_cdata.stge_rxtail = mp;
1856 if ((status & RFD_FrameEnd) != 0) {
1857 m = sc->sc_cdata.stge_rxhead;
1858 m->m_pkthdr.rcvif = ifp;
1859 m->m_pkthdr.len = sc->sc_cdata.stge_rxlen;
1861 if (m->m_pkthdr.len > sc->sc_if_framesize) {
1863 STGE_RXCHAIN_RESET(sc);
1867 * Set the incoming checksum information for
1870 if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1871 if ((status & RFD_IPDetected) != 0) {
1872 m->m_pkthdr.csum_flags |=
1874 if ((status & RFD_IPError) == 0)
1875 m->m_pkthdr.csum_flags |=
1878 if (((status & RFD_TCPDetected) != 0 &&
1879 (status & RFD_TCPError) == 0) ||
1880 ((status & RFD_UDPDetected) != 0 &&
1881 (status & RFD_UDPError) == 0)) {
1882 m->m_pkthdr.csum_flags |=
1883 (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1884 m->m_pkthdr.csum_data = 0xffff;
1888 #ifndef __NO_STRICT_ALIGNMENT
1889 if (sc->sc_if_framesize > (MCLBYTES - ETHER_ALIGN)) {
1890 if ((m = stge_fixup_rx(sc, m)) == NULL) {
1891 STGE_RXCHAIN_RESET(sc);
1896 /* Check for VLAN tagged packets. */
1897 if ((status & RFD_VLANDetected) != 0 &&
1898 (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1899 m->m_pkthdr.ether_vtag = RFD_TCI(status64);
1900 m->m_flags |= M_VLANTAG;
1905 (*ifp->if_input)(ifp, m);
1909 STGE_RXCHAIN_RESET(sc);
1914 /* Update the consumer index. */
1915 sc->sc_cdata.stge_rx_cons = cons;
1916 bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
1917 sc->sc_cdata.stge_rx_ring_map,
1918 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1923 #ifdef DEVICE_POLLING
1925 stge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1927 struct stge_softc *sc;
1934 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1939 sc->sc_cdata.stge_rxcycles = count;
1940 rx_npkts = stge_rxeof(sc);
1943 if (cmd == POLL_AND_CHECK_STATUS) {
1944 status = CSR_READ_2(sc, STGE_IntStatus);
1945 status &= sc->sc_IntEnable;
1947 if ((status & IS_HostError) != 0) {
1948 device_printf(sc->sc_dev,
1949 "Host interface error, resetting...\n");
1950 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1951 stge_init_locked(sc);
1953 if ((status & IS_TxComplete) != 0) {
1954 if (stge_tx_error(sc) != 0) {
1955 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1956 stge_init_locked(sc);
1963 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1964 stge_start_locked(ifp);
1969 #endif /* DEVICE_POLLING */
1974 * One second timer, used to tick the MII.
1977 stge_tick(void *arg)
1979 struct stge_softc *sc;
1980 struct mii_data *mii;
1982 sc = (struct stge_softc *)arg;
1984 STGE_LOCK_ASSERT(sc);
1986 mii = device_get_softc(sc->sc_miibus);
1989 /* Update statistics counters. */
1990 stge_stats_update(sc);
1993 * Relcaim any pending Tx descriptors to release mbufs in a
1994 * timely manner as we don't generate Tx completion interrupts
1995 * for every frame. This limits the delay to a maximum of one
1998 if (sc->sc_cdata.stge_tx_cnt != 0)
2003 callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
2007 * stge_stats_update:
2009 * Read the TC9021 statistics counters.
2012 stge_stats_update(struct stge_softc *sc)
2016 STGE_LOCK_ASSERT(sc);
2020 CSR_READ_4(sc,STGE_OctetRcvOk);
2022 ifp->if_ipackets += CSR_READ_4(sc, STGE_FramesRcvdOk);
2024 ifp->if_ierrors += CSR_READ_2(sc, STGE_FramesLostRxErrors);
2026 CSR_READ_4(sc, STGE_OctetXmtdOk);
2028 ifp->if_opackets += CSR_READ_4(sc, STGE_FramesXmtdOk);
2030 ifp->if_collisions +=
2031 CSR_READ_4(sc, STGE_LateCollisions) +
2032 CSR_READ_4(sc, STGE_MultiColFrames) +
2033 CSR_READ_4(sc, STGE_SingleColFrames);
2036 CSR_READ_2(sc, STGE_FramesAbortXSColls) +
2037 CSR_READ_2(sc, STGE_FramesWEXDeferal);
2043 * Perform a soft reset on the TC9021.
2046 stge_reset(struct stge_softc *sc, uint32_t how)
2052 STGE_LOCK_ASSERT(sc);
2055 ac = CSR_READ_4(sc, STGE_AsicCtrl);
2058 ac |= AC_TxReset | AC_FIFO;
2062 ac |= AC_RxReset | AC_FIFO;
2065 case STGE_RESET_FULL:
2068 * Only assert RstOut if we're fiber. We need GMII clocks
2069 * to be present in order for the reset to complete on fiber
2072 ac |= AC_GlobalReset | AC_RxReset | AC_TxReset |
2073 AC_DMA | AC_FIFO | AC_Network | AC_Host | AC_AutoInit |
2074 (sc->sc_usefiber ? AC_RstOut : 0);
2078 CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
2080 /* Account for reset problem at 10Mbps. */
2083 for (i = 0; i < STGE_TIMEOUT; i++) {
2084 if ((CSR_READ_4(sc, STGE_AsicCtrl) & AC_ResetBusy) == 0)
2089 if (i == STGE_TIMEOUT)
2090 device_printf(sc->sc_dev, "reset failed to complete\n");
2092 /* Set LED, from Linux IPG driver. */
2093 ac = CSR_READ_4(sc, STGE_AsicCtrl);
2094 ac &= ~(AC_LEDMode | AC_LEDSpeed | AC_LEDModeBit1);
2095 if ((sc->sc_led & 0x01) != 0)
2097 if ((sc->sc_led & 0x03) != 0)
2098 ac |= AC_LEDModeBit1;
2099 if ((sc->sc_led & 0x08) != 0)
2101 CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
2103 /* Set PHY, from Linux IPG driver */
2104 v = CSR_READ_1(sc, STGE_PhySet);
2105 v &= ~(PS_MemLenb9b | PS_MemLen | PS_NonCompdet);
2106 v |= ((sc->sc_led & 0x70) >> 4);
2107 CSR_WRITE_1(sc, STGE_PhySet, v);
2111 * stge_init: [ ifnet interface function ]
2113 * Initialize the interface.
2116 stge_init(void *xsc)
2118 struct stge_softc *sc;
2120 sc = (struct stge_softc *)xsc;
2122 stge_init_locked(sc);
2127 stge_init_locked(struct stge_softc *sc)
2130 struct mii_data *mii;
2135 STGE_LOCK_ASSERT(sc);
2138 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2140 mii = device_get_softc(sc->sc_miibus);
2143 * Cancel any pending I/O.
2148 * Reset the chip to a known state.
2150 stge_reset(sc, STGE_RESET_FULL);
2152 /* Init descriptors. */
2153 error = stge_init_rx_ring(sc);
2155 device_printf(sc->sc_dev,
2156 "initialization failed: no memory for rx buffers\n");
2160 stge_init_tx_ring(sc);
2162 /* Set the station address. */
2163 bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2164 CSR_WRITE_2(sc, STGE_StationAddress0, htole16(eaddr[0]));
2165 CSR_WRITE_2(sc, STGE_StationAddress1, htole16(eaddr[1]));
2166 CSR_WRITE_2(sc, STGE_StationAddress2, htole16(eaddr[2]));
2169 * Set the statistics masks. Disable all the RMON stats,
2170 * and disable selected stats in the non-RMON stats registers.
2172 CSR_WRITE_4(sc, STGE_RMONStatisticsMask, 0xffffffff);
2173 CSR_WRITE_4(sc, STGE_StatisticsMask,
2174 (1U << 1) | (1U << 2) | (1U << 3) | (1U << 4) | (1U << 5) |
2175 (1U << 6) | (1U << 7) | (1U << 8) | (1U << 9) | (1U << 10) |
2176 (1U << 13) | (1U << 14) | (1U << 15) | (1U << 19) | (1U << 20) |
2179 /* Set up the receive filter. */
2180 stge_set_filter(sc);
2181 /* Program multicast filter. */
2185 * Give the transmit and receive ring to the chip.
2187 CSR_WRITE_4(sc, STGE_TFDListPtrHi,
2188 STGE_ADDR_HI(STGE_TX_RING_ADDR(sc, 0)));
2189 CSR_WRITE_4(sc, STGE_TFDListPtrLo,
2190 STGE_ADDR_LO(STGE_TX_RING_ADDR(sc, 0)));
2192 CSR_WRITE_4(sc, STGE_RFDListPtrHi,
2193 STGE_ADDR_HI(STGE_RX_RING_ADDR(sc, 0)));
2194 CSR_WRITE_4(sc, STGE_RFDListPtrLo,
2195 STGE_ADDR_LO(STGE_RX_RING_ADDR(sc, 0)));
2198 * Initialize the Tx auto-poll period. It's OK to make this number
2199 * large (255 is the max, but we use 127) -- we explicitly kick the
2200 * transmit engine when there's actually a packet.
2202 CSR_WRITE_1(sc, STGE_TxDMAPollPeriod, 127);
2204 /* ..and the Rx auto-poll period. */
2205 CSR_WRITE_1(sc, STGE_RxDMAPollPeriod, 1);
2207 /* Initialize the Tx start threshold. */
2208 CSR_WRITE_2(sc, STGE_TxStartThresh, sc->sc_txthresh);
2210 /* Rx DMA thresholds, from Linux */
2211 CSR_WRITE_1(sc, STGE_RxDMABurstThresh, 0x30);
2212 CSR_WRITE_1(sc, STGE_RxDMAUrgentThresh, 0x30);
2214 /* Rx early threhold, from Linux */
2215 CSR_WRITE_2(sc, STGE_RxEarlyThresh, 0x7ff);
2217 /* Tx DMA thresholds, from Linux */
2218 CSR_WRITE_1(sc, STGE_TxDMABurstThresh, 0x30);
2219 CSR_WRITE_1(sc, STGE_TxDMAUrgentThresh, 0x04);
2222 * Initialize the Rx DMA interrupt control register. We
2223 * request an interrupt after every incoming packet, but
2224 * defer it for sc_rxint_dmawait us. When the number of
2225 * interrupts pending reaches STGE_RXINT_NFRAME, we stop
2226 * deferring the interrupt, and signal it immediately.
2228 CSR_WRITE_4(sc, STGE_RxDMAIntCtrl,
2229 RDIC_RxFrameCount(sc->sc_rxint_nframe) |
2230 RDIC_RxDMAWaitTime(STGE_RXINT_USECS2TICK(sc->sc_rxint_dmawait)));
2233 * Initialize the interrupt mask.
2235 sc->sc_IntEnable = IS_HostError | IS_TxComplete |
2236 IS_TxDMAComplete | IS_RxDMAComplete | IS_RFDListEnd;
2237 #ifdef DEVICE_POLLING
2238 /* Disable interrupts if we are polling. */
2239 if ((ifp->if_capenable & IFCAP_POLLING) != 0)
2240 CSR_WRITE_2(sc, STGE_IntEnable, 0);
2243 CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
2246 * Configure the DMA engine.
2247 * XXX Should auto-tune TxBurstLimit.
2249 CSR_WRITE_4(sc, STGE_DMACtrl, sc->sc_DMACtrl | DMAC_TxBurstLimit(3));
2252 * Send a PAUSE frame when we reach 29,696 bytes in the Rx
2253 * FIFO, and send an un-PAUSE frame when we reach 3056 bytes
2256 CSR_WRITE_2(sc, STGE_FlowOnTresh, 29696 / 16);
2257 CSR_WRITE_2(sc, STGE_FlowOffThresh, 3056 / 16);
2260 * Set the maximum frame size.
2262 sc->sc_if_framesize = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
2263 CSR_WRITE_2(sc, STGE_MaxFrameSize, sc->sc_if_framesize);
2266 * Initialize MacCtrl -- do it before setting the media,
2267 * as setting the media will actually program the register.
2269 * Note: We have to poke the IFS value before poking
2272 /* Tx/Rx MAC should be disabled before programming IFS.*/
2273 CSR_WRITE_4(sc, STGE_MACCtrl, MC_IFSSelect(MC_IFS96bit));
2275 stge_vlan_setup(sc);
2277 if (sc->sc_rev >= 6) { /* >= B.2 */
2278 /* Multi-frag frame bug work-around. */
2279 CSR_WRITE_2(sc, STGE_DebugCtrl,
2280 CSR_READ_2(sc, STGE_DebugCtrl) | 0x0200);
2282 /* Tx Poll Now bug work-around. */
2283 CSR_WRITE_2(sc, STGE_DebugCtrl,
2284 CSR_READ_2(sc, STGE_DebugCtrl) | 0x0010);
2285 /* Tx Poll Now bug work-around. */
2286 CSR_WRITE_2(sc, STGE_DebugCtrl,
2287 CSR_READ_2(sc, STGE_DebugCtrl) | 0x0020);
2290 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2291 v |= MC_StatisticsEnable | MC_TxEnable | MC_RxEnable;
2292 CSR_WRITE_4(sc, STGE_MACCtrl, v);
2294 * It seems that transmitting frames without checking the state of
2295 * Rx/Tx MAC wedge the hardware.
2302 * Set the current media.
2307 * Start the one second MII clock.
2309 callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
2314 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2315 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2319 device_printf(sc->sc_dev, "interface not running\n");
2323 stge_vlan_setup(struct stge_softc *sc)
2330 * The NIC always copy a VLAN tag regardless of STGE_MACCtrl
2331 * MC_AutoVLANuntagging bit.
2332 * MC_AutoVLANtagging bit selects which VLAN source to use
2333 * between STGE_VLANTag and TFC. However TFC TFD_VLANTagInsert
2334 * bit has priority over MC_AutoVLANtagging bit. So we always
2335 * use TFC instead of STGE_VLANTag register.
2337 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2338 if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
2339 v |= MC_AutoVLANuntagging;
2341 v &= ~MC_AutoVLANuntagging;
2342 CSR_WRITE_4(sc, STGE_MACCtrl, v);
2346 * Stop transmission on the interface.
2349 stge_stop(struct stge_softc *sc)
2352 struct stge_txdesc *txd;
2353 struct stge_rxdesc *rxd;
2357 STGE_LOCK_ASSERT(sc);
2359 * Stop the one second clock.
2361 callout_stop(&sc->sc_tick_ch);
2362 sc->sc_watchdog_timer = 0;
2365 * Disable interrupts.
2367 CSR_WRITE_2(sc, STGE_IntEnable, 0);
2370 * Stop receiver, transmitter, and stats update.
2374 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2375 v |= MC_StatisticsDisable;
2376 CSR_WRITE_4(sc, STGE_MACCtrl, v);
2379 * Stop the transmit and receive DMA.
2382 CSR_WRITE_4(sc, STGE_TFDListPtrHi, 0);
2383 CSR_WRITE_4(sc, STGE_TFDListPtrLo, 0);
2384 CSR_WRITE_4(sc, STGE_RFDListPtrHi, 0);
2385 CSR_WRITE_4(sc, STGE_RFDListPtrLo, 0);
2388 * Free RX and TX mbufs still in the queues.
2390 for (i = 0; i < STGE_RX_RING_CNT; i++) {
2391 rxd = &sc->sc_cdata.stge_rxdesc[i];
2392 if (rxd->rx_m != NULL) {
2393 bus_dmamap_sync(sc->sc_cdata.stge_rx_tag,
2394 rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2395 bus_dmamap_unload(sc->sc_cdata.stge_rx_tag,
2401 for (i = 0; i < STGE_TX_RING_CNT; i++) {
2402 txd = &sc->sc_cdata.stge_txdesc[i];
2403 if (txd->tx_m != NULL) {
2404 bus_dmamap_sync(sc->sc_cdata.stge_tx_tag,
2405 txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2406 bus_dmamap_unload(sc->sc_cdata.stge_tx_tag,
2414 * Mark the interface down and cancel the watchdog timer.
2417 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2422 stge_start_tx(struct stge_softc *sc)
2427 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2428 if ((v & MC_TxEnabled) != 0)
2431 CSR_WRITE_4(sc, STGE_MACCtrl, v);
2432 CSR_WRITE_1(sc, STGE_TxDMAPollPeriod, 127);
2433 for (i = STGE_TIMEOUT; i > 0; i--) {
2435 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2436 if ((v & MC_TxEnabled) != 0)
2440 device_printf(sc->sc_dev, "Starting Tx MAC timed out\n");
2444 stge_start_rx(struct stge_softc *sc)
2449 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2450 if ((v & MC_RxEnabled) != 0)
2453 CSR_WRITE_4(sc, STGE_MACCtrl, v);
2454 CSR_WRITE_1(sc, STGE_RxDMAPollPeriod, 1);
2455 for (i = STGE_TIMEOUT; i > 0; i--) {
2457 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2458 if ((v & MC_RxEnabled) != 0)
2462 device_printf(sc->sc_dev, "Starting Rx MAC timed out\n");
2466 stge_stop_tx(struct stge_softc *sc)
2471 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2472 if ((v & MC_TxEnabled) == 0)
2475 CSR_WRITE_4(sc, STGE_MACCtrl, v);
2476 for (i = STGE_TIMEOUT; i > 0; i--) {
2478 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2479 if ((v & MC_TxEnabled) == 0)
2483 device_printf(sc->sc_dev, "Stopping Tx MAC timed out\n");
2487 stge_stop_rx(struct stge_softc *sc)
2492 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2493 if ((v & MC_RxEnabled) == 0)
2496 CSR_WRITE_4(sc, STGE_MACCtrl, v);
2497 for (i = STGE_TIMEOUT; i > 0; i--) {
2499 v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2500 if ((v & MC_RxEnabled) == 0)
2504 device_printf(sc->sc_dev, "Stopping Rx MAC timed out\n");
2508 stge_init_tx_ring(struct stge_softc *sc)
2510 struct stge_ring_data *rd;
2511 struct stge_txdesc *txd;
2515 STAILQ_INIT(&sc->sc_cdata.stge_txfreeq);
2516 STAILQ_INIT(&sc->sc_cdata.stge_txbusyq);
2518 sc->sc_cdata.stge_tx_prod = 0;
2519 sc->sc_cdata.stge_tx_cons = 0;
2520 sc->sc_cdata.stge_tx_cnt = 0;
2523 bzero(rd->stge_tx_ring, STGE_TX_RING_SZ);
2524 for (i = 0; i < STGE_TX_RING_CNT; i++) {
2525 if (i == (STGE_TX_RING_CNT - 1))
2526 addr = STGE_TX_RING_ADDR(sc, 0);
2528 addr = STGE_TX_RING_ADDR(sc, i + 1);
2529 rd->stge_tx_ring[i].tfd_next = htole64(addr);
2530 rd->stge_tx_ring[i].tfd_control = htole64(TFD_TFDDone);
2531 txd = &sc->sc_cdata.stge_txdesc[i];
2532 STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txfreeq, txd, tx_q);
2535 bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
2536 sc->sc_cdata.stge_tx_ring_map,
2537 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2542 stge_init_rx_ring(struct stge_softc *sc)
2544 struct stge_ring_data *rd;
2548 sc->sc_cdata.stge_rx_cons = 0;
2549 STGE_RXCHAIN_RESET(sc);
2552 bzero(rd->stge_rx_ring, STGE_RX_RING_SZ);
2553 for (i = 0; i < STGE_RX_RING_CNT; i++) {
2554 if (stge_newbuf(sc, i) != 0)
2556 if (i == (STGE_RX_RING_CNT - 1))
2557 addr = STGE_RX_RING_ADDR(sc, 0);
2559 addr = STGE_RX_RING_ADDR(sc, i + 1);
2560 rd->stge_rx_ring[i].rfd_next = htole64(addr);
2561 rd->stge_rx_ring[i].rfd_status = 0;
2564 bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
2565 sc->sc_cdata.stge_rx_ring_map,
2566 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2574 * Add a receive buffer to the indicated descriptor.
2577 stge_newbuf(struct stge_softc *sc, int idx)
2579 struct stge_rxdesc *rxd;
2580 struct stge_rfd *rfd;
2582 bus_dma_segment_t segs[1];
2586 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
2589 m->m_len = m->m_pkthdr.len = MCLBYTES;
2591 * The hardware requires 4bytes aligned DMA address when JUMBO
2594 if (sc->sc_if_framesize <= (MCLBYTES - ETHER_ALIGN))
2595 m_adj(m, ETHER_ALIGN);
2597 if (bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_rx_tag,
2598 sc->sc_cdata.stge_rx_sparemap, m, segs, &nsegs, 0) != 0) {
2602 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
2604 rxd = &sc->sc_cdata.stge_rxdesc[idx];
2605 if (rxd->rx_m != NULL) {
2606 bus_dmamap_sync(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap,
2607 BUS_DMASYNC_POSTREAD);
2608 bus_dmamap_unload(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap);
2610 map = rxd->rx_dmamap;
2611 rxd->rx_dmamap = sc->sc_cdata.stge_rx_sparemap;
2612 sc->sc_cdata.stge_rx_sparemap = map;
2613 bus_dmamap_sync(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap,
2614 BUS_DMASYNC_PREREAD);
2617 rfd = &sc->sc_rdata.stge_rx_ring[idx];
2618 rfd->rfd_frag.frag_word0 =
2619 htole64(FRAG_ADDR(segs[0].ds_addr) | FRAG_LEN(segs[0].ds_len));
2620 rfd->rfd_status = 0;
2628 * Set up the receive filter.
2631 stge_set_filter(struct stge_softc *sc)
2636 STGE_LOCK_ASSERT(sc);
2640 mode = CSR_READ_2(sc, STGE_ReceiveMode);
2641 mode |= RM_ReceiveUnicast;
2642 if ((ifp->if_flags & IFF_BROADCAST) != 0)
2643 mode |= RM_ReceiveBroadcast;
2645 mode &= ~RM_ReceiveBroadcast;
2646 if ((ifp->if_flags & IFF_PROMISC) != 0)
2647 mode |= RM_ReceiveAllFrames;
2649 mode &= ~RM_ReceiveAllFrames;
2651 CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2655 stge_set_multi(struct stge_softc *sc)
2658 struct ifmultiaddr *ifma;
2664 STGE_LOCK_ASSERT(sc);
2668 mode = CSR_READ_2(sc, STGE_ReceiveMode);
2669 if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
2670 if ((ifp->if_flags & IFF_PROMISC) != 0)
2671 mode |= RM_ReceiveAllFrames;
2672 else if ((ifp->if_flags & IFF_ALLMULTI) != 0)
2673 mode |= RM_ReceiveMulticast;
2674 CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2678 /* clear existing filters. */
2679 CSR_WRITE_4(sc, STGE_HashTable0, 0);
2680 CSR_WRITE_4(sc, STGE_HashTable1, 0);
2683 * Set up the multicast address filter by passing all multicast
2684 * addresses through a CRC generator, and then using the low-order
2685 * 6 bits as an index into the 64 bit multicast hash table. The
2686 * high order bits select the register, while the rest of the bits
2687 * select the bit within the register.
2690 bzero(mchash, sizeof(mchash));
2693 if_maddr_rlock(sc->sc_ifp);
2694 TAILQ_FOREACH(ifma, &sc->sc_ifp->if_multiaddrs, ifma_link) {
2695 if (ifma->ifma_addr->sa_family != AF_LINK)
2697 crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
2698 ifma->ifma_addr), ETHER_ADDR_LEN);
2700 /* Just want the 6 least significant bits. */
2703 /* Set the corresponding bit in the hash table. */
2704 mchash[crc >> 5] |= 1 << (crc & 0x1f);
2707 if_maddr_runlock(ifp);
2709 mode &= ~(RM_ReceiveMulticast | RM_ReceiveAllFrames);
2711 mode |= RM_ReceiveMulticastHash;
2713 mode &= ~RM_ReceiveMulticastHash;
2715 CSR_WRITE_4(sc, STGE_HashTable0, mchash[0]);
2716 CSR_WRITE_4(sc, STGE_HashTable1, mchash[1]);
2717 CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2721 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2727 value = *(int *)arg1;
2728 error = sysctl_handle_int(oidp, &value, 0, req);
2729 if (error || !req->newptr)
2731 if (value < low || value > high)
2733 *(int *)arg1 = value;
2739 sysctl_hw_stge_rxint_nframe(SYSCTL_HANDLER_ARGS)
2741 return (sysctl_int_range(oidp, arg1, arg2, req,
2742 STGE_RXINT_NFRAME_MIN, STGE_RXINT_NFRAME_MAX));
2746 sysctl_hw_stge_rxint_dmawait(SYSCTL_HANDLER_ARGS)
2748 return (sysctl_int_range(oidp, arg1, arg2, req,
2749 STGE_RXINT_DMAWAIT_MIN, STGE_RXINT_DMAWAIT_MAX));