2 * Copyright (c) 1997, 1998, 1999
3 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #ifdef HAVE_KERNEL_OPTION_HEADERS
37 #include "opt_device_polling.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
43 #include <sys/endian.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
48 #include <sys/module.h>
50 #include <sys/socket.h>
51 #include <sys/sockio.h>
52 #include <sys/sysctl.h>
56 #include <net/if_arp.h>
57 #include <net/ethernet.h>
58 #include <net/if_dl.h>
59 #include <net/if_media.h>
60 #include <net/if_types.h>
61 #include <net/if_vlan_var.h>
63 #include <machine/bus.h>
64 #include <machine/resource.h>
66 #include <dev/mii/mii.h>
67 #include <dev/mii/miivar.h>
69 #include <dev/pci/pcireg.h>
70 #include <dev/pci/pcivar.h>
72 #include <dev/ste/if_stereg.h>
74 /* "device miibus" required. See GENERIC if you get errors here. */
75 #include "miibus_if.h"
77 MODULE_DEPEND(ste, pci, 1, 1, 1);
78 MODULE_DEPEND(ste, ether, 1, 1, 1);
79 MODULE_DEPEND(ste, miibus, 1, 1, 1);
81 /* Define to show Tx error status. */
82 #define STE_SHOW_TXERRORS
85 * Various supported device vendors/types and their names.
87 static struct ste_type ste_devs[] = {
88 { ST_VENDORID, ST_DEVICEID_ST201_1, "Sundance ST201 10/100BaseTX" },
89 { ST_VENDORID, ST_DEVICEID_ST201_2, "Sundance ST201 10/100BaseTX" },
90 { DL_VENDORID, DL_DEVICEID_DL10050, "D-Link DL10050 10/100BaseTX" },
94 static int ste_attach(device_t);
95 static int ste_detach(device_t);
96 static int ste_probe(device_t);
97 static int ste_resume(device_t);
98 static int ste_shutdown(device_t);
99 static int ste_suspend(device_t);
101 static int ste_dma_alloc(struct ste_softc *);
102 static void ste_dma_free(struct ste_softc *);
103 static void ste_dmamap_cb(void *, bus_dma_segment_t *, int, int);
104 static int ste_eeprom_wait(struct ste_softc *);
105 static int ste_encap(struct ste_softc *, struct mbuf **,
107 static int ste_ifmedia_upd(struct ifnet *);
108 static void ste_ifmedia_sts(struct ifnet *, struct ifmediareq *);
109 static void ste_init(void *);
110 static void ste_init_locked(struct ste_softc *);
111 static int ste_init_rx_list(struct ste_softc *);
112 static void ste_init_tx_list(struct ste_softc *);
113 static void ste_intr(void *);
114 static int ste_ioctl(struct ifnet *, u_long, caddr_t);
115 static int ste_mii_readreg(struct ste_softc *, struct ste_mii_frame *);
116 static void ste_mii_send(struct ste_softc *, uint32_t, int);
117 static void ste_mii_sync(struct ste_softc *);
118 static int ste_mii_writereg(struct ste_softc *, struct ste_mii_frame *);
119 static int ste_miibus_readreg(device_t, int, int);
120 static void ste_miibus_statchg(device_t);
121 static int ste_miibus_writereg(device_t, int, int, int);
122 static int ste_newbuf(struct ste_softc *, struct ste_chain_onefrag *);
123 static int ste_read_eeprom(struct ste_softc *, uint16_t *, int, int);
124 static void ste_reset(struct ste_softc *);
125 static void ste_restart_tx(struct ste_softc *);
126 static int ste_rxeof(struct ste_softc *, int);
127 static void ste_rxfilter(struct ste_softc *);
128 static void ste_setwol(struct ste_softc *);
129 static void ste_start(struct ifnet *);
130 static void ste_start_locked(struct ifnet *);
131 static void ste_stats_clear(struct ste_softc *);
132 static void ste_stats_update(struct ste_softc *);
133 static void ste_stop(struct ste_softc *);
134 static void ste_sysctl_node(struct ste_softc *);
135 static void ste_tick(void *);
136 static void ste_txeoc(struct ste_softc *);
137 static void ste_txeof(struct ste_softc *);
138 static void ste_wait(struct ste_softc *);
139 static void ste_watchdog(struct ste_softc *);
141 static device_method_t ste_methods[] = {
142 /* Device interface */
143 DEVMETHOD(device_probe, ste_probe),
144 DEVMETHOD(device_attach, ste_attach),
145 DEVMETHOD(device_detach, ste_detach),
146 DEVMETHOD(device_shutdown, ste_shutdown),
147 DEVMETHOD(device_suspend, ste_suspend),
148 DEVMETHOD(device_resume, ste_resume),
151 DEVMETHOD(bus_print_child, bus_generic_print_child),
152 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
155 DEVMETHOD(miibus_readreg, ste_miibus_readreg),
156 DEVMETHOD(miibus_writereg, ste_miibus_writereg),
157 DEVMETHOD(miibus_statchg, ste_miibus_statchg),
162 static driver_t ste_driver = {
165 sizeof(struct ste_softc)
168 static devclass_t ste_devclass;
170 DRIVER_MODULE(ste, pci, ste_driver, ste_devclass, 0, 0);
171 DRIVER_MODULE(miibus, ste, miibus_driver, miibus_devclass, 0, 0);
173 #define STE_SETBIT4(sc, reg, x) \
174 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
176 #define STE_CLRBIT4(sc, reg, x) \
177 CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
179 #define STE_SETBIT2(sc, reg, x) \
180 CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) | (x))
182 #define STE_CLRBIT2(sc, reg, x) \
183 CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) & ~(x))
185 #define STE_SETBIT1(sc, reg, x) \
186 CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) | (x))
188 #define STE_CLRBIT1(sc, reg, x) \
189 CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) & ~(x))
192 #define MII_SET(x) STE_SETBIT1(sc, STE_PHYCTL, x)
193 #define MII_CLR(x) STE_CLRBIT1(sc, STE_PHYCTL, x)
196 * Sync the PHYs by setting data bit and strobing the clock 32 times.
199 ste_mii_sync(struct ste_softc *sc)
203 MII_SET(STE_PHYCTL_MDIR|STE_PHYCTL_MDATA);
205 for (i = 0; i < 32; i++) {
206 MII_SET(STE_PHYCTL_MCLK);
208 MII_CLR(STE_PHYCTL_MCLK);
214 * Clock a series of bits through the MII.
217 ste_mii_send(struct ste_softc *sc, uint32_t bits, int cnt)
221 MII_CLR(STE_PHYCTL_MCLK);
223 for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
225 MII_SET(STE_PHYCTL_MDATA);
227 MII_CLR(STE_PHYCTL_MDATA);
230 MII_CLR(STE_PHYCTL_MCLK);
232 MII_SET(STE_PHYCTL_MCLK);
237 * Read an PHY register through the MII.
240 ste_mii_readreg(struct ste_softc *sc, struct ste_mii_frame *frame)
245 * Set up frame for RX.
247 frame->mii_stdelim = STE_MII_STARTDELIM;
248 frame->mii_opcode = STE_MII_READOP;
249 frame->mii_turnaround = 0;
252 CSR_WRITE_2(sc, STE_PHYCTL, 0);
256 MII_SET(STE_PHYCTL_MDIR);
261 * Send command/address info.
263 ste_mii_send(sc, frame->mii_stdelim, 2);
264 ste_mii_send(sc, frame->mii_opcode, 2);
265 ste_mii_send(sc, frame->mii_phyaddr, 5);
266 ste_mii_send(sc, frame->mii_regaddr, 5);
269 MII_CLR(STE_PHYCTL_MDIR);
272 MII_CLR((STE_PHYCTL_MCLK|STE_PHYCTL_MDATA));
274 MII_SET(STE_PHYCTL_MCLK);
278 MII_CLR(STE_PHYCTL_MCLK);
280 ack = CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA;
281 MII_SET(STE_PHYCTL_MCLK);
285 * Now try reading data bits. If the ack failed, we still
286 * need to clock through 16 cycles to keep the PHY(s) in sync.
289 for (i = 0; i < 16; i++) {
290 MII_CLR(STE_PHYCTL_MCLK);
292 MII_SET(STE_PHYCTL_MCLK);
298 for (i = 0x8000; i; i >>= 1) {
299 MII_CLR(STE_PHYCTL_MCLK);
302 if (CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA)
303 frame->mii_data |= i;
306 MII_SET(STE_PHYCTL_MCLK);
312 MII_CLR(STE_PHYCTL_MCLK);
314 MII_SET(STE_PHYCTL_MCLK);
323 * Write to a PHY register through the MII.
326 ste_mii_writereg(struct ste_softc *sc, struct ste_mii_frame *frame)
330 * Set up frame for TX.
333 frame->mii_stdelim = STE_MII_STARTDELIM;
334 frame->mii_opcode = STE_MII_WRITEOP;
335 frame->mii_turnaround = STE_MII_TURNAROUND;
338 * Turn on data output.
340 MII_SET(STE_PHYCTL_MDIR);
344 ste_mii_send(sc, frame->mii_stdelim, 2);
345 ste_mii_send(sc, frame->mii_opcode, 2);
346 ste_mii_send(sc, frame->mii_phyaddr, 5);
347 ste_mii_send(sc, frame->mii_regaddr, 5);
348 ste_mii_send(sc, frame->mii_turnaround, 2);
349 ste_mii_send(sc, frame->mii_data, 16);
352 MII_SET(STE_PHYCTL_MCLK);
354 MII_CLR(STE_PHYCTL_MCLK);
360 MII_CLR(STE_PHYCTL_MDIR);
366 ste_miibus_readreg(device_t dev, int phy, int reg)
368 struct ste_softc *sc;
369 struct ste_mii_frame frame;
371 sc = device_get_softc(dev);
372 bzero((char *)&frame, sizeof(frame));
374 frame.mii_phyaddr = phy;
375 frame.mii_regaddr = reg;
376 ste_mii_readreg(sc, &frame);
378 return (frame.mii_data);
382 ste_miibus_writereg(device_t dev, int phy, int reg, int data)
384 struct ste_softc *sc;
385 struct ste_mii_frame frame;
387 sc = device_get_softc(dev);
388 bzero((char *)&frame, sizeof(frame));
390 frame.mii_phyaddr = phy;
391 frame.mii_regaddr = reg;
392 frame.mii_data = data;
394 ste_mii_writereg(sc, &frame);
400 ste_miibus_statchg(device_t dev)
402 struct ste_softc *sc;
403 struct mii_data *mii;
407 sc = device_get_softc(dev);
409 mii = device_get_softc(sc->ste_miibus);
411 if (mii == NULL || ifp == NULL ||
412 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
415 sc->ste_flags &= ~STE_FLAG_LINK;
416 if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
417 (IFM_ACTIVE | IFM_AVALID)) {
418 switch (IFM_SUBTYPE(mii->mii_media_active)) {
423 sc->ste_flags |= STE_FLAG_LINK;
429 /* Program MACs with resolved speed/duplex/flow-control. */
430 if ((sc->ste_flags & STE_FLAG_LINK) != 0) {
431 cfg = CSR_READ_2(sc, STE_MACCTL0);
432 cfg &= ~(STE_MACCTL0_FLOWCTL_ENABLE | STE_MACCTL0_FULLDUPLEX);
433 if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
435 * ST201 data sheet says driver should enable receiving
436 * MAC control frames bit of receive mode register to
437 * receive flow-control frames but the register has no
438 * such bits. In addition the controller has no ability
439 * to send pause frames so it should be handled in
440 * driver. Implementing pause timer handling in driver
441 * layer is not trivial, so don't enable flow-control
444 cfg |= STE_MACCTL0_FULLDUPLEX;
446 CSR_WRITE_2(sc, STE_MACCTL0, cfg);
451 ste_ifmedia_upd(struct ifnet *ifp)
453 struct ste_softc *sc;
454 struct mii_data *mii;
455 struct mii_softc *miisc;
460 mii = device_get_softc(sc->ste_miibus);
461 if (mii->mii_instance) {
462 LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
463 mii_phy_reset(miisc);
465 error = mii_mediachg(mii);
472 ste_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
474 struct ste_softc *sc;
475 struct mii_data *mii;
478 mii = device_get_softc(sc->ste_miibus);
481 if ((ifp->if_flags & IFF_UP) == 0) {
486 ifmr->ifm_active = mii->mii_media_active;
487 ifmr->ifm_status = mii->mii_media_status;
492 ste_wait(struct ste_softc *sc)
496 for (i = 0; i < STE_TIMEOUT; i++) {
497 if (!(CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_DMA_HALTINPROG))
502 if (i == STE_TIMEOUT)
503 device_printf(sc->ste_dev, "command never completed!\n");
507 * The EEPROM is slow: give it time to come ready after issuing
511 ste_eeprom_wait(struct ste_softc *sc)
517 for (i = 0; i < 100; i++) {
518 if (CSR_READ_2(sc, STE_EEPROM_CTL) & STE_EECTL_BUSY)
525 device_printf(sc->ste_dev, "eeprom failed to come ready\n");
533 * Read a sequence of words from the EEPROM. Note that ethernet address
534 * data is stored in the EEPROM in network byte order.
537 ste_read_eeprom(struct ste_softc *sc, uint16_t *dest, int off, int cnt)
541 if (ste_eeprom_wait(sc))
544 for (i = 0; i < cnt; i++) {
545 CSR_WRITE_2(sc, STE_EEPROM_CTL, STE_EEOPCODE_READ | (off + i));
546 err = ste_eeprom_wait(sc);
549 *dest = le16toh(CSR_READ_2(sc, STE_EEPROM_DATA));
553 return (err ? 1 : 0);
557 ste_rxfilter(struct ste_softc *sc)
560 struct ifmultiaddr *ifma;
561 uint32_t hashes[2] = { 0, 0 };
568 rxcfg = CSR_READ_1(sc, STE_RX_MODE);
569 rxcfg |= STE_RXMODE_UNICAST;
570 rxcfg &= ~(STE_RXMODE_ALLMULTI | STE_RXMODE_MULTIHASH |
571 STE_RXMODE_BROADCAST | STE_RXMODE_PROMISC);
572 if (ifp->if_flags & IFF_BROADCAST)
573 rxcfg |= STE_RXMODE_BROADCAST;
574 if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
575 if ((ifp->if_flags & IFF_ALLMULTI) != 0)
576 rxcfg |= STE_RXMODE_ALLMULTI;
577 if ((ifp->if_flags & IFF_PROMISC) != 0)
578 rxcfg |= STE_RXMODE_PROMISC;
582 rxcfg |= STE_RXMODE_MULTIHASH;
583 /* Now program new ones. */
585 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
586 if (ifma->ifma_addr->sa_family != AF_LINK)
588 h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
589 ifma->ifma_addr), ETHER_ADDR_LEN) & 0x3F;
591 hashes[0] |= (1 << h);
593 hashes[1] |= (1 << (h - 32));
595 if_maddr_runlock(ifp);
598 CSR_WRITE_2(sc, STE_MAR0, hashes[0] & 0xFFFF);
599 CSR_WRITE_2(sc, STE_MAR1, (hashes[0] >> 16) & 0xFFFF);
600 CSR_WRITE_2(sc, STE_MAR2, hashes[1] & 0xFFFF);
601 CSR_WRITE_2(sc, STE_MAR3, (hashes[1] >> 16) & 0xFFFF);
602 CSR_WRITE_1(sc, STE_RX_MODE, rxcfg);
603 CSR_READ_1(sc, STE_RX_MODE);
606 #ifdef DEVICE_POLLING
607 static poll_handler_t ste_poll, ste_poll_locked;
610 ste_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
612 struct ste_softc *sc = ifp->if_softc;
616 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
617 rx_npkts = ste_poll_locked(ifp, cmd, count);
623 ste_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
625 struct ste_softc *sc = ifp->if_softc;
630 rx_npkts = ste_rxeof(sc, count);
633 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
634 ste_start_locked(ifp);
636 if (cmd == POLL_AND_CHECK_STATUS) {
639 status = CSR_READ_2(sc, STE_ISR_ACK);
641 if (status & STE_ISR_STATS_OFLOW)
642 ste_stats_update(sc);
644 if (status & STE_ISR_HOSTERR) {
645 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
651 #endif /* DEVICE_POLLING */
656 struct ste_softc *sc;
658 uint16_t intrs, status;
664 #ifdef DEVICE_POLLING
665 if (ifp->if_capenable & IFCAP_POLLING) {
670 /* Reading STE_ISR_ACK clears STE_IMR register. */
671 status = CSR_READ_2(sc, STE_ISR_ACK);
672 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
678 if (status == 0xFFFF || (status & intrs) == 0)
681 if (sc->ste_int_rx_act > 0) {
682 status &= ~STE_ISR_RX_DMADONE;
683 intrs &= ~STE_IMR_RX_DMADONE;
686 if ((status & (STE_ISR_SOFTINTR | STE_ISR_RX_DMADONE)) != 0) {
689 * The controller has no ability to Rx interrupt
690 * moderation feature. Receiving 64 bytes frames
691 * from wire generates too many interrupts which in
692 * turn make system useless to process other useful
693 * things. Fortunately ST201 supports single shot
694 * timer so use the timer to implement Rx interrupt
695 * moderation in driver. This adds more register
696 * access but it greatly reduces number of Rx
697 * interrupts under high network load.
699 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
700 (sc->ste_int_rx_mod != 0)) {
701 if ((status & STE_ISR_RX_DMADONE) != 0) {
702 CSR_WRITE_2(sc, STE_COUNTDOWN,
703 STE_TIMER_USECS(sc->ste_int_rx_mod));
704 intrs &= ~STE_IMR_RX_DMADONE;
705 sc->ste_int_rx_act = 1;
707 intrs |= STE_IMR_RX_DMADONE;
708 sc->ste_int_rx_act = 0;
712 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
713 if ((status & STE_ISR_TX_DMADONE) != 0)
715 if ((status & STE_ISR_TX_DONE) != 0)
717 if ((status & STE_ISR_STATS_OFLOW) != 0)
718 ste_stats_update(sc);
719 if ((status & STE_ISR_HOSTERR) != 0) {
720 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
725 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
726 ste_start_locked(ifp);
728 /* Re-enable interrupts */
729 CSR_WRITE_2(sc, STE_IMR, intrs);
735 * A frame has been uploaded: pass the resulting mbuf chain up to
736 * the higher level protocols.
739 ste_rxeof(struct ste_softc *sc, int count)
743 struct ste_chain_onefrag *cur_rx;
745 int total_len, rx_npkts;
749 bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
750 sc->ste_cdata.ste_rx_list_map,
751 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
753 cur_rx = sc->ste_cdata.ste_rx_head;
754 for (rx_npkts = 0; rx_npkts < STE_RX_LIST_CNT; rx_npkts++,
755 cur_rx = cur_rx->ste_next) {
756 rxstat = le32toh(cur_rx->ste_ptr->ste_status);
757 if ((rxstat & STE_RXSTAT_DMADONE) == 0)
759 #ifdef DEVICE_POLLING
760 if (ifp->if_capenable & IFCAP_POLLING) {
766 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
769 * If an error occurs, update stats, clear the
770 * status word and leave the mbuf cluster in place:
771 * it should simply get re-used next time this descriptor
772 * comes up in the ring.
774 if (rxstat & STE_RXSTAT_FRAME_ERR) {
776 cur_rx->ste_ptr->ste_status = 0;
780 /* No errors; receive the packet. */
781 m = cur_rx->ste_mbuf;
782 total_len = STE_RX_BYTES(rxstat);
785 * Try to conjure up a new mbuf cluster. If that
786 * fails, it means we have an out of memory condition and
787 * should leave the buffer in place and continue. This will
788 * result in a lost packet, but there's little else we
789 * can do in this situation.
791 if (ste_newbuf(sc, cur_rx) != 0) {
793 cur_rx->ste_ptr->ste_status = 0;
797 m->m_pkthdr.rcvif = ifp;
798 m->m_pkthdr.len = m->m_len = total_len;
802 (*ifp->if_input)(ifp, m);
807 sc->ste_cdata.ste_rx_head = cur_rx;
808 bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
809 sc->ste_cdata.ste_rx_list_map,
810 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
817 ste_txeoc(struct ste_softc *sc)
827 * STE_TX_STATUS register implements a queue of up to 31
828 * transmit status byte. Writing an arbitrary value to the
829 * register will advance the queue to the next transmit
830 * status byte. This means if driver does not read
831 * STE_TX_STATUS register after completing sending more
832 * than 31 frames the controller would be stalled so driver
833 * should re-wake the Tx MAC. This is the most severe
834 * limitation of ST201 based controller.
837 txstat = CSR_READ_2(sc, STE_TX_STATUS);
838 if ((txstat & STE_TXSTATUS_TXDONE) == 0)
840 if ((txstat & (STE_TXSTATUS_UNDERRUN |
841 STE_TXSTATUS_EXCESSCOLLS | STE_TXSTATUS_RECLAIMERR |
842 STE_TXSTATUS_STATSOFLOW)) != 0) {
844 #ifdef STE_SHOW_TXERRORS
845 device_printf(sc->ste_dev, "TX error : 0x%b\n",
846 txstat & 0xFF, STE_ERR_BITS);
848 if ((txstat & STE_TXSTATUS_UNDERRUN) != 0 &&
849 sc->ste_tx_thresh < STE_PACKET_SIZE) {
850 sc->ste_tx_thresh += STE_MIN_FRAMELEN;
851 if (sc->ste_tx_thresh > STE_PACKET_SIZE)
852 sc->ste_tx_thresh = STE_PACKET_SIZE;
853 device_printf(sc->ste_dev,
854 "TX underrun, increasing TX"
855 " start threshold to %d bytes\n",
857 /* Make sure to disable active DMA cycles. */
858 STE_SETBIT4(sc, STE_DMACTL,
859 STE_DMACTL_TXDMA_STALL);
861 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
869 * Advance to next status and ACK TxComplete
870 * interrupt. ST201 data sheet was wrong here, to
871 * get next Tx status, we have to write both
872 * STE_TX_STATUS and STE_TX_FRAMEID register.
873 * Otherwise controller returns the same status
874 * as well as not acknowledge Tx completion
877 CSR_WRITE_2(sc, STE_TX_STATUS, txstat);
884 struct ste_softc *sc;
885 struct mii_data *mii;
887 sc = (struct ste_softc *)arg;
891 mii = device_get_softc(sc->ste_miibus);
894 * ukphy(4) does not seem to generate CB that reports
895 * resolved link state so if we know we lost a link,
896 * explicitly check the link state.
898 if ((sc->ste_flags & STE_FLAG_LINK) == 0)
899 ste_miibus_statchg(sc->ste_dev);
901 * Because we are not generating Tx completion
902 * interrupt for every frame, reclaim transmitted
907 ste_stats_update(sc);
909 callout_reset(&sc->ste_callout, hz, ste_tick, sc);
913 ste_txeof(struct ste_softc *sc)
916 struct ste_chain *cur_tx;
923 idx = sc->ste_cdata.ste_tx_cons;
924 if (idx == sc->ste_cdata.ste_tx_prod)
927 bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
928 sc->ste_cdata.ste_tx_list_map,
929 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
931 while (idx != sc->ste_cdata.ste_tx_prod) {
932 cur_tx = &sc->ste_cdata.ste_tx_chain[idx];
933 txstat = le32toh(cur_tx->ste_ptr->ste_ctl);
934 if ((txstat & STE_TXCTL_DMADONE) == 0)
936 bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, cur_tx->ste_map,
937 BUS_DMASYNC_POSTWRITE);
938 bus_dmamap_unload(sc->ste_cdata.ste_tx_tag, cur_tx->ste_map);
939 KASSERT(cur_tx->ste_mbuf != NULL,
940 ("%s: freeing NULL mbuf!\n", __func__));
941 m_freem(cur_tx->ste_mbuf);
942 cur_tx->ste_mbuf = NULL;
943 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
945 sc->ste_cdata.ste_tx_cnt--;
946 STE_INC(idx, STE_TX_LIST_CNT);
949 sc->ste_cdata.ste_tx_cons = idx;
950 if (sc->ste_cdata.ste_tx_cnt == 0)
955 ste_stats_clear(struct ste_softc *sc)
961 CSR_READ_2(sc, STE_STAT_RX_OCTETS_LO);
962 CSR_READ_2(sc, STE_STAT_RX_OCTETS_HI);
963 CSR_READ_2(sc, STE_STAT_RX_FRAMES);
964 CSR_READ_1(sc, STE_STAT_RX_BCAST);
965 CSR_READ_1(sc, STE_STAT_RX_MCAST);
966 CSR_READ_1(sc, STE_STAT_RX_LOST);
968 CSR_READ_2(sc, STE_STAT_TX_OCTETS_LO);
969 CSR_READ_2(sc, STE_STAT_TX_OCTETS_HI);
970 CSR_READ_2(sc, STE_STAT_TX_FRAMES);
971 CSR_READ_1(sc, STE_STAT_TX_BCAST);
972 CSR_READ_1(sc, STE_STAT_TX_MCAST);
973 CSR_READ_1(sc, STE_STAT_CARRIER_ERR);
974 CSR_READ_1(sc, STE_STAT_SINGLE_COLLS);
975 CSR_READ_1(sc, STE_STAT_MULTI_COLLS);
976 CSR_READ_1(sc, STE_STAT_LATE_COLLS);
977 CSR_READ_1(sc, STE_STAT_TX_DEFER);
978 CSR_READ_1(sc, STE_STAT_TX_EXDEFER);
979 CSR_READ_1(sc, STE_STAT_TX_ABORT);
983 ste_stats_update(struct ste_softc *sc)
986 struct ste_hw_stats *stats;
992 stats = &sc->ste_stats;
994 val = (uint32_t)CSR_READ_2(sc, STE_STAT_RX_OCTETS_LO) |
995 ((uint32_t)CSR_READ_2(sc, STE_STAT_RX_OCTETS_HI)) << 16;
997 stats->rx_bytes += val;
998 stats->rx_frames += CSR_READ_2(sc, STE_STAT_RX_FRAMES);
999 stats->rx_bcast_frames += CSR_READ_1(sc, STE_STAT_RX_BCAST);
1000 stats->rx_mcast_frames += CSR_READ_1(sc, STE_STAT_RX_MCAST);
1001 stats->rx_lost_frames += CSR_READ_1(sc, STE_STAT_RX_LOST);
1003 val = (uint32_t)CSR_READ_2(sc, STE_STAT_TX_OCTETS_LO) |
1004 ((uint32_t)CSR_READ_2(sc, STE_STAT_TX_OCTETS_HI)) << 16;
1006 stats->tx_bytes += val;
1007 stats->tx_frames += CSR_READ_2(sc, STE_STAT_TX_FRAMES);
1008 stats->tx_bcast_frames += CSR_READ_1(sc, STE_STAT_TX_BCAST);
1009 stats->tx_mcast_frames += CSR_READ_1(sc, STE_STAT_TX_MCAST);
1010 stats->tx_carrsense_errs += CSR_READ_1(sc, STE_STAT_CARRIER_ERR);
1011 val = CSR_READ_1(sc, STE_STAT_SINGLE_COLLS);
1012 stats->tx_single_colls += val;
1013 ifp->if_collisions += val;
1014 val = CSR_READ_1(sc, STE_STAT_MULTI_COLLS);
1015 stats->tx_multi_colls += val;
1016 ifp->if_collisions += val;
1017 val += CSR_READ_1(sc, STE_STAT_LATE_COLLS);
1018 stats->tx_late_colls += val;
1019 ifp->if_collisions += val;
1020 stats->tx_frames_defered += CSR_READ_1(sc, STE_STAT_TX_DEFER);
1021 stats->tx_excess_defers += CSR_READ_1(sc, STE_STAT_TX_EXDEFER);
1022 stats->tx_abort += CSR_READ_1(sc, STE_STAT_TX_ABORT);
1026 * Probe for a Sundance ST201 chip. Check the PCI vendor and device
1027 * IDs against our list and return a device name if we find a match.
1030 ste_probe(device_t dev)
1036 while (t->ste_name != NULL) {
1037 if ((pci_get_vendor(dev) == t->ste_vid) &&
1038 (pci_get_device(dev) == t->ste_did)) {
1039 device_set_desc(dev, t->ste_name);
1040 return (BUS_PROBE_DEFAULT);
1049 * Attach the interface. Allocate softc structures, do ifmedia
1050 * setup and ethernet/BPF attach.
1053 ste_attach(device_t dev)
1055 struct ste_softc *sc;
1057 uint16_t eaddr[ETHER_ADDR_LEN / 2];
1058 int error = 0, phy, pmc, prefer_iomap, rid;
1060 sc = device_get_softc(dev);
1064 * Only use one PHY since this chip reports multiple
1065 * Note on the DFE-550 the PHY is at 1 on the DFE-580
1066 * it is at 0 & 1. It is rev 0x12.
1068 if (pci_get_vendor(dev) == DL_VENDORID &&
1069 pci_get_device(dev) == DL_DEVICEID_DL10050 &&
1070 pci_get_revid(dev) == 0x12 )
1071 sc->ste_flags |= STE_FLAG_ONE_PHY;
1073 mtx_init(&sc->ste_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1076 * Map control/status registers.
1078 pci_enable_busmaster(dev);
1081 * Prefer memory space register mapping over IO space but use
1082 * IO space for a device that is known to have issues on memory
1086 if (pci_get_device(dev) == ST_DEVICEID_ST201_1)
1089 resource_int_value(device_get_name(sc->ste_dev),
1090 device_get_unit(sc->ste_dev), "prefer_iomap",
1092 if (prefer_iomap == 0) {
1093 sc->ste_res_id = PCIR_BAR(1);
1094 sc->ste_res_type = SYS_RES_MEMORY;
1095 sc->ste_res = bus_alloc_resource_any(dev, sc->ste_res_type,
1096 &sc->ste_res_id, RF_ACTIVE);
1098 if (prefer_iomap || sc->ste_res == NULL) {
1099 sc->ste_res_id = PCIR_BAR(0);
1100 sc->ste_res_type = SYS_RES_IOPORT;
1101 sc->ste_res = bus_alloc_resource_any(dev, sc->ste_res_type,
1102 &sc->ste_res_id, RF_ACTIVE);
1104 if (sc->ste_res == NULL) {
1105 device_printf(dev, "couldn't map ports/memory\n");
1110 /* Allocate interrupt */
1112 sc->ste_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1113 RF_SHAREABLE | RF_ACTIVE);
1115 if (sc->ste_irq == NULL) {
1116 device_printf(dev, "couldn't map interrupt\n");
1121 callout_init_mtx(&sc->ste_callout, &sc->ste_mtx, 0);
1123 /* Reset the adapter. */
1127 * Get station address from the EEPROM.
1129 if (ste_read_eeprom(sc, eaddr, STE_EEADDR_NODE0, ETHER_ADDR_LEN / 2)) {
1130 device_printf(dev, "failed to read station address\n");
1134 ste_sysctl_node(sc);
1136 if ((error = ste_dma_alloc(sc)) != 0)
1139 ifp = sc->ste_ifp = if_alloc(IFT_ETHER);
1141 device_printf(dev, "can not if_alloc()\n");
1148 if ((sc->ste_flags & STE_FLAG_ONE_PHY) != 0)
1150 error = mii_attach(dev, &sc->ste_miibus, ifp, ste_ifmedia_upd,
1151 ste_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
1153 device_printf(dev, "attaching PHYs failed\n");
1158 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1159 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1160 ifp->if_ioctl = ste_ioctl;
1161 ifp->if_start = ste_start;
1162 ifp->if_init = ste_init;
1163 IFQ_SET_MAXLEN(&ifp->if_snd, STE_TX_LIST_CNT - 1);
1164 ifp->if_snd.ifq_drv_maxlen = STE_TX_LIST_CNT - 1;
1165 IFQ_SET_READY(&ifp->if_snd);
1167 sc->ste_tx_thresh = STE_TXSTART_THRESH;
1170 * Call MI attach routine.
1172 ether_ifattach(ifp, (uint8_t *)eaddr);
1175 * Tell the upper layer(s) we support long frames.
1177 ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1178 ifp->if_capabilities |= IFCAP_VLAN_MTU;
1179 if (pci_find_extcap(dev, PCIY_PMG, &pmc) == 0)
1180 ifp->if_capabilities |= IFCAP_WOL_MAGIC;
1181 ifp->if_capenable = ifp->if_capabilities;
1182 #ifdef DEVICE_POLLING
1183 ifp->if_capabilities |= IFCAP_POLLING;
1186 /* Hook interrupt last to avoid having to lock softc */
1187 error = bus_setup_intr(dev, sc->ste_irq, INTR_TYPE_NET | INTR_MPSAFE,
1188 NULL, ste_intr, sc, &sc->ste_intrhand);
1191 device_printf(dev, "couldn't set up irq\n");
1192 ether_ifdetach(ifp);
1204 * Shutdown hardware and free up resources. This can be called any
1205 * time after the mutex has been initialized. It is called in both
1206 * the error case in attach and the normal detach case so it needs
1207 * to be careful about only freeing resources that have actually been
1211 ste_detach(device_t dev)
1213 struct ste_softc *sc;
1216 sc = device_get_softc(dev);
1217 KASSERT(mtx_initialized(&sc->ste_mtx), ("ste mutex not initialized"));
1220 #ifdef DEVICE_POLLING
1221 if (ifp->if_capenable & IFCAP_POLLING)
1222 ether_poll_deregister(ifp);
1225 /* These should only be active if attach succeeded */
1226 if (device_is_attached(dev)) {
1227 ether_ifdetach(ifp);
1231 callout_drain(&sc->ste_callout);
1234 device_delete_child(dev, sc->ste_miibus);
1235 bus_generic_detach(dev);
1237 if (sc->ste_intrhand)
1238 bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand);
1240 bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq);
1242 bus_release_resource(dev, sc->ste_res_type, sc->ste_res_id,
1249 mtx_destroy(&sc->ste_mtx);
1254 struct ste_dmamap_arg {
1255 bus_addr_t ste_busaddr;
1259 ste_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
1261 struct ste_dmamap_arg *ctx;
1266 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1268 ctx = (struct ste_dmamap_arg *)arg;
1269 ctx->ste_busaddr = segs[0].ds_addr;
1273 ste_dma_alloc(struct ste_softc *sc)
1275 struct ste_chain *txc;
1276 struct ste_chain_onefrag *rxc;
1277 struct ste_dmamap_arg ctx;
1280 /* Create parent DMA tag. */
1281 error = bus_dma_tag_create(
1282 bus_get_dma_tag(sc->ste_dev), /* parent */
1283 1, 0, /* alignment, boundary */
1284 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1285 BUS_SPACE_MAXADDR, /* highaddr */
1286 NULL, NULL, /* filter, filterarg */
1287 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
1289 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1291 NULL, NULL, /* lockfunc, lockarg */
1292 &sc->ste_cdata.ste_parent_tag);
1294 device_printf(sc->ste_dev,
1295 "could not create parent DMA tag.\n");
1299 /* Create DMA tag for Tx descriptor list. */
1300 error = bus_dma_tag_create(
1301 sc->ste_cdata.ste_parent_tag, /* parent */
1302 STE_DESC_ALIGN, 0, /* alignment, boundary */
1303 BUS_SPACE_MAXADDR, /* lowaddr */
1304 BUS_SPACE_MAXADDR, /* highaddr */
1305 NULL, NULL, /* filter, filterarg */
1306 STE_TX_LIST_SZ, /* maxsize */
1308 STE_TX_LIST_SZ, /* maxsegsize */
1310 NULL, NULL, /* lockfunc, lockarg */
1311 &sc->ste_cdata.ste_tx_list_tag);
1313 device_printf(sc->ste_dev,
1314 "could not create Tx list DMA tag.\n");
1318 /* Create DMA tag for Rx descriptor list. */
1319 error = bus_dma_tag_create(
1320 sc->ste_cdata.ste_parent_tag, /* parent */
1321 STE_DESC_ALIGN, 0, /* alignment, boundary */
1322 BUS_SPACE_MAXADDR, /* lowaddr */
1323 BUS_SPACE_MAXADDR, /* highaddr */
1324 NULL, NULL, /* filter, filterarg */
1325 STE_RX_LIST_SZ, /* maxsize */
1327 STE_RX_LIST_SZ, /* maxsegsize */
1329 NULL, NULL, /* lockfunc, lockarg */
1330 &sc->ste_cdata.ste_rx_list_tag);
1332 device_printf(sc->ste_dev,
1333 "could not create Rx list DMA tag.\n");
1337 /* Create DMA tag for Tx buffers. */
1338 error = bus_dma_tag_create(
1339 sc->ste_cdata.ste_parent_tag, /* parent */
1340 1, 0, /* alignment, boundary */
1341 BUS_SPACE_MAXADDR, /* lowaddr */
1342 BUS_SPACE_MAXADDR, /* highaddr */
1343 NULL, NULL, /* filter, filterarg */
1344 MCLBYTES * STE_MAXFRAGS, /* maxsize */
1345 STE_MAXFRAGS, /* nsegments */
1346 MCLBYTES, /* maxsegsize */
1348 NULL, NULL, /* lockfunc, lockarg */
1349 &sc->ste_cdata.ste_tx_tag);
1351 device_printf(sc->ste_dev, "could not create Tx DMA tag.\n");
1355 /* Create DMA tag for Rx buffers. */
1356 error = bus_dma_tag_create(
1357 sc->ste_cdata.ste_parent_tag, /* parent */
1358 1, 0, /* alignment, boundary */
1359 BUS_SPACE_MAXADDR, /* lowaddr */
1360 BUS_SPACE_MAXADDR, /* highaddr */
1361 NULL, NULL, /* filter, filterarg */
1362 MCLBYTES, /* maxsize */
1364 MCLBYTES, /* maxsegsize */
1366 NULL, NULL, /* lockfunc, lockarg */
1367 &sc->ste_cdata.ste_rx_tag);
1369 device_printf(sc->ste_dev, "could not create Rx DMA tag.\n");
1373 /* Allocate DMA'able memory and load the DMA map for Tx list. */
1374 error = bus_dmamem_alloc(sc->ste_cdata.ste_tx_list_tag,
1375 (void **)&sc->ste_ldata.ste_tx_list,
1376 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1377 &sc->ste_cdata.ste_tx_list_map);
1379 device_printf(sc->ste_dev,
1380 "could not allocate DMA'able memory for Tx list.\n");
1383 ctx.ste_busaddr = 0;
1384 error = bus_dmamap_load(sc->ste_cdata.ste_tx_list_tag,
1385 sc->ste_cdata.ste_tx_list_map, sc->ste_ldata.ste_tx_list,
1386 STE_TX_LIST_SZ, ste_dmamap_cb, &ctx, 0);
1387 if (error != 0 || ctx.ste_busaddr == 0) {
1388 device_printf(sc->ste_dev,
1389 "could not load DMA'able memory for Tx list.\n");
1392 sc->ste_ldata.ste_tx_list_paddr = ctx.ste_busaddr;
1394 /* Allocate DMA'able memory and load the DMA map for Rx list. */
1395 error = bus_dmamem_alloc(sc->ste_cdata.ste_rx_list_tag,
1396 (void **)&sc->ste_ldata.ste_rx_list,
1397 BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1398 &sc->ste_cdata.ste_rx_list_map);
1400 device_printf(sc->ste_dev,
1401 "could not allocate DMA'able memory for Rx list.\n");
1404 ctx.ste_busaddr = 0;
1405 error = bus_dmamap_load(sc->ste_cdata.ste_rx_list_tag,
1406 sc->ste_cdata.ste_rx_list_map, sc->ste_ldata.ste_rx_list,
1407 STE_RX_LIST_SZ, ste_dmamap_cb, &ctx, 0);
1408 if (error != 0 || ctx.ste_busaddr == 0) {
1409 device_printf(sc->ste_dev,
1410 "could not load DMA'able memory for Rx list.\n");
1413 sc->ste_ldata.ste_rx_list_paddr = ctx.ste_busaddr;
1415 /* Create DMA maps for Tx buffers. */
1416 for (i = 0; i < STE_TX_LIST_CNT; i++) {
1417 txc = &sc->ste_cdata.ste_tx_chain[i];
1418 txc->ste_ptr = NULL;
1419 txc->ste_mbuf = NULL;
1420 txc->ste_next = NULL;
1422 txc->ste_map = NULL;
1423 error = bus_dmamap_create(sc->ste_cdata.ste_tx_tag, 0,
1426 device_printf(sc->ste_dev,
1427 "could not create Tx dmamap.\n");
1431 /* Create DMA maps for Rx buffers. */
1432 if ((error = bus_dmamap_create(sc->ste_cdata.ste_rx_tag, 0,
1433 &sc->ste_cdata.ste_rx_sparemap)) != 0) {
1434 device_printf(sc->ste_dev,
1435 "could not create spare Rx dmamap.\n");
1438 for (i = 0; i < STE_RX_LIST_CNT; i++) {
1439 rxc = &sc->ste_cdata.ste_rx_chain[i];
1440 rxc->ste_ptr = NULL;
1441 rxc->ste_mbuf = NULL;
1442 rxc->ste_next = NULL;
1443 rxc->ste_map = NULL;
1444 error = bus_dmamap_create(sc->ste_cdata.ste_rx_tag, 0,
1447 device_printf(sc->ste_dev,
1448 "could not create Rx dmamap.\n");
1458 ste_dma_free(struct ste_softc *sc)
1460 struct ste_chain *txc;
1461 struct ste_chain_onefrag *rxc;
1465 if (sc->ste_cdata.ste_tx_tag != NULL) {
1466 for (i = 0; i < STE_TX_LIST_CNT; i++) {
1467 txc = &sc->ste_cdata.ste_tx_chain[i];
1468 if (txc->ste_map != NULL) {
1469 bus_dmamap_destroy(sc->ste_cdata.ste_tx_tag,
1471 txc->ste_map = NULL;
1474 bus_dma_tag_destroy(sc->ste_cdata.ste_tx_tag);
1475 sc->ste_cdata.ste_tx_tag = NULL;
1478 if (sc->ste_cdata.ste_rx_tag != NULL) {
1479 for (i = 0; i < STE_RX_LIST_CNT; i++) {
1480 rxc = &sc->ste_cdata.ste_rx_chain[i];
1481 if (rxc->ste_map != NULL) {
1482 bus_dmamap_destroy(sc->ste_cdata.ste_rx_tag,
1484 rxc->ste_map = NULL;
1487 if (sc->ste_cdata.ste_rx_sparemap != NULL) {
1488 bus_dmamap_destroy(sc->ste_cdata.ste_rx_tag,
1489 sc->ste_cdata.ste_rx_sparemap);
1490 sc->ste_cdata.ste_rx_sparemap = NULL;
1492 bus_dma_tag_destroy(sc->ste_cdata.ste_rx_tag);
1493 sc->ste_cdata.ste_rx_tag = NULL;
1495 /* Tx descriptor list. */
1496 if (sc->ste_cdata.ste_tx_list_tag != NULL) {
1497 if (sc->ste_cdata.ste_tx_list_map != NULL)
1498 bus_dmamap_unload(sc->ste_cdata.ste_tx_list_tag,
1499 sc->ste_cdata.ste_tx_list_map);
1500 if (sc->ste_cdata.ste_tx_list_map != NULL &&
1501 sc->ste_ldata.ste_tx_list != NULL)
1502 bus_dmamem_free(sc->ste_cdata.ste_tx_list_tag,
1503 sc->ste_ldata.ste_tx_list,
1504 sc->ste_cdata.ste_tx_list_map);
1505 sc->ste_ldata.ste_tx_list = NULL;
1506 sc->ste_cdata.ste_tx_list_map = NULL;
1507 bus_dma_tag_destroy(sc->ste_cdata.ste_tx_list_tag);
1508 sc->ste_cdata.ste_tx_list_tag = NULL;
1510 /* Rx descriptor list. */
1511 if (sc->ste_cdata.ste_rx_list_tag != NULL) {
1512 if (sc->ste_cdata.ste_rx_list_map != NULL)
1513 bus_dmamap_unload(sc->ste_cdata.ste_rx_list_tag,
1514 sc->ste_cdata.ste_rx_list_map);
1515 if (sc->ste_cdata.ste_rx_list_map != NULL &&
1516 sc->ste_ldata.ste_rx_list != NULL)
1517 bus_dmamem_free(sc->ste_cdata.ste_rx_list_tag,
1518 sc->ste_ldata.ste_rx_list,
1519 sc->ste_cdata.ste_rx_list_map);
1520 sc->ste_ldata.ste_rx_list = NULL;
1521 sc->ste_cdata.ste_rx_list_map = NULL;
1522 bus_dma_tag_destroy(sc->ste_cdata.ste_rx_list_tag);
1523 sc->ste_cdata.ste_rx_list_tag = NULL;
1525 if (sc->ste_cdata.ste_parent_tag != NULL) {
1526 bus_dma_tag_destroy(sc->ste_cdata.ste_parent_tag);
1527 sc->ste_cdata.ste_parent_tag = NULL;
1532 ste_newbuf(struct ste_softc *sc, struct ste_chain_onefrag *rxc)
1535 bus_dma_segment_t segs[1];
1539 m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1542 m->m_len = m->m_pkthdr.len = MCLBYTES;
1543 m_adj(m, ETHER_ALIGN);
1545 if ((error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_rx_tag,
1546 sc->ste_cdata.ste_rx_sparemap, m, segs, &nsegs, 0)) != 0) {
1550 KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1552 if (rxc->ste_mbuf != NULL) {
1553 bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, rxc->ste_map,
1554 BUS_DMASYNC_POSTREAD);
1555 bus_dmamap_unload(sc->ste_cdata.ste_rx_tag, rxc->ste_map);
1558 rxc->ste_map = sc->ste_cdata.ste_rx_sparemap;
1559 sc->ste_cdata.ste_rx_sparemap = map;
1560 bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, rxc->ste_map,
1561 BUS_DMASYNC_PREREAD);
1563 rxc->ste_ptr->ste_status = 0;
1564 rxc->ste_ptr->ste_frag.ste_addr = htole32(segs[0].ds_addr);
1565 rxc->ste_ptr->ste_frag.ste_len = htole32(segs[0].ds_len |
1571 ste_init_rx_list(struct ste_softc *sc)
1573 struct ste_chain_data *cd;
1574 struct ste_list_data *ld;
1577 sc->ste_int_rx_act = 0;
1578 cd = &sc->ste_cdata;
1579 ld = &sc->ste_ldata;
1580 bzero(ld->ste_rx_list, STE_RX_LIST_SZ);
1581 for (i = 0; i < STE_RX_LIST_CNT; i++) {
1582 cd->ste_rx_chain[i].ste_ptr = &ld->ste_rx_list[i];
1583 error = ste_newbuf(sc, &cd->ste_rx_chain[i]);
1586 if (i == (STE_RX_LIST_CNT - 1)) {
1587 cd->ste_rx_chain[i].ste_next = &cd->ste_rx_chain[0];
1588 ld->ste_rx_list[i].ste_next =
1589 htole32(ld->ste_rx_list_paddr +
1590 (sizeof(struct ste_desc_onefrag) * 0));
1592 cd->ste_rx_chain[i].ste_next = &cd->ste_rx_chain[i + 1];
1593 ld->ste_rx_list[i].ste_next =
1594 htole32(ld->ste_rx_list_paddr +
1595 (sizeof(struct ste_desc_onefrag) * (i + 1)));
1599 cd->ste_rx_head = &cd->ste_rx_chain[0];
1600 bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag,
1601 sc->ste_cdata.ste_rx_list_map,
1602 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1608 ste_init_tx_list(struct ste_softc *sc)
1610 struct ste_chain_data *cd;
1611 struct ste_list_data *ld;
1614 cd = &sc->ste_cdata;
1615 ld = &sc->ste_ldata;
1616 bzero(ld->ste_tx_list, STE_TX_LIST_SZ);
1617 for (i = 0; i < STE_TX_LIST_CNT; i++) {
1618 cd->ste_tx_chain[i].ste_ptr = &ld->ste_tx_list[i];
1619 cd->ste_tx_chain[i].ste_mbuf = NULL;
1620 if (i == (STE_TX_LIST_CNT - 1)) {
1621 cd->ste_tx_chain[i].ste_next = &cd->ste_tx_chain[0];
1622 cd->ste_tx_chain[i].ste_phys = htole32(STE_ADDR_LO(
1623 ld->ste_tx_list_paddr +
1624 (sizeof(struct ste_desc) * 0)));
1626 cd->ste_tx_chain[i].ste_next = &cd->ste_tx_chain[i + 1];
1627 cd->ste_tx_chain[i].ste_phys = htole32(STE_ADDR_LO(
1628 ld->ste_tx_list_paddr +
1629 (sizeof(struct ste_desc) * (i + 1))));
1633 cd->ste_last_tx = NULL;
1634 cd->ste_tx_prod = 0;
1635 cd->ste_tx_cons = 0;
1638 bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
1639 sc->ste_cdata.ste_tx_list_map,
1640 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1646 struct ste_softc *sc;
1650 ste_init_locked(sc);
1655 ste_init_locked(struct ste_softc *sc)
1658 struct mii_data *mii;
1662 STE_LOCK_ASSERT(sc);
1664 mii = device_get_softc(sc->ste_miibus);
1666 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1670 /* Reset the chip to a known state. */
1673 /* Init our MAC address */
1674 for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
1675 CSR_WRITE_2(sc, STE_PAR0 + i,
1676 ((IF_LLADDR(sc->ste_ifp)[i] & 0xff) |
1677 IF_LLADDR(sc->ste_ifp)[i + 1] << 8));
1681 if (ste_init_rx_list(sc) != 0) {
1682 device_printf(sc->ste_dev,
1683 "initialization failed: no memory for RX buffers\n");
1688 /* Set RX polling interval */
1689 CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 64);
1691 /* Init TX descriptors */
1692 ste_init_tx_list(sc);
1694 /* Clear and disable WOL. */
1695 val = CSR_READ_1(sc, STE_WAKE_EVENT);
1696 val &= ~(STE_WAKEEVENT_WAKEPKT_ENB | STE_WAKEEVENT_MAGICPKT_ENB |
1697 STE_WAKEEVENT_LINKEVT_ENB | STE_WAKEEVENT_WAKEONLAN_ENB);
1698 CSR_WRITE_1(sc, STE_WAKE_EVENT, val);
1700 /* Set the TX freethresh value */
1701 CSR_WRITE_1(sc, STE_TX_DMABURST_THRESH, STE_PACKET_SIZE >> 8);
1703 /* Set the TX start threshold for best performance. */
1704 CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh);
1706 /* Set the TX reclaim threshold. */
1707 CSR_WRITE_1(sc, STE_TX_RECLAIM_THRESH, (STE_PACKET_SIZE >> 4));
1709 /* Accept VLAN length packets */
1710 CSR_WRITE_2(sc, STE_MAX_FRAMELEN, ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN);
1712 /* Set up the RX filter. */
1715 /* Load the address of the RX list. */
1716 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL);
1718 CSR_WRITE_4(sc, STE_RX_DMALIST_PTR,
1719 STE_ADDR_LO(sc->ste_ldata.ste_rx_list_paddr));
1720 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1721 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL);
1723 /* Set TX polling interval(defer until we TX first packet). */
1724 CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0);
1726 /* Load address of the TX list */
1727 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
1729 CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0);
1730 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1731 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
1733 /* Select 3.2us timer. */
1734 STE_CLRBIT4(sc, STE_DMACTL, STE_DMACTL_COUNTDOWN_SPEED |
1735 STE_DMACTL_COUNTDOWN_MODE);
1737 /* Enable receiver and transmitter */
1738 CSR_WRITE_2(sc, STE_MACCTL0, 0);
1739 CSR_WRITE_2(sc, STE_MACCTL1, 0);
1740 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_ENABLE);
1741 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_ENABLE);
1743 /* Enable stats counters. */
1744 STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_ENABLE);
1745 /* Clear stats counters. */
1746 ste_stats_clear(sc);
1748 CSR_WRITE_2(sc, STE_COUNTDOWN, 0);
1749 CSR_WRITE_2(sc, STE_ISR, 0xFFFF);
1750 #ifdef DEVICE_POLLING
1751 /* Disable interrupts if we are polling. */
1752 if (ifp->if_capenable & IFCAP_POLLING)
1753 CSR_WRITE_2(sc, STE_IMR, 0);
1756 /* Enable interrupts. */
1757 CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
1759 sc->ste_flags &= ~STE_FLAG_LINK;
1760 /* Switch to the current media. */
1763 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1764 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1766 callout_reset(&sc->ste_callout, hz, ste_tick, sc);
1770 ste_stop(struct ste_softc *sc)
1773 struct ste_chain_onefrag *cur_rx;
1774 struct ste_chain *cur_tx;
1778 STE_LOCK_ASSERT(sc);
1781 callout_stop(&sc->ste_callout);
1783 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE);
1785 CSR_WRITE_2(sc, STE_IMR, 0);
1786 CSR_WRITE_2(sc, STE_COUNTDOWN, 0);
1787 /* Stop pending DMA. */
1788 val = CSR_READ_4(sc, STE_DMACTL);
1789 val |= STE_DMACTL_TXDMA_STALL | STE_DMACTL_RXDMA_STALL;
1790 CSR_WRITE_4(sc, STE_DMACTL, val);
1792 /* Disable auto-polling. */
1793 CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 0);
1794 CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0);
1795 /* Nullify DMA address to stop any further DMA. */
1796 CSR_WRITE_4(sc, STE_RX_DMALIST_PTR, 0);
1797 CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0);
1798 /* Stop TX/RX MAC. */
1799 val = CSR_READ_2(sc, STE_MACCTL1);
1800 val |= STE_MACCTL1_TX_DISABLE | STE_MACCTL1_RX_DISABLE |
1801 STE_MACCTL1_STATS_DISABLE;
1802 CSR_WRITE_2(sc, STE_MACCTL1, val);
1803 for (i = 0; i < STE_TIMEOUT; i++) {
1805 if ((CSR_READ_2(sc, STE_MACCTL1) & (STE_MACCTL1_TX_DISABLE |
1806 STE_MACCTL1_RX_DISABLE | STE_MACCTL1_STATS_DISABLE)) == 0)
1809 if (i == STE_TIMEOUT)
1810 device_printf(sc->ste_dev, "Stopping MAC timed out\n");
1811 /* Acknowledge any pending interrupts. */
1812 CSR_READ_2(sc, STE_ISR_ACK);
1813 ste_stats_update(sc);
1815 for (i = 0; i < STE_RX_LIST_CNT; i++) {
1816 cur_rx = &sc->ste_cdata.ste_rx_chain[i];
1817 if (cur_rx->ste_mbuf != NULL) {
1818 bus_dmamap_sync(sc->ste_cdata.ste_rx_tag,
1819 cur_rx->ste_map, BUS_DMASYNC_POSTREAD);
1820 bus_dmamap_unload(sc->ste_cdata.ste_rx_tag,
1822 m_freem(cur_rx->ste_mbuf);
1823 cur_rx->ste_mbuf = NULL;
1827 for (i = 0; i < STE_TX_LIST_CNT; i++) {
1828 cur_tx = &sc->ste_cdata.ste_tx_chain[i];
1829 if (cur_tx->ste_mbuf != NULL) {
1830 bus_dmamap_sync(sc->ste_cdata.ste_tx_tag,
1831 cur_tx->ste_map, BUS_DMASYNC_POSTWRITE);
1832 bus_dmamap_unload(sc->ste_cdata.ste_tx_tag,
1834 m_freem(cur_tx->ste_mbuf);
1835 cur_tx->ste_mbuf = NULL;
1841 ste_reset(struct ste_softc *sc)
1846 ctl = CSR_READ_4(sc, STE_ASICCTL);
1847 ctl |= STE_ASICCTL_GLOBAL_RESET | STE_ASICCTL_RX_RESET |
1848 STE_ASICCTL_TX_RESET | STE_ASICCTL_DMA_RESET |
1849 STE_ASICCTL_FIFO_RESET | STE_ASICCTL_NETWORK_RESET |
1850 STE_ASICCTL_AUTOINIT_RESET |STE_ASICCTL_HOST_RESET |
1851 STE_ASICCTL_EXTRESET_RESET;
1852 CSR_WRITE_4(sc, STE_ASICCTL, ctl);
1853 CSR_READ_4(sc, STE_ASICCTL);
1855 * Due to the need of accessing EEPROM controller can take
1856 * up to 1ms to complete the global reset.
1860 for (i = 0; i < STE_TIMEOUT; i++) {
1861 if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_RESET_BUSY))
1866 if (i == STE_TIMEOUT)
1867 device_printf(sc->ste_dev, "global reset never completed\n");
1871 ste_restart_tx(struct ste_softc *sc)
1876 for (i = 0; i < STE_TIMEOUT; i++) {
1877 mac = CSR_READ_2(sc, STE_MACCTL1);
1878 mac |= STE_MACCTL1_TX_ENABLE;
1879 CSR_WRITE_2(sc, STE_MACCTL1, mac);
1880 mac = CSR_READ_2(sc, STE_MACCTL1);
1881 if ((mac & STE_MACCTL1_TX_ENABLED) != 0)
1886 if (i == STE_TIMEOUT)
1887 device_printf(sc->ste_dev, "starting Tx failed");
1891 ste_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1893 struct ste_softc *sc;
1895 struct mii_data *mii;
1896 int error = 0, mask;
1899 ifr = (struct ifreq *)data;
1904 if ((ifp->if_flags & IFF_UP) != 0) {
1905 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 &&
1906 ((ifp->if_flags ^ sc->ste_if_flags) &
1907 (IFF_PROMISC | IFF_ALLMULTI)) != 0)
1910 ste_init_locked(sc);
1911 } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1913 sc->ste_if_flags = ifp->if_flags;
1919 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1925 mii = device_get_softc(sc->ste_miibus);
1926 error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1930 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1931 #ifdef DEVICE_POLLING
1932 if ((mask & IFCAP_POLLING) != 0 &&
1933 (IFCAP_POLLING & ifp->if_capabilities) != 0) {
1934 ifp->if_capenable ^= IFCAP_POLLING;
1935 if ((IFCAP_POLLING & ifp->if_capenable) != 0) {
1936 error = ether_poll_register(ste_poll, ifp);
1941 /* Disable interrupts. */
1942 CSR_WRITE_2(sc, STE_IMR, 0);
1944 error = ether_poll_deregister(ifp);
1945 /* Enable interrupts. */
1946 CSR_WRITE_2(sc, STE_IMR, STE_INTRS);
1949 #endif /* DEVICE_POLLING */
1950 if ((mask & IFCAP_WOL_MAGIC) != 0 &&
1951 (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
1952 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
1956 error = ether_ioctl(ifp, command, data);
1964 ste_encap(struct ste_softc *sc, struct mbuf **m_head, struct ste_chain *txc)
1966 struct ste_frag *frag;
1968 struct ste_desc *desc;
1969 bus_dma_segment_t txsegs[STE_MAXFRAGS];
1970 int error, i, nsegs;
1972 STE_LOCK_ASSERT(sc);
1973 M_ASSERTPKTHDR((*m_head));
1975 error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_tx_tag,
1976 txc->ste_map, *m_head, txsegs, &nsegs, 0);
1977 if (error == EFBIG) {
1978 m = m_collapse(*m_head, M_DONTWAIT, STE_MAXFRAGS);
1985 error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_tx_tag,
1986 txc->ste_map, *m_head, txsegs, &nsegs, 0);
1992 } else if (error != 0)
1999 bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, txc->ste_map,
2000 BUS_DMASYNC_PREWRITE);
2002 desc = txc->ste_ptr;
2003 for (i = 0; i < nsegs; i++) {
2004 frag = &desc->ste_frags[i];
2005 frag->ste_addr = htole32(STE_ADDR_LO(txsegs[i].ds_addr));
2006 frag->ste_len = htole32(txsegs[i].ds_len);
2008 desc->ste_frags[i - 1].ste_len |= htole32(STE_FRAG_LAST);
2010 * Because we use Tx polling we can't chain multiple
2011 * Tx descriptors here. Otherwise we race with controller.
2014 if ((sc->ste_cdata.ste_tx_prod % STE_TX_INTR_FRAMES) == 0)
2015 desc->ste_ctl = htole32(STE_TXCTL_ALIGN_DIS |
2018 desc->ste_ctl = htole32(STE_TXCTL_ALIGN_DIS);
2019 txc->ste_mbuf = *m_head;
2020 STE_INC(sc->ste_cdata.ste_tx_prod, STE_TX_LIST_CNT);
2021 sc->ste_cdata.ste_tx_cnt++;
2027 ste_start(struct ifnet *ifp)
2029 struct ste_softc *sc;
2033 ste_start_locked(ifp);
2038 ste_start_locked(struct ifnet *ifp)
2040 struct ste_softc *sc;
2041 struct ste_chain *cur_tx;
2042 struct mbuf *m_head = NULL;
2046 STE_LOCK_ASSERT(sc);
2048 if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2049 IFF_DRV_RUNNING || (sc->ste_flags & STE_FLAG_LINK) == 0)
2052 for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) {
2053 if (sc->ste_cdata.ste_tx_cnt == STE_TX_LIST_CNT - 1) {
2055 * Controller may have cached copy of the last used
2056 * next ptr so we have to reserve one TFD to avoid
2059 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2062 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2065 cur_tx = &sc->ste_cdata.ste_tx_chain[sc->ste_cdata.ste_tx_prod];
2066 if (ste_encap(sc, &m_head, cur_tx) != 0) {
2069 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
2072 if (sc->ste_cdata.ste_last_tx == NULL) {
2073 bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
2074 sc->ste_cdata.ste_tx_list_map,
2075 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2076 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL);
2078 CSR_WRITE_4(sc, STE_TX_DMALIST_PTR,
2079 STE_ADDR_LO(sc->ste_ldata.ste_tx_list_paddr));
2080 CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 64);
2081 STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL);
2084 sc->ste_cdata.ste_last_tx->ste_ptr->ste_next =
2085 sc->ste_cdata.ste_last_tx->ste_phys;
2086 bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag,
2087 sc->ste_cdata.ste_tx_list_map,
2088 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2090 sc->ste_cdata.ste_last_tx = cur_tx;
2094 * If there's a BPF listener, bounce a copy of this frame
2097 BPF_MTAP(ifp, m_head);
2101 sc->ste_timer = STE_TX_TIMEOUT;
2105 ste_watchdog(struct ste_softc *sc)
2110 STE_LOCK_ASSERT(sc);
2112 if (sc->ste_timer == 0 || --sc->ste_timer)
2116 if_printf(ifp, "watchdog timeout\n");
2121 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2122 ste_init_locked(sc);
2124 if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2125 ste_start_locked(ifp);
2129 ste_shutdown(device_t dev)
2132 return (ste_suspend(dev));
2136 ste_suspend(device_t dev)
2138 struct ste_softc *sc;
2140 sc = device_get_softc(dev);
2151 ste_resume(device_t dev)
2153 struct ste_softc *sc;
2158 sc = device_get_softc(dev);
2160 if (pci_find_extcap(sc->ste_dev, PCIY_PMG, &pmc) == 0) {
2161 /* Disable PME and clear PME status. */
2162 pmstat = pci_read_config(sc->ste_dev,
2163 pmc + PCIR_POWER_STATUS, 2);
2164 if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
2165 pmstat &= ~PCIM_PSTAT_PMEENABLE;
2166 pci_write_config(sc->ste_dev,
2167 pmc + PCIR_POWER_STATUS, pmstat, 2);
2171 if ((ifp->if_flags & IFF_UP) != 0) {
2172 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2173 ste_init_locked(sc);
2180 #define STE_SYSCTL_STAT_ADD32(c, h, n, p, d) \
2181 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2182 #define STE_SYSCTL_STAT_ADD64(c, h, n, p, d) \
2183 SYSCTL_ADD_QUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
2186 ste_sysctl_node(struct ste_softc *sc)
2188 struct sysctl_ctx_list *ctx;
2189 struct sysctl_oid_list *child, *parent;
2190 struct sysctl_oid *tree;
2191 struct ste_hw_stats *stats;
2193 stats = &sc->ste_stats;
2194 ctx = device_get_sysctl_ctx(sc->ste_dev);
2195 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ste_dev));
2197 SYSCTL_ADD_INT(ctx, child, OID_AUTO, "int_rx_mod",
2198 CTLFLAG_RW, &sc->ste_int_rx_mod, 0, "ste RX interrupt moderation");
2199 /* Pull in device tunables. */
2200 sc->ste_int_rx_mod = STE_IM_RX_TIMER_DEFAULT;
2201 resource_int_value(device_get_name(sc->ste_dev),
2202 device_get_unit(sc->ste_dev), "int_rx_mod", &sc->ste_int_rx_mod);
2204 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2205 NULL, "STE statistics");
2206 parent = SYSCTL_CHILDREN(tree);
2208 /* Rx statistics. */
2209 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
2210 NULL, "Rx MAC statistics");
2211 child = SYSCTL_CHILDREN(tree);
2212 STE_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
2213 &stats->rx_bytes, "Good octets");
2214 STE_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
2215 &stats->rx_frames, "Good frames");
2216 STE_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
2217 &stats->rx_bcast_frames, "Good broadcast frames");
2218 STE_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
2219 &stats->rx_mcast_frames, "Good multicast frames");
2220 STE_SYSCTL_STAT_ADD32(ctx, child, "lost_frames",
2221 &stats->rx_lost_frames, "Lost frames");
2223 /* Tx statistics. */
2224 tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
2225 NULL, "Tx MAC statistics");
2226 child = SYSCTL_CHILDREN(tree);
2227 STE_SYSCTL_STAT_ADD64(ctx, child, "good_octets",
2228 &stats->tx_bytes, "Good octets");
2229 STE_SYSCTL_STAT_ADD32(ctx, child, "good_frames",
2230 &stats->tx_frames, "Good frames");
2231 STE_SYSCTL_STAT_ADD32(ctx, child, "good_bcast_frames",
2232 &stats->tx_bcast_frames, "Good broadcast frames");
2233 STE_SYSCTL_STAT_ADD32(ctx, child, "good_mcast_frames",
2234 &stats->tx_mcast_frames, "Good multicast frames");
2235 STE_SYSCTL_STAT_ADD32(ctx, child, "carrier_errs",
2236 &stats->tx_carrsense_errs, "Carrier sense errors");
2237 STE_SYSCTL_STAT_ADD32(ctx, child, "single_colls",
2238 &stats->tx_single_colls, "Single collisions");
2239 STE_SYSCTL_STAT_ADD32(ctx, child, "multi_colls",
2240 &stats->tx_multi_colls, "Multiple collisions");
2241 STE_SYSCTL_STAT_ADD32(ctx, child, "late_colls",
2242 &stats->tx_late_colls, "Late collisions");
2243 STE_SYSCTL_STAT_ADD32(ctx, child, "defers",
2244 &stats->tx_frames_defered, "Frames with deferrals");
2245 STE_SYSCTL_STAT_ADD32(ctx, child, "excess_defers",
2246 &stats->tx_excess_defers, "Frames with excessive derferrals");
2247 STE_SYSCTL_STAT_ADD32(ctx, child, "abort",
2248 &stats->tx_abort, "Aborted frames due to Excessive collisions");
2251 #undef STE_SYSCTL_STAT_ADD32
2252 #undef STE_SYSCTL_STAT_ADD64
2255 ste_setwol(struct ste_softc *sc)
2262 STE_LOCK_ASSERT(sc);
2264 if (pci_find_extcap(sc->ste_dev, PCIY_PMG, &pmc) != 0) {
2266 CSR_READ_1(sc, STE_WAKE_EVENT);
2267 CSR_WRITE_1(sc, STE_WAKE_EVENT, 0);
2272 val = CSR_READ_1(sc, STE_WAKE_EVENT);
2273 val &= ~(STE_WAKEEVENT_WAKEPKT_ENB | STE_WAKEEVENT_MAGICPKT_ENB |
2274 STE_WAKEEVENT_LINKEVT_ENB | STE_WAKEEVENT_WAKEONLAN_ENB);
2275 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2276 val |= STE_WAKEEVENT_MAGICPKT_ENB | STE_WAKEEVENT_WAKEONLAN_ENB;
2277 CSR_WRITE_1(sc, STE_WAKE_EVENT, val);
2279 pmstat = pci_read_config(sc->ste_dev, pmc + PCIR_POWER_STATUS, 2);
2280 pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2281 if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2282 pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2283 pci_write_config(sc->ste_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);