1c8befdd5SWarner Losh /*- 2c8befdd5SWarner Losh * Copyright (c) 1997, 1998, 1999 3c8befdd5SWarner Losh * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved. 4c8befdd5SWarner Losh * 5c8befdd5SWarner Losh * Redistribution and use in source and binary forms, with or without 6c8befdd5SWarner Losh * modification, are permitted provided that the following conditions 7c8befdd5SWarner Losh * are met: 8c8befdd5SWarner Losh * 1. Redistributions of source code must retain the above copyright 9c8befdd5SWarner Losh * notice, this list of conditions and the following disclaimer. 10c8befdd5SWarner Losh * 2. Redistributions in binary form must reproduce the above copyright 11c8befdd5SWarner Losh * notice, this list of conditions and the following disclaimer in the 12c8befdd5SWarner Losh * documentation and/or other materials provided with the distribution. 13c8befdd5SWarner Losh * 3. All advertising materials mentioning features or use of this software 14c8befdd5SWarner Losh * must display the following acknowledgement: 15c8befdd5SWarner Losh * This product includes software developed by Bill Paul. 16c8befdd5SWarner Losh * 4. Neither the name of the author nor the names of any co-contributors 17c8befdd5SWarner Losh * may be used to endorse or promote products derived from this software 18c8befdd5SWarner Losh * without specific prior written permission. 19c8befdd5SWarner Losh * 20c8befdd5SWarner Losh * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 21c8befdd5SWarner Losh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22c8befdd5SWarner Losh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23c8befdd5SWarner Losh * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 24c8befdd5SWarner Losh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25c8befdd5SWarner Losh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26c8befdd5SWarner Losh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27c8befdd5SWarner Losh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28c8befdd5SWarner Losh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29c8befdd5SWarner Losh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 30c8befdd5SWarner Losh * THE POSSIBILITY OF SUCH DAMAGE. 31c8befdd5SWarner Losh */ 32c8befdd5SWarner Losh 33c8befdd5SWarner Losh #include <sys/cdefs.h> 34c8befdd5SWarner Losh __FBSDID("$FreeBSD$"); 35c8befdd5SWarner Losh 36c8befdd5SWarner Losh #ifdef HAVE_KERNEL_OPTION_HEADERS 37c8befdd5SWarner Losh #include "opt_device_polling.h" 38c8befdd5SWarner Losh #endif 39c8befdd5SWarner Losh 40c8befdd5SWarner Losh #include <sys/param.h> 41c8befdd5SWarner Losh #include <sys/systm.h> 42a1b2c209SPyun YongHyeon #include <sys/bus.h> 43a1b2c209SPyun YongHyeon #include <sys/endian.h> 44c8befdd5SWarner Losh #include <sys/kernel.h> 45a1b2c209SPyun YongHyeon #include <sys/lock.h> 46a1b2c209SPyun YongHyeon #include <sys/malloc.h> 47a1b2c209SPyun YongHyeon #include <sys/mbuf.h> 48c8befdd5SWarner Losh #include <sys/module.h> 49a1b2c209SPyun YongHyeon #include <sys/rman.h> 50c8befdd5SWarner Losh #include <sys/socket.h> 51a1b2c209SPyun YongHyeon #include <sys/sockio.h> 52c8befdd5SWarner Losh #include <sys/sysctl.h> 53c8befdd5SWarner Losh 54a1b2c209SPyun YongHyeon #include <net/bpf.h> 55c8befdd5SWarner Losh #include <net/if.h> 56c8befdd5SWarner Losh #include <net/if_arp.h> 57c8befdd5SWarner Losh #include <net/ethernet.h> 58c8befdd5SWarner Losh #include <net/if_dl.h> 59c8befdd5SWarner Losh #include <net/if_media.h> 60c8befdd5SWarner Losh #include <net/if_types.h> 61c8befdd5SWarner Losh #include <net/if_vlan_var.h> 62c8befdd5SWarner Losh 63c8befdd5SWarner Losh #include <machine/bus.h> 64c8befdd5SWarner Losh #include <machine/resource.h> 65c8befdd5SWarner Losh 66c8befdd5SWarner Losh #include <dev/mii/mii.h> 67c8befdd5SWarner Losh #include <dev/mii/miivar.h> 68c8befdd5SWarner Losh 69c8befdd5SWarner Losh #include <dev/pci/pcireg.h> 70c8befdd5SWarner Losh #include <dev/pci/pcivar.h> 71c8befdd5SWarner Losh 72a1b2c209SPyun YongHyeon #include <dev/ste/if_stereg.h> 73a1b2c209SPyun YongHyeon 74c8befdd5SWarner Losh /* "device miibus" required. See GENERIC if you get errors here. */ 75c8befdd5SWarner Losh #include "miibus_if.h" 76c8befdd5SWarner Losh 77c8befdd5SWarner Losh MODULE_DEPEND(ste, pci, 1, 1, 1); 78c8befdd5SWarner Losh MODULE_DEPEND(ste, ether, 1, 1, 1); 79c8befdd5SWarner Losh MODULE_DEPEND(ste, miibus, 1, 1, 1); 80c8befdd5SWarner Losh 8181598b3eSPyun YongHyeon /* Define to show Tx error status. */ 8281598b3eSPyun YongHyeon #define STE_SHOW_TXERRORS 8381598b3eSPyun YongHyeon 84c8befdd5SWarner Losh /* 85c8befdd5SWarner Losh * Various supported device vendors/types and their names. 86c8befdd5SWarner Losh */ 87c8befdd5SWarner Losh static struct ste_type ste_devs[] = { 88c8befdd5SWarner Losh { ST_VENDORID, ST_DEVICEID_ST201_1, "Sundance ST201 10/100BaseTX" }, 89c8befdd5SWarner Losh { ST_VENDORID, ST_DEVICEID_ST201_2, "Sundance ST201 10/100BaseTX" }, 90c8befdd5SWarner Losh { DL_VENDORID, DL_DEVICEID_DL10050, "D-Link DL10050 10/100BaseTX" }, 91c8befdd5SWarner Losh { 0, 0, NULL } 92c8befdd5SWarner Losh }; 93c8befdd5SWarner Losh 94c8befdd5SWarner Losh static int ste_attach(device_t); 95c8befdd5SWarner Losh static int ste_detach(device_t); 96084dc54bSPyun YongHyeon static int ste_probe(device_t); 97c8befdd5SWarner Losh static int ste_shutdown(device_t); 98084dc54bSPyun YongHyeon 99a1b2c209SPyun YongHyeon static int ste_dma_alloc(struct ste_softc *); 100a1b2c209SPyun YongHyeon static void ste_dma_free(struct ste_softc *); 101a1b2c209SPyun YongHyeon static void ste_dmamap_cb(void *, bus_dma_segment_t *, int, int); 102084dc54bSPyun YongHyeon static int ste_eeprom_wait(struct ste_softc *); 103a1b2c209SPyun YongHyeon static int ste_encap(struct ste_softc *, struct mbuf **, 104a1b2c209SPyun YongHyeon struct ste_chain *); 105c8befdd5SWarner Losh static int ste_ifmedia_upd(struct ifnet *); 106c8befdd5SWarner Losh static void ste_ifmedia_upd_locked(struct ifnet *); 107c8befdd5SWarner Losh static void ste_ifmedia_sts(struct ifnet *, struct ifmediareq *); 108084dc54bSPyun YongHyeon static void ste_init(void *); 109084dc54bSPyun YongHyeon static void ste_init_locked(struct ste_softc *); 110c8befdd5SWarner Losh static int ste_init_rx_list(struct ste_softc *); 111c8befdd5SWarner Losh static void ste_init_tx_list(struct ste_softc *); 112084dc54bSPyun YongHyeon static void ste_intr(void *); 113084dc54bSPyun YongHyeon static int ste_ioctl(struct ifnet *, u_long, caddr_t); 114084dc54bSPyun YongHyeon static int ste_mii_readreg(struct ste_softc *, struct ste_mii_frame *); 115084dc54bSPyun YongHyeon static void ste_mii_send(struct ste_softc *, uint32_t, int); 116084dc54bSPyun YongHyeon static void ste_mii_sync(struct ste_softc *); 117084dc54bSPyun YongHyeon static int ste_mii_writereg(struct ste_softc *, struct ste_mii_frame *); 118084dc54bSPyun YongHyeon static int ste_miibus_readreg(device_t, int, int); 119084dc54bSPyun YongHyeon static void ste_miibus_statchg(device_t); 120084dc54bSPyun YongHyeon static int ste_miibus_writereg(device_t, int, int, int); 121a1b2c209SPyun YongHyeon static int ste_newbuf(struct ste_softc *, struct ste_chain_onefrag *); 122084dc54bSPyun YongHyeon static int ste_read_eeprom(struct ste_softc *, caddr_t, int, int, int); 123084dc54bSPyun YongHyeon static void ste_reset(struct ste_softc *); 12481598b3eSPyun YongHyeon static void ste_restart_tx(struct ste_softc *); 125a1b2c209SPyun YongHyeon static int ste_rxeof(struct ste_softc *, int); 126931ec15aSPyun YongHyeon static void ste_rxfilter(struct ste_softc *); 127084dc54bSPyun YongHyeon static void ste_start(struct ifnet *); 128084dc54bSPyun YongHyeon static void ste_start_locked(struct ifnet *); 12910f695eeSPyun YongHyeon static void ste_stats_update(struct ste_softc *); 130084dc54bSPyun YongHyeon static void ste_stop(struct ste_softc *); 13110f695eeSPyun YongHyeon static void ste_tick(void *); 132084dc54bSPyun YongHyeon static void ste_txeoc(struct ste_softc *); 133084dc54bSPyun YongHyeon static void ste_txeof(struct ste_softc *); 134084dc54bSPyun YongHyeon static void ste_wait(struct ste_softc *); 135084dc54bSPyun YongHyeon static void ste_watchdog(struct ste_softc *); 136c8befdd5SWarner Losh 137c8befdd5SWarner Losh static device_method_t ste_methods[] = { 138c8befdd5SWarner Losh /* Device interface */ 139c8befdd5SWarner Losh DEVMETHOD(device_probe, ste_probe), 140c8befdd5SWarner Losh DEVMETHOD(device_attach, ste_attach), 141c8befdd5SWarner Losh DEVMETHOD(device_detach, ste_detach), 142c8befdd5SWarner Losh DEVMETHOD(device_shutdown, ste_shutdown), 143c8befdd5SWarner Losh 144c8befdd5SWarner Losh /* bus interface */ 145c8befdd5SWarner Losh DEVMETHOD(bus_print_child, bus_generic_print_child), 146c8befdd5SWarner Losh DEVMETHOD(bus_driver_added, bus_generic_driver_added), 147c8befdd5SWarner Losh 148c8befdd5SWarner Losh /* MII interface */ 149c8befdd5SWarner Losh DEVMETHOD(miibus_readreg, ste_miibus_readreg), 150c8befdd5SWarner Losh DEVMETHOD(miibus_writereg, ste_miibus_writereg), 151c8befdd5SWarner Losh DEVMETHOD(miibus_statchg, ste_miibus_statchg), 152c8befdd5SWarner Losh 153c8befdd5SWarner Losh { 0, 0 } 154c8befdd5SWarner Losh }; 155c8befdd5SWarner Losh 156c8befdd5SWarner Losh static driver_t ste_driver = { 157c8befdd5SWarner Losh "ste", 158c8befdd5SWarner Losh ste_methods, 159c8befdd5SWarner Losh sizeof(struct ste_softc) 160c8befdd5SWarner Losh }; 161c8befdd5SWarner Losh 162c8befdd5SWarner Losh static devclass_t ste_devclass; 163c8befdd5SWarner Losh 164c8befdd5SWarner Losh DRIVER_MODULE(ste, pci, ste_driver, ste_devclass, 0, 0); 165c8befdd5SWarner Losh DRIVER_MODULE(miibus, ste, miibus_driver, miibus_devclass, 0, 0); 166c8befdd5SWarner Losh 167c8befdd5SWarner Losh #define STE_SETBIT4(sc, reg, x) \ 168c8befdd5SWarner Losh CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x)) 169c8befdd5SWarner Losh 170c8befdd5SWarner Losh #define STE_CLRBIT4(sc, reg, x) \ 171c8befdd5SWarner Losh CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x)) 172c8befdd5SWarner Losh 173c8befdd5SWarner Losh #define STE_SETBIT2(sc, reg, x) \ 174c8befdd5SWarner Losh CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) | (x)) 175c8befdd5SWarner Losh 176c8befdd5SWarner Losh #define STE_CLRBIT2(sc, reg, x) \ 177c8befdd5SWarner Losh CSR_WRITE_2(sc, reg, CSR_READ_2(sc, reg) & ~(x)) 178c8befdd5SWarner Losh 179c8befdd5SWarner Losh #define STE_SETBIT1(sc, reg, x) \ 180c8befdd5SWarner Losh CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) | (x)) 181c8befdd5SWarner Losh 182c8befdd5SWarner Losh #define STE_CLRBIT1(sc, reg, x) \ 183c8befdd5SWarner Losh CSR_WRITE_1(sc, reg, CSR_READ_1(sc, reg) & ~(x)) 184c8befdd5SWarner Losh 185c8befdd5SWarner Losh 186c8befdd5SWarner Losh #define MII_SET(x) STE_SETBIT1(sc, STE_PHYCTL, x) 187c8befdd5SWarner Losh #define MII_CLR(x) STE_CLRBIT1(sc, STE_PHYCTL, x) 188c8befdd5SWarner Losh 189c8befdd5SWarner Losh /* 190c8befdd5SWarner Losh * Sync the PHYs by setting data bit and strobing the clock 32 times. 191c8befdd5SWarner Losh */ 192c8befdd5SWarner Losh static void 19360270842SPyun YongHyeon ste_mii_sync(struct ste_softc *sc) 194c8befdd5SWarner Losh { 19542306cb0SPyun YongHyeon int i; 196c8befdd5SWarner Losh 197c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MDIR|STE_PHYCTL_MDATA); 198c8befdd5SWarner Losh 199c8befdd5SWarner Losh for (i = 0; i < 32; i++) { 200c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MCLK); 201c8befdd5SWarner Losh DELAY(1); 202c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MCLK); 203c8befdd5SWarner Losh DELAY(1); 204c8befdd5SWarner Losh } 205c8befdd5SWarner Losh } 206c8befdd5SWarner Losh 207c8befdd5SWarner Losh /* 208c8befdd5SWarner Losh * Clock a series of bits through the MII. 209c8befdd5SWarner Losh */ 210c8befdd5SWarner Losh static void 21156af54f2SPyun YongHyeon ste_mii_send(struct ste_softc *sc, uint32_t bits, int cnt) 212c8befdd5SWarner Losh { 213c8befdd5SWarner Losh int i; 214c8befdd5SWarner Losh 215c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MCLK); 216c8befdd5SWarner Losh 217c8befdd5SWarner Losh for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 218c8befdd5SWarner Losh if (bits & i) { 219c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MDATA); 220c8befdd5SWarner Losh } else { 221c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MDATA); 222c8befdd5SWarner Losh } 223c8befdd5SWarner Losh DELAY(1); 224c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MCLK); 225c8befdd5SWarner Losh DELAY(1); 226c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MCLK); 227c8befdd5SWarner Losh } 228c8befdd5SWarner Losh } 229c8befdd5SWarner Losh 230c8befdd5SWarner Losh /* 231c8befdd5SWarner Losh * Read an PHY register through the MII. 232c8befdd5SWarner Losh */ 233c8befdd5SWarner Losh static int 23460270842SPyun YongHyeon ste_mii_readreg(struct ste_softc *sc, struct ste_mii_frame *frame) 235c8befdd5SWarner Losh { 236c8befdd5SWarner Losh int i, ack; 237c8befdd5SWarner Losh 238c8befdd5SWarner Losh /* 239c8befdd5SWarner Losh * Set up frame for RX. 240c8befdd5SWarner Losh */ 241c8befdd5SWarner Losh frame->mii_stdelim = STE_MII_STARTDELIM; 242c8befdd5SWarner Losh frame->mii_opcode = STE_MII_READOP; 243c8befdd5SWarner Losh frame->mii_turnaround = 0; 244c8befdd5SWarner Losh frame->mii_data = 0; 245c8befdd5SWarner Losh 246c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_PHYCTL, 0); 247c8befdd5SWarner Losh /* 248c8befdd5SWarner Losh * Turn on data xmit. 249c8befdd5SWarner Losh */ 250c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MDIR); 251c8befdd5SWarner Losh 252c8befdd5SWarner Losh ste_mii_sync(sc); 253c8befdd5SWarner Losh 254c8befdd5SWarner Losh /* 255c8befdd5SWarner Losh * Send command/address info. 256c8befdd5SWarner Losh */ 257c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_stdelim, 2); 258c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_opcode, 2); 259c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_phyaddr, 5); 260c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_regaddr, 5); 261c8befdd5SWarner Losh 262c8befdd5SWarner Losh /* Turn off xmit. */ 263c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MDIR); 264c8befdd5SWarner Losh 265c8befdd5SWarner Losh /* Idle bit */ 266c8befdd5SWarner Losh MII_CLR((STE_PHYCTL_MCLK|STE_PHYCTL_MDATA)); 267c8befdd5SWarner Losh DELAY(1); 268c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MCLK); 269c8befdd5SWarner Losh DELAY(1); 270c8befdd5SWarner Losh 271c8befdd5SWarner Losh /* Check for ack */ 272c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MCLK); 273c8befdd5SWarner Losh DELAY(1); 274c8befdd5SWarner Losh ack = CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA; 275c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MCLK); 276c8befdd5SWarner Losh DELAY(1); 277c8befdd5SWarner Losh 278c8befdd5SWarner Losh /* 279c8befdd5SWarner Losh * Now try reading data bits. If the ack failed, we still 280c8befdd5SWarner Losh * need to clock through 16 cycles to keep the PHY(s) in sync. 281c8befdd5SWarner Losh */ 282c8befdd5SWarner Losh if (ack) { 283c8befdd5SWarner Losh for (i = 0; i < 16; i++) { 284c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MCLK); 285c8befdd5SWarner Losh DELAY(1); 286c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MCLK); 287c8befdd5SWarner Losh DELAY(1); 288c8befdd5SWarner Losh } 289c8befdd5SWarner Losh goto fail; 290c8befdd5SWarner Losh } 291c8befdd5SWarner Losh 292c8befdd5SWarner Losh for (i = 0x8000; i; i >>= 1) { 293c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MCLK); 294c8befdd5SWarner Losh DELAY(1); 295c8befdd5SWarner Losh if (!ack) { 296c8befdd5SWarner Losh if (CSR_READ_2(sc, STE_PHYCTL) & STE_PHYCTL_MDATA) 297c8befdd5SWarner Losh frame->mii_data |= i; 298c8befdd5SWarner Losh DELAY(1); 299c8befdd5SWarner Losh } 300c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MCLK); 301c8befdd5SWarner Losh DELAY(1); 302c8befdd5SWarner Losh } 303c8befdd5SWarner Losh 304c8befdd5SWarner Losh fail: 305c8befdd5SWarner Losh 306c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MCLK); 307c8befdd5SWarner Losh DELAY(1); 308c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MCLK); 309c8befdd5SWarner Losh DELAY(1); 310c8befdd5SWarner Losh 311c8befdd5SWarner Losh if (ack) 312c8befdd5SWarner Losh return (1); 313c8befdd5SWarner Losh return (0); 314c8befdd5SWarner Losh } 315c8befdd5SWarner Losh 316c8befdd5SWarner Losh /* 317c8befdd5SWarner Losh * Write to a PHY register through the MII. 318c8befdd5SWarner Losh */ 319c8befdd5SWarner Losh static int 32060270842SPyun YongHyeon ste_mii_writereg(struct ste_softc *sc, struct ste_mii_frame *frame) 321c8befdd5SWarner Losh { 322c8befdd5SWarner Losh 323c8befdd5SWarner Losh /* 324c8befdd5SWarner Losh * Set up frame for TX. 325c8befdd5SWarner Losh */ 326c8befdd5SWarner Losh 327c8befdd5SWarner Losh frame->mii_stdelim = STE_MII_STARTDELIM; 328c8befdd5SWarner Losh frame->mii_opcode = STE_MII_WRITEOP; 329c8befdd5SWarner Losh frame->mii_turnaround = STE_MII_TURNAROUND; 330c8befdd5SWarner Losh 331c8befdd5SWarner Losh /* 332c8befdd5SWarner Losh * Turn on data output. 333c8befdd5SWarner Losh */ 334c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MDIR); 335c8befdd5SWarner Losh 336c8befdd5SWarner Losh ste_mii_sync(sc); 337c8befdd5SWarner Losh 338c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_stdelim, 2); 339c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_opcode, 2); 340c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_phyaddr, 5); 341c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_regaddr, 5); 342c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_turnaround, 2); 343c8befdd5SWarner Losh ste_mii_send(sc, frame->mii_data, 16); 344c8befdd5SWarner Losh 345c8befdd5SWarner Losh /* Idle bit. */ 346c8befdd5SWarner Losh MII_SET(STE_PHYCTL_MCLK); 347c8befdd5SWarner Losh DELAY(1); 348c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MCLK); 349c8befdd5SWarner Losh DELAY(1); 350c8befdd5SWarner Losh 351c8befdd5SWarner Losh /* 352c8befdd5SWarner Losh * Turn off xmit. 353c8befdd5SWarner Losh */ 354c8befdd5SWarner Losh MII_CLR(STE_PHYCTL_MDIR); 355c8befdd5SWarner Losh 356c8befdd5SWarner Losh return (0); 357c8befdd5SWarner Losh } 358c8befdd5SWarner Losh 359c8befdd5SWarner Losh static int 36060270842SPyun YongHyeon ste_miibus_readreg(device_t dev, int phy, int reg) 361c8befdd5SWarner Losh { 362c8befdd5SWarner Losh struct ste_softc *sc; 363c8befdd5SWarner Losh struct ste_mii_frame frame; 364c8befdd5SWarner Losh 365c8befdd5SWarner Losh sc = device_get_softc(dev); 366c8befdd5SWarner Losh 3674465097bSPyun YongHyeon if ((sc->ste_flags & STE_FLAG_ONE_PHY) != 0 && phy != 0) 368c8befdd5SWarner Losh return (0); 369c8befdd5SWarner Losh 370c8befdd5SWarner Losh bzero((char *)&frame, sizeof(frame)); 371c8befdd5SWarner Losh 372c8befdd5SWarner Losh frame.mii_phyaddr = phy; 373c8befdd5SWarner Losh frame.mii_regaddr = reg; 374c8befdd5SWarner Losh ste_mii_readreg(sc, &frame); 375c8befdd5SWarner Losh 376c8befdd5SWarner Losh return (frame.mii_data); 377c8befdd5SWarner Losh } 378c8befdd5SWarner Losh 379c8befdd5SWarner Losh static int 38060270842SPyun YongHyeon ste_miibus_writereg(device_t dev, int phy, int reg, int data) 381c8befdd5SWarner Losh { 382c8befdd5SWarner Losh struct ste_softc *sc; 383c8befdd5SWarner Losh struct ste_mii_frame frame; 384c8befdd5SWarner Losh 385c8befdd5SWarner Losh sc = device_get_softc(dev); 386c8befdd5SWarner Losh bzero((char *)&frame, sizeof(frame)); 387c8befdd5SWarner Losh 388c8befdd5SWarner Losh frame.mii_phyaddr = phy; 389c8befdd5SWarner Losh frame.mii_regaddr = reg; 390c8befdd5SWarner Losh frame.mii_data = data; 391c8befdd5SWarner Losh 392c8befdd5SWarner Losh ste_mii_writereg(sc, &frame); 393c8befdd5SWarner Losh 394c8befdd5SWarner Losh return (0); 395c8befdd5SWarner Losh } 396c8befdd5SWarner Losh 397c8befdd5SWarner Losh static void 39860270842SPyun YongHyeon ste_miibus_statchg(device_t dev) 399c8befdd5SWarner Losh { 400c8befdd5SWarner Losh struct ste_softc *sc; 401c8befdd5SWarner Losh struct mii_data *mii; 40210f695eeSPyun YongHyeon struct ifnet *ifp; 40310f695eeSPyun YongHyeon uint16_t cfg; 404c8befdd5SWarner Losh 405c8befdd5SWarner Losh sc = device_get_softc(dev); 406c8befdd5SWarner Losh 407c8befdd5SWarner Losh mii = device_get_softc(sc->ste_miibus); 40810f695eeSPyun YongHyeon ifp = sc->ste_ifp; 40910f695eeSPyun YongHyeon if (mii == NULL || ifp == NULL || 41010f695eeSPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 41110f695eeSPyun YongHyeon return; 412c8befdd5SWarner Losh 41310f695eeSPyun YongHyeon sc->ste_flags &= ~STE_FLAG_LINK; 41410f695eeSPyun YongHyeon if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) == 41510f695eeSPyun YongHyeon (IFM_ACTIVE | IFM_AVALID)) { 41610f695eeSPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) { 41710f695eeSPyun YongHyeon case IFM_10_T: 41810f695eeSPyun YongHyeon case IFM_100_TX: 41910f695eeSPyun YongHyeon case IFM_100_FX: 42010f695eeSPyun YongHyeon case IFM_100_T4: 42110f695eeSPyun YongHyeon sc->ste_flags |= STE_FLAG_LINK; 42210f695eeSPyun YongHyeon default: 42310f695eeSPyun YongHyeon break; 42410f695eeSPyun YongHyeon } 42510f695eeSPyun YongHyeon } 42610f695eeSPyun YongHyeon 42710f695eeSPyun YongHyeon /* Program MACs with resolved speed/duplex/flow-control. */ 42810f695eeSPyun YongHyeon if ((sc->ste_flags & STE_FLAG_LINK) != 0) { 42910f695eeSPyun YongHyeon cfg = CSR_READ_2(sc, STE_MACCTL0); 43010f695eeSPyun YongHyeon cfg &= ~(STE_MACCTL0_FLOWCTL_ENABLE | STE_MACCTL0_FULLDUPLEX); 43110f695eeSPyun YongHyeon if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { 43210f695eeSPyun YongHyeon /* 43310f695eeSPyun YongHyeon * ST201 data sheet says driver should enable receiving 43410f695eeSPyun YongHyeon * MAC control frames bit of receive mode register to 43510f695eeSPyun YongHyeon * receive flow-control frames but the register has no 43610f695eeSPyun YongHyeon * such bits. In addition the controller has no ability 43710f695eeSPyun YongHyeon * to send pause frames so it should be handled in 43810f695eeSPyun YongHyeon * driver. Implementing pause timer handling in driver 43910f695eeSPyun YongHyeon * layer is not trivial, so don't enable flow-control 44010f695eeSPyun YongHyeon * here. 44110f695eeSPyun YongHyeon */ 44210f695eeSPyun YongHyeon cfg |= STE_MACCTL0_FULLDUPLEX; 44310f695eeSPyun YongHyeon } 44410f695eeSPyun YongHyeon CSR_WRITE_2(sc, STE_MACCTL0, cfg); 445c8befdd5SWarner Losh } 446c8befdd5SWarner Losh } 447c8befdd5SWarner Losh 448c8befdd5SWarner Losh static int 44960270842SPyun YongHyeon ste_ifmedia_upd(struct ifnet *ifp) 450c8befdd5SWarner Losh { 451c8befdd5SWarner Losh struct ste_softc *sc; 452c8befdd5SWarner Losh 453c8befdd5SWarner Losh sc = ifp->if_softc; 454c8befdd5SWarner Losh STE_LOCK(sc); 455c8befdd5SWarner Losh ste_ifmedia_upd_locked(ifp); 456c8befdd5SWarner Losh STE_UNLOCK(sc); 457c8befdd5SWarner Losh 458c8befdd5SWarner Losh return (0); 459c8befdd5SWarner Losh } 460c8befdd5SWarner Losh 461c8befdd5SWarner Losh static void 46260270842SPyun YongHyeon ste_ifmedia_upd_locked(struct ifnet *ifp) 463c8befdd5SWarner Losh { 464c8befdd5SWarner Losh struct ste_softc *sc; 465c8befdd5SWarner Losh struct mii_data *mii; 466c8befdd5SWarner Losh 467c8befdd5SWarner Losh sc = ifp->if_softc; 468c8befdd5SWarner Losh STE_LOCK_ASSERT(sc); 469c8befdd5SWarner Losh mii = device_get_softc(sc->ste_miibus); 4704465097bSPyun YongHyeon sc->ste_flags &= ~STE_FLAG_LINK; 471c8befdd5SWarner Losh if (mii->mii_instance) { 472c8befdd5SWarner Losh struct mii_softc *miisc; 473c8befdd5SWarner Losh LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 474c8befdd5SWarner Losh mii_phy_reset(miisc); 475c8befdd5SWarner Losh } 476c8befdd5SWarner Losh mii_mediachg(mii); 477c8befdd5SWarner Losh } 478c8befdd5SWarner Losh 479c8befdd5SWarner Losh static void 48060270842SPyun YongHyeon ste_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr) 481c8befdd5SWarner Losh { 482c8befdd5SWarner Losh struct ste_softc *sc; 483c8befdd5SWarner Losh struct mii_data *mii; 484c8befdd5SWarner Losh 485c8befdd5SWarner Losh sc = ifp->if_softc; 486c8befdd5SWarner Losh mii = device_get_softc(sc->ste_miibus); 487c8befdd5SWarner Losh 488c8befdd5SWarner Losh STE_LOCK(sc); 489c8befdd5SWarner Losh mii_pollstat(mii); 490c8befdd5SWarner Losh ifmr->ifm_active = mii->mii_media_active; 491c8befdd5SWarner Losh ifmr->ifm_status = mii->mii_media_status; 492c8befdd5SWarner Losh STE_UNLOCK(sc); 493c8befdd5SWarner Losh } 494c8befdd5SWarner Losh 495c8befdd5SWarner Losh static void 49660270842SPyun YongHyeon ste_wait(struct ste_softc *sc) 497c8befdd5SWarner Losh { 49842306cb0SPyun YongHyeon int i; 499c8befdd5SWarner Losh 500c8befdd5SWarner Losh for (i = 0; i < STE_TIMEOUT; i++) { 501c8befdd5SWarner Losh if (!(CSR_READ_4(sc, STE_DMACTL) & STE_DMACTL_DMA_HALTINPROG)) 502c8befdd5SWarner Losh break; 5031bf71544SPyun YongHyeon DELAY(1); 504c8befdd5SWarner Losh } 505c8befdd5SWarner Losh 506c8befdd5SWarner Losh if (i == STE_TIMEOUT) 507c8befdd5SWarner Losh device_printf(sc->ste_dev, "command never completed!\n"); 508c8befdd5SWarner Losh } 509c8befdd5SWarner Losh 510c8befdd5SWarner Losh /* 511c8befdd5SWarner Losh * The EEPROM is slow: give it time to come ready after issuing 512c8befdd5SWarner Losh * it a command. 513c8befdd5SWarner Losh */ 514c8befdd5SWarner Losh static int 51560270842SPyun YongHyeon ste_eeprom_wait(struct ste_softc *sc) 516c8befdd5SWarner Losh { 517c8befdd5SWarner Losh int i; 518c8befdd5SWarner Losh 519c8befdd5SWarner Losh DELAY(1000); 520c8befdd5SWarner Losh 521c8befdd5SWarner Losh for (i = 0; i < 100; i++) { 522c8befdd5SWarner Losh if (CSR_READ_2(sc, STE_EEPROM_CTL) & STE_EECTL_BUSY) 523c8befdd5SWarner Losh DELAY(1000); 524c8befdd5SWarner Losh else 525c8befdd5SWarner Losh break; 526c8befdd5SWarner Losh } 527c8befdd5SWarner Losh 528c8befdd5SWarner Losh if (i == 100) { 529c8befdd5SWarner Losh device_printf(sc->ste_dev, "eeprom failed to come ready\n"); 530c8befdd5SWarner Losh return (1); 531c8befdd5SWarner Losh } 532c8befdd5SWarner Losh 533c8befdd5SWarner Losh return (0); 534c8befdd5SWarner Losh } 535c8befdd5SWarner Losh 536c8befdd5SWarner Losh /* 537c8befdd5SWarner Losh * Read a sequence of words from the EEPROM. Note that ethernet address 538c8befdd5SWarner Losh * data is stored in the EEPROM in network byte order. 539c8befdd5SWarner Losh */ 540c8befdd5SWarner Losh static int 54160270842SPyun YongHyeon ste_read_eeprom(struct ste_softc *sc, caddr_t dest, int off, int cnt, int swap) 542c8befdd5SWarner Losh { 543f2632c3bSPyun YongHyeon uint16_t word, *ptr; 544c8befdd5SWarner Losh int err = 0, i; 545c8befdd5SWarner Losh 546c8befdd5SWarner Losh if (ste_eeprom_wait(sc)) 547c8befdd5SWarner Losh return (1); 548c8befdd5SWarner Losh 549c8befdd5SWarner Losh for (i = 0; i < cnt; i++) { 550c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_EEPROM_CTL, STE_EEOPCODE_READ | (off + i)); 551c8befdd5SWarner Losh err = ste_eeprom_wait(sc); 552c8befdd5SWarner Losh if (err) 553c8befdd5SWarner Losh break; 554c8befdd5SWarner Losh word = CSR_READ_2(sc, STE_EEPROM_DATA); 55556af54f2SPyun YongHyeon ptr = (uint16_t *)(dest + (i * 2)); 556c8befdd5SWarner Losh if (swap) 557c8befdd5SWarner Losh *ptr = ntohs(word); 558c8befdd5SWarner Losh else 559c8befdd5SWarner Losh *ptr = word; 560c8befdd5SWarner Losh } 561c8befdd5SWarner Losh 562c8befdd5SWarner Losh return (err ? 1 : 0); 563c8befdd5SWarner Losh } 564c8befdd5SWarner Losh 565c8befdd5SWarner Losh static void 566931ec15aSPyun YongHyeon ste_rxfilter(struct ste_softc *sc) 567c8befdd5SWarner Losh { 568c8befdd5SWarner Losh struct ifnet *ifp; 569c8befdd5SWarner Losh struct ifmultiaddr *ifma; 570f2632c3bSPyun YongHyeon uint32_t hashes[2] = { 0, 0 }; 571931ec15aSPyun YongHyeon uint8_t rxcfg; 572f2632c3bSPyun YongHyeon int h; 573c8befdd5SWarner Losh 574931ec15aSPyun YongHyeon STE_LOCK_ASSERT(sc); 575931ec15aSPyun YongHyeon 576c8befdd5SWarner Losh ifp = sc->ste_ifp; 577931ec15aSPyun YongHyeon rxcfg = CSR_READ_1(sc, STE_RX_MODE); 578931ec15aSPyun YongHyeon rxcfg |= STE_RXMODE_UNICAST; 579931ec15aSPyun YongHyeon rxcfg &= ~(STE_RXMODE_ALLMULTI | STE_RXMODE_MULTIHASH | 580931ec15aSPyun YongHyeon STE_RXMODE_BROADCAST | STE_RXMODE_PROMISC); 581931ec15aSPyun YongHyeon if (ifp->if_flags & IFF_BROADCAST) 582931ec15aSPyun YongHyeon rxcfg |= STE_RXMODE_BROADCAST; 583931ec15aSPyun YongHyeon if ((ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) != 0) { 584931ec15aSPyun YongHyeon if ((ifp->if_flags & IFF_ALLMULTI) != 0) 585931ec15aSPyun YongHyeon rxcfg |= STE_RXMODE_ALLMULTI; 586931ec15aSPyun YongHyeon if ((ifp->if_flags & IFF_PROMISC) != 0) 587931ec15aSPyun YongHyeon rxcfg |= STE_RXMODE_PROMISC; 588931ec15aSPyun YongHyeon goto chipit; 589c8befdd5SWarner Losh } 590c8befdd5SWarner Losh 591931ec15aSPyun YongHyeon rxcfg |= STE_RXMODE_MULTIHASH; 592931ec15aSPyun YongHyeon /* Now program new ones. */ 593eb956cd0SRobert Watson if_maddr_rlock(ifp); 594c8befdd5SWarner Losh TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 595c8befdd5SWarner Losh if (ifma->ifma_addr->sa_family != AF_LINK) 596c8befdd5SWarner Losh continue; 597c8befdd5SWarner Losh h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 598c8befdd5SWarner Losh ifma->ifma_addr), ETHER_ADDR_LEN) & 0x3F; 599c8befdd5SWarner Losh if (h < 32) 600c8befdd5SWarner Losh hashes[0] |= (1 << h); 601c8befdd5SWarner Losh else 602c8befdd5SWarner Losh hashes[1] |= (1 << (h - 32)); 603c8befdd5SWarner Losh } 604eb956cd0SRobert Watson if_maddr_runlock(ifp); 605c8befdd5SWarner Losh 606931ec15aSPyun YongHyeon chipit: 607c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_MAR0, hashes[0] & 0xFFFF); 608c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_MAR1, (hashes[0] >> 16) & 0xFFFF); 609c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_MAR2, hashes[1] & 0xFFFF); 610c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_MAR3, (hashes[1] >> 16) & 0xFFFF); 611931ec15aSPyun YongHyeon CSR_WRITE_1(sc, STE_RX_MODE, rxcfg); 612931ec15aSPyun YongHyeon CSR_READ_1(sc, STE_RX_MODE); 613c8befdd5SWarner Losh } 614c8befdd5SWarner Losh 615c8befdd5SWarner Losh #ifdef DEVICE_POLLING 616c8befdd5SWarner Losh static poll_handler_t ste_poll, ste_poll_locked; 617c8befdd5SWarner Losh 6181abcdbd1SAttilio Rao static int 619c8befdd5SWarner Losh ste_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 620c8befdd5SWarner Losh { 621c8befdd5SWarner Losh struct ste_softc *sc = ifp->if_softc; 6221abcdbd1SAttilio Rao int rx_npkts = 0; 623c8befdd5SWarner Losh 624c8befdd5SWarner Losh STE_LOCK(sc); 625c8befdd5SWarner Losh if (ifp->if_drv_flags & IFF_DRV_RUNNING) 6261abcdbd1SAttilio Rao rx_npkts = ste_poll_locked(ifp, cmd, count); 627c8befdd5SWarner Losh STE_UNLOCK(sc); 6281abcdbd1SAttilio Rao return (rx_npkts); 629c8befdd5SWarner Losh } 630c8befdd5SWarner Losh 6311abcdbd1SAttilio Rao static int 632c8befdd5SWarner Losh ste_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count) 633c8befdd5SWarner Losh { 634c8befdd5SWarner Losh struct ste_softc *sc = ifp->if_softc; 6351abcdbd1SAttilio Rao int rx_npkts; 636c8befdd5SWarner Losh 637c8befdd5SWarner Losh STE_LOCK_ASSERT(sc); 638c8befdd5SWarner Losh 639a1b2c209SPyun YongHyeon rx_npkts = ste_rxeof(sc, count); 640c8befdd5SWarner Losh ste_txeof(sc); 64181598b3eSPyun YongHyeon ste_txeoc(sc); 642c8befdd5SWarner Losh if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 643c8befdd5SWarner Losh ste_start_locked(ifp); 644c8befdd5SWarner Losh 645c8befdd5SWarner Losh if (cmd == POLL_AND_CHECK_STATUS) { 64656af54f2SPyun YongHyeon uint16_t status; 647c8befdd5SWarner Losh 648c8befdd5SWarner Losh status = CSR_READ_2(sc, STE_ISR_ACK); 649c8befdd5SWarner Losh 65010f695eeSPyun YongHyeon if (status & STE_ISR_STATS_OFLOW) 651c8befdd5SWarner Losh ste_stats_update(sc); 652c8befdd5SWarner Losh 65355d7003eSPyun YongHyeon if (status & STE_ISR_HOSTERR) { 65455d7003eSPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 655c8befdd5SWarner Losh ste_init_locked(sc); 656c8befdd5SWarner Losh } 65755d7003eSPyun YongHyeon } 6581abcdbd1SAttilio Rao return (rx_npkts); 659c8befdd5SWarner Losh } 660c8befdd5SWarner Losh #endif /* DEVICE_POLLING */ 661c8befdd5SWarner Losh 662c8befdd5SWarner Losh static void 66360270842SPyun YongHyeon ste_intr(void *xsc) 664c8befdd5SWarner Losh { 665c8befdd5SWarner Losh struct ste_softc *sc; 666c8befdd5SWarner Losh struct ifnet *ifp; 66756af54f2SPyun YongHyeon uint16_t status; 668c8befdd5SWarner Losh 669c8befdd5SWarner Losh sc = xsc; 670c8befdd5SWarner Losh STE_LOCK(sc); 671c8befdd5SWarner Losh ifp = sc->ste_ifp; 672c8befdd5SWarner Losh 673c8befdd5SWarner Losh #ifdef DEVICE_POLLING 674c8befdd5SWarner Losh if (ifp->if_capenable & IFCAP_POLLING) { 675c8befdd5SWarner Losh STE_UNLOCK(sc); 676c8befdd5SWarner Losh return; 677c8befdd5SWarner Losh } 678c8befdd5SWarner Losh #endif 679c8befdd5SWarner Losh 680c8befdd5SWarner Losh /* See if this is really our interrupt. */ 681c8befdd5SWarner Losh if (!(CSR_READ_2(sc, STE_ISR) & STE_ISR_INTLATCH)) { 682c8befdd5SWarner Losh STE_UNLOCK(sc); 683c8befdd5SWarner Losh return; 684c8befdd5SWarner Losh } 685c8befdd5SWarner Losh 686c8befdd5SWarner Losh for (;;) { 687c8befdd5SWarner Losh status = CSR_READ_2(sc, STE_ISR_ACK); 688c8befdd5SWarner Losh 689c8befdd5SWarner Losh if (!(status & STE_INTRS)) 690c8befdd5SWarner Losh break; 691c8befdd5SWarner Losh 692a1b2c209SPyun YongHyeon if (status & STE_ISR_RX_DMADONE) 693a1b2c209SPyun YongHyeon ste_rxeof(sc, -1); 694c8befdd5SWarner Losh 695c8befdd5SWarner Losh if (status & STE_ISR_TX_DMADONE) 696c8befdd5SWarner Losh ste_txeof(sc); 697c8befdd5SWarner Losh 698c8befdd5SWarner Losh if (status & STE_ISR_TX_DONE) 699c8befdd5SWarner Losh ste_txeoc(sc); 700c8befdd5SWarner Losh 70110f695eeSPyun YongHyeon if (status & STE_ISR_STATS_OFLOW) 702c8befdd5SWarner Losh ste_stats_update(sc); 703c8befdd5SWarner Losh 70455d7003eSPyun YongHyeon if (status & STE_ISR_HOSTERR) { 705c8befdd5SWarner Losh ste_init_locked(sc); 70655d7003eSPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 70755d7003eSPyun YongHyeon } 708c8befdd5SWarner Losh } 709c8befdd5SWarner Losh 710c8befdd5SWarner Losh /* Re-enable interrupts */ 711c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_IMR, STE_INTRS); 712c8befdd5SWarner Losh 713c8befdd5SWarner Losh if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 714c8befdd5SWarner Losh ste_start_locked(ifp); 715c8befdd5SWarner Losh 716c8befdd5SWarner Losh STE_UNLOCK(sc); 717c8befdd5SWarner Losh } 718c8befdd5SWarner Losh 719c8befdd5SWarner Losh /* 720c8befdd5SWarner Losh * A frame has been uploaded: pass the resulting mbuf chain up to 721c8befdd5SWarner Losh * the higher level protocols. 722c8befdd5SWarner Losh */ 7231abcdbd1SAttilio Rao static int 724a1b2c209SPyun YongHyeon ste_rxeof(struct ste_softc *sc, int count) 725c8befdd5SWarner Losh { 726c8befdd5SWarner Losh struct mbuf *m; 727c8befdd5SWarner Losh struct ifnet *ifp; 728c8befdd5SWarner Losh struct ste_chain_onefrag *cur_rx; 72956af54f2SPyun YongHyeon uint32_t rxstat; 730a1b2c209SPyun YongHyeon int total_len, rx_npkts; 731c8befdd5SWarner Losh 732c8befdd5SWarner Losh ifp = sc->ste_ifp; 733c8befdd5SWarner Losh 734a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag, 735a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_list_map, 736a1b2c209SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 737c8befdd5SWarner Losh 738c8befdd5SWarner Losh cur_rx = sc->ste_cdata.ste_rx_head; 739a1b2c209SPyun YongHyeon for (rx_npkts = 0; rx_npkts < STE_RX_LIST_CNT; rx_npkts++, 740a1b2c209SPyun YongHyeon cur_rx = cur_rx->ste_next) { 741a1b2c209SPyun YongHyeon rxstat = le32toh(cur_rx->ste_ptr->ste_status); 742a1b2c209SPyun YongHyeon if ((rxstat & STE_RXSTAT_DMADONE) == 0) 743a1b2c209SPyun YongHyeon break; 744a1b2c209SPyun YongHyeon #ifdef DEVICE_POLLING 745a1b2c209SPyun YongHyeon if (ifp->if_capenable & IFCAP_POLLING) { 746a1b2c209SPyun YongHyeon if (count == 0) 747a1b2c209SPyun YongHyeon break; 748a1b2c209SPyun YongHyeon count--; 749a1b2c209SPyun YongHyeon } 750a1b2c209SPyun YongHyeon #endif 751a1b2c209SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 752a1b2c209SPyun YongHyeon break; 753c8befdd5SWarner Losh /* 754c8befdd5SWarner Losh * If an error occurs, update stats, clear the 755c8befdd5SWarner Losh * status word and leave the mbuf cluster in place: 756c8befdd5SWarner Losh * it should simply get re-used next time this descriptor 757c8befdd5SWarner Losh * comes up in the ring. 758c8befdd5SWarner Losh */ 759c8befdd5SWarner Losh if (rxstat & STE_RXSTAT_FRAME_ERR) { 760c8befdd5SWarner Losh ifp->if_ierrors++; 761c8befdd5SWarner Losh cur_rx->ste_ptr->ste_status = 0; 762c8befdd5SWarner Losh continue; 763c8befdd5SWarner Losh } 764c8befdd5SWarner Losh 765c8befdd5SWarner Losh /* No errors; receive the packet. */ 766c8befdd5SWarner Losh m = cur_rx->ste_mbuf; 767a1b2c209SPyun YongHyeon total_len = STE_RX_BYTES(rxstat); 768c8befdd5SWarner Losh 769c8befdd5SWarner Losh /* 770c8befdd5SWarner Losh * Try to conjure up a new mbuf cluster. If that 771c8befdd5SWarner Losh * fails, it means we have an out of memory condition and 772c8befdd5SWarner Losh * should leave the buffer in place and continue. This will 773c8befdd5SWarner Losh * result in a lost packet, but there's little else we 774c8befdd5SWarner Losh * can do in this situation. 775c8befdd5SWarner Losh */ 776a1b2c209SPyun YongHyeon if (ste_newbuf(sc, cur_rx) != 0) { 777c8befdd5SWarner Losh ifp->if_ierrors++; 778c8befdd5SWarner Losh cur_rx->ste_ptr->ste_status = 0; 779c8befdd5SWarner Losh continue; 780c8befdd5SWarner Losh } 781c8befdd5SWarner Losh 782c8befdd5SWarner Losh m->m_pkthdr.rcvif = ifp; 783c8befdd5SWarner Losh m->m_pkthdr.len = m->m_len = total_len; 784c8befdd5SWarner Losh 785c8befdd5SWarner Losh ifp->if_ipackets++; 786c8befdd5SWarner Losh STE_UNLOCK(sc); 787c8befdd5SWarner Losh (*ifp->if_input)(ifp, m); 788c8befdd5SWarner Losh STE_LOCK(sc); 789a1b2c209SPyun YongHyeon } 790c8befdd5SWarner Losh 791a1b2c209SPyun YongHyeon if (rx_npkts > 0) { 792a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_head = cur_rx; 793a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag, 794a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_list_map, 795a1b2c209SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 796c8befdd5SWarner Losh } 797c8befdd5SWarner Losh 7981abcdbd1SAttilio Rao return (rx_npkts); 799c8befdd5SWarner Losh } 800c8befdd5SWarner Losh 801c8befdd5SWarner Losh static void 80260270842SPyun YongHyeon ste_txeoc(struct ste_softc *sc) 803c8befdd5SWarner Losh { 80481598b3eSPyun YongHyeon uint16_t txstat; 805c8befdd5SWarner Losh struct ifnet *ifp; 80681598b3eSPyun YongHyeon 80781598b3eSPyun YongHyeon STE_LOCK_ASSERT(sc); 808c8befdd5SWarner Losh 809c8befdd5SWarner Losh ifp = sc->ste_ifp; 810c8befdd5SWarner Losh 81181598b3eSPyun YongHyeon /* 81281598b3eSPyun YongHyeon * STE_TX_STATUS register implements a queue of up to 31 81381598b3eSPyun YongHyeon * transmit status byte. Writing an arbitrary value to the 81481598b3eSPyun YongHyeon * register will advance the queue to the next transmit 81581598b3eSPyun YongHyeon * status byte. This means if driver does not read 81681598b3eSPyun YongHyeon * STE_TX_STATUS register after completing sending more 81781598b3eSPyun YongHyeon * than 31 frames the controller would be stalled so driver 81881598b3eSPyun YongHyeon * should re-wake the Tx MAC. This is the most severe 81981598b3eSPyun YongHyeon * limitation of ST201 based controller. 82081598b3eSPyun YongHyeon */ 82181598b3eSPyun YongHyeon for (;;) { 82281598b3eSPyun YongHyeon txstat = CSR_READ_2(sc, STE_TX_STATUS); 82381598b3eSPyun YongHyeon if ((txstat & STE_TXSTATUS_TXDONE) == 0) 82481598b3eSPyun YongHyeon break; 82581598b3eSPyun YongHyeon if ((txstat & (STE_TXSTATUS_UNDERRUN | 82681598b3eSPyun YongHyeon STE_TXSTATUS_EXCESSCOLLS | STE_TXSTATUS_RECLAIMERR | 82781598b3eSPyun YongHyeon STE_TXSTATUS_STATSOFLOW)) != 0) { 828c8befdd5SWarner Losh ifp->if_oerrors++; 82981598b3eSPyun YongHyeon #ifdef STE_SHOW_TXERRORS 83081598b3eSPyun YongHyeon device_printf(sc->ste_dev, "TX error : 0x%b\n", 83181598b3eSPyun YongHyeon txstat & 0xFF, STE_ERR_BITS); 83281598b3eSPyun YongHyeon #endif 83381598b3eSPyun YongHyeon if ((txstat & STE_TXSTATUS_UNDERRUN) != 0 && 834c8befdd5SWarner Losh sc->ste_tx_thresh < STE_PACKET_SIZE) { 835c8befdd5SWarner Losh sc->ste_tx_thresh += STE_MIN_FRAMELEN; 83681598b3eSPyun YongHyeon if (sc->ste_tx_thresh > STE_PACKET_SIZE) 83781598b3eSPyun YongHyeon sc->ste_tx_thresh = STE_PACKET_SIZE; 838c8befdd5SWarner Losh device_printf(sc->ste_dev, 83981598b3eSPyun YongHyeon "TX underrun, increasing TX" 840c8befdd5SWarner Losh " start threshold to %d bytes\n", 841c8befdd5SWarner Losh sc->ste_tx_thresh); 84281598b3eSPyun YongHyeon /* Make sure to disable active DMA cycles. */ 84381598b3eSPyun YongHyeon STE_SETBIT4(sc, STE_DMACTL, 84481598b3eSPyun YongHyeon STE_DMACTL_TXDMA_STALL); 84581598b3eSPyun YongHyeon ste_wait(sc); 84655d7003eSPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 847c8befdd5SWarner Losh ste_init_locked(sc); 84881598b3eSPyun YongHyeon break; 84981598b3eSPyun YongHyeon } 85081598b3eSPyun YongHyeon /* Restart Tx. */ 85181598b3eSPyun YongHyeon ste_restart_tx(sc); 85281598b3eSPyun YongHyeon } 85381598b3eSPyun YongHyeon /* 85481598b3eSPyun YongHyeon * Advance to next status and ACK TxComplete 85581598b3eSPyun YongHyeon * interrupt. ST201 data sheet was wrong here, to 85681598b3eSPyun YongHyeon * get next Tx status, we have to write both 85781598b3eSPyun YongHyeon * STE_TX_STATUS and STE_TX_FRAMEID register. 85881598b3eSPyun YongHyeon * Otherwise controller returns the same status 85981598b3eSPyun YongHyeon * as well as not acknowledge Tx completion 86081598b3eSPyun YongHyeon * interrupt. 86181598b3eSPyun YongHyeon */ 862c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_TX_STATUS, txstat); 863c8befdd5SWarner Losh } 864c8befdd5SWarner Losh } 865c8befdd5SWarner Losh 866c8befdd5SWarner Losh static void 86710f695eeSPyun YongHyeon ste_tick(void *arg) 86810f695eeSPyun YongHyeon { 86910f695eeSPyun YongHyeon struct ste_softc *sc; 87010f695eeSPyun YongHyeon struct mii_data *mii; 87110f695eeSPyun YongHyeon 87210f695eeSPyun YongHyeon sc = (struct ste_softc *)arg; 87310f695eeSPyun YongHyeon 87410f695eeSPyun YongHyeon STE_LOCK_ASSERT(sc); 87510f695eeSPyun YongHyeon 87610f695eeSPyun YongHyeon mii = device_get_softc(sc->ste_miibus); 87710f695eeSPyun YongHyeon mii_tick(mii); 87810f695eeSPyun YongHyeon /* 87910f695eeSPyun YongHyeon * ukphy(4) does not seem to generate CB that reports 88010f695eeSPyun YongHyeon * resolved link state so if we know we lost a link, 88110f695eeSPyun YongHyeon * explicitly check the link state. 88210f695eeSPyun YongHyeon */ 88310f695eeSPyun YongHyeon if ((sc->ste_flags & STE_FLAG_LINK) == 0) 88410f695eeSPyun YongHyeon ste_miibus_statchg(sc->ste_dev); 88510f695eeSPyun YongHyeon ste_stats_update(sc); 88610f695eeSPyun YongHyeon ste_watchdog(sc); 88710f695eeSPyun YongHyeon callout_reset(&sc->ste_callout, hz, ste_tick, sc); 88810f695eeSPyun YongHyeon } 88910f695eeSPyun YongHyeon 89010f695eeSPyun YongHyeon static void 89160270842SPyun YongHyeon ste_txeof(struct ste_softc *sc) 892c8befdd5SWarner Losh { 893c8befdd5SWarner Losh struct ifnet *ifp; 894f2632c3bSPyun YongHyeon struct ste_chain *cur_tx; 895a1b2c209SPyun YongHyeon uint32_t txstat; 896c8befdd5SWarner Losh int idx; 897c8befdd5SWarner Losh 898a1b2c209SPyun YongHyeon STE_LOCK_ASSERT(sc); 899c8befdd5SWarner Losh 900a1b2c209SPyun YongHyeon ifp = sc->ste_ifp; 901c8befdd5SWarner Losh idx = sc->ste_cdata.ste_tx_cons; 902a1b2c209SPyun YongHyeon if (idx == sc->ste_cdata.ste_tx_prod) 903a1b2c209SPyun YongHyeon return; 904a1b2c209SPyun YongHyeon 905a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag, 906a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_map, 907a1b2c209SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 908a1b2c209SPyun YongHyeon 909c8befdd5SWarner Losh while (idx != sc->ste_cdata.ste_tx_prod) { 910c8befdd5SWarner Losh cur_tx = &sc->ste_cdata.ste_tx_chain[idx]; 911a1b2c209SPyun YongHyeon txstat = le32toh(cur_tx->ste_ptr->ste_ctl); 912a1b2c209SPyun YongHyeon if ((txstat & STE_TXCTL_DMADONE) == 0) 913c8befdd5SWarner Losh break; 914a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, cur_tx->ste_map, 915a1b2c209SPyun YongHyeon BUS_DMASYNC_POSTWRITE); 916a1b2c209SPyun YongHyeon bus_dmamap_unload(sc->ste_cdata.ste_tx_tag, cur_tx->ste_map); 917a1b2c209SPyun YongHyeon KASSERT(cur_tx->ste_mbuf != NULL, 918a1b2c209SPyun YongHyeon ("%s: freeing NULL mbuf!\n", __func__)); 919c8befdd5SWarner Losh m_freem(cur_tx->ste_mbuf); 920c8befdd5SWarner Losh cur_tx->ste_mbuf = NULL; 921c8befdd5SWarner Losh ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 922c8befdd5SWarner Losh ifp->if_opackets++; 923a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_cnt--; 924c8befdd5SWarner Losh STE_INC(idx, STE_TX_LIST_CNT); 925c8befdd5SWarner Losh } 926c8befdd5SWarner Losh 927c8befdd5SWarner Losh sc->ste_cdata.ste_tx_cons = idx; 928a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_tx_cnt == 0) 9297cf545d0SJohn Baldwin sc->ste_timer = 0; 930c8befdd5SWarner Losh } 931c8befdd5SWarner Losh 932c8befdd5SWarner Losh static void 93310f695eeSPyun YongHyeon ste_stats_update(struct ste_softc *sc) 934c8befdd5SWarner Losh { 935c8befdd5SWarner Losh struct ifnet *ifp; 936c8befdd5SWarner Losh 937c8befdd5SWarner Losh STE_LOCK_ASSERT(sc); 938c8befdd5SWarner Losh 939c8befdd5SWarner Losh ifp = sc->ste_ifp; 940c8befdd5SWarner Losh ifp->if_collisions += CSR_READ_1(sc, STE_LATE_COLLS) 941c8befdd5SWarner Losh + CSR_READ_1(sc, STE_MULTI_COLLS) 942c8befdd5SWarner Losh + CSR_READ_1(sc, STE_SINGLE_COLLS); 943c8befdd5SWarner Losh } 944c8befdd5SWarner Losh 945c8befdd5SWarner Losh /* 946c8befdd5SWarner Losh * Probe for a Sundance ST201 chip. Check the PCI vendor and device 947c8befdd5SWarner Losh * IDs against our list and return a device name if we find a match. 948c8befdd5SWarner Losh */ 949c8befdd5SWarner Losh static int 95060270842SPyun YongHyeon ste_probe(device_t dev) 951c8befdd5SWarner Losh { 952c8befdd5SWarner Losh struct ste_type *t; 953c8befdd5SWarner Losh 954c8befdd5SWarner Losh t = ste_devs; 955c8befdd5SWarner Losh 956c8befdd5SWarner Losh while (t->ste_name != NULL) { 957c8befdd5SWarner Losh if ((pci_get_vendor(dev) == t->ste_vid) && 958c8befdd5SWarner Losh (pci_get_device(dev) == t->ste_did)) { 959c8befdd5SWarner Losh device_set_desc(dev, t->ste_name); 960c8befdd5SWarner Losh return (BUS_PROBE_DEFAULT); 961c8befdd5SWarner Losh } 962c8befdd5SWarner Losh t++; 963c8befdd5SWarner Losh } 964c8befdd5SWarner Losh 965c8befdd5SWarner Losh return (ENXIO); 966c8befdd5SWarner Losh } 967c8befdd5SWarner Losh 968c8befdd5SWarner Losh /* 969c8befdd5SWarner Losh * Attach the interface. Allocate softc structures, do ifmedia 970c8befdd5SWarner Losh * setup and ethernet/BPF attach. 971c8befdd5SWarner Losh */ 972c8befdd5SWarner Losh static int 97360270842SPyun YongHyeon ste_attach(device_t dev) 974c8befdd5SWarner Losh { 975c8befdd5SWarner Losh struct ste_softc *sc; 976c8befdd5SWarner Losh struct ifnet *ifp; 977c8befdd5SWarner Losh u_char eaddr[6]; 978f2632c3bSPyun YongHyeon int error = 0, rid; 979c8befdd5SWarner Losh 980c8befdd5SWarner Losh sc = device_get_softc(dev); 981c8befdd5SWarner Losh sc->ste_dev = dev; 982c8befdd5SWarner Losh 983c8befdd5SWarner Losh /* 984c8befdd5SWarner Losh * Only use one PHY since this chip reports multiple 985c8befdd5SWarner Losh * Note on the DFE-550 the PHY is at 1 on the DFE-580 986c8befdd5SWarner Losh * it is at 0 & 1. It is rev 0x12. 987c8befdd5SWarner Losh */ 988c8befdd5SWarner Losh if (pci_get_vendor(dev) == DL_VENDORID && 989c8befdd5SWarner Losh pci_get_device(dev) == DL_DEVICEID_DL10050 && 990c8befdd5SWarner Losh pci_get_revid(dev) == 0x12 ) 9914465097bSPyun YongHyeon sc->ste_flags |= STE_FLAG_ONE_PHY; 992c8befdd5SWarner Losh 993c8befdd5SWarner Losh mtx_init(&sc->ste_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 994c8befdd5SWarner Losh MTX_DEF); 995c8befdd5SWarner Losh /* 996c8befdd5SWarner Losh * Map control/status registers. 997c8befdd5SWarner Losh */ 998c8befdd5SWarner Losh pci_enable_busmaster(dev); 999c8befdd5SWarner Losh 1000c0270e60SPyun YongHyeon /* Prefer memory space register mapping over IO space. */ 1001c0270e60SPyun YongHyeon sc->ste_res_id = PCIR_BAR(1); 1002c0270e60SPyun YongHyeon sc->ste_res_type = SYS_RES_MEMORY; 1003c0270e60SPyun YongHyeon sc->ste_res = bus_alloc_resource_any(dev, sc->ste_res_type, 1004c0270e60SPyun YongHyeon &sc->ste_res_id, RF_ACTIVE); 1005c0270e60SPyun YongHyeon if (sc->ste_res == NULL) { 1006c0270e60SPyun YongHyeon sc->ste_res_id = PCIR_BAR(0); 1007c0270e60SPyun YongHyeon sc->ste_res_type = SYS_RES_IOPORT; 1008c0270e60SPyun YongHyeon sc->ste_res = bus_alloc_resource_any(dev, sc->ste_res_type, 1009c0270e60SPyun YongHyeon &sc->ste_res_id, RF_ACTIVE); 1010c0270e60SPyun YongHyeon } 1011c8befdd5SWarner Losh if (sc->ste_res == NULL) { 1012c8befdd5SWarner Losh device_printf(dev, "couldn't map ports/memory\n"); 1013c8befdd5SWarner Losh error = ENXIO; 1014c8befdd5SWarner Losh goto fail; 1015c8befdd5SWarner Losh } 1016c8befdd5SWarner Losh 1017c8befdd5SWarner Losh /* Allocate interrupt */ 1018c8befdd5SWarner Losh rid = 0; 1019c8befdd5SWarner Losh sc->ste_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1020c8befdd5SWarner Losh RF_SHAREABLE | RF_ACTIVE); 1021c8befdd5SWarner Losh 1022c8befdd5SWarner Losh if (sc->ste_irq == NULL) { 1023c8befdd5SWarner Losh device_printf(dev, "couldn't map interrupt\n"); 1024c8befdd5SWarner Losh error = ENXIO; 1025c8befdd5SWarner Losh goto fail; 1026c8befdd5SWarner Losh } 1027c8befdd5SWarner Losh 102810f695eeSPyun YongHyeon callout_init_mtx(&sc->ste_callout, &sc->ste_mtx, 0); 1029c8befdd5SWarner Losh 1030c8befdd5SWarner Losh /* Reset the adapter. */ 1031c8befdd5SWarner Losh ste_reset(sc); 1032c8befdd5SWarner Losh 1033c8befdd5SWarner Losh /* 1034c8befdd5SWarner Losh * Get station address from the EEPROM. 1035c8befdd5SWarner Losh */ 1036c8befdd5SWarner Losh if (ste_read_eeprom(sc, eaddr, 1037c8befdd5SWarner Losh STE_EEADDR_NODE0, 3, 0)) { 1038c8befdd5SWarner Losh device_printf(dev, "failed to read station address\n"); 1039c8befdd5SWarner Losh error = ENXIO;; 1040c8befdd5SWarner Losh goto fail; 1041c8befdd5SWarner Losh } 1042c8befdd5SWarner Losh 1043a1b2c209SPyun YongHyeon if ((error = ste_dma_alloc(sc)) != 0) 1044c8befdd5SWarner Losh goto fail; 1045c8befdd5SWarner Losh 1046c8befdd5SWarner Losh ifp = sc->ste_ifp = if_alloc(IFT_ETHER); 1047c8befdd5SWarner Losh if (ifp == NULL) { 1048c8befdd5SWarner Losh device_printf(dev, "can not if_alloc()\n"); 1049c8befdd5SWarner Losh error = ENOSPC; 1050c8befdd5SWarner Losh goto fail; 1051c8befdd5SWarner Losh } 1052c8befdd5SWarner Losh 1053c8befdd5SWarner Losh /* Do MII setup. */ 1054c8befdd5SWarner Losh if (mii_phy_probe(dev, &sc->ste_miibus, 1055c8befdd5SWarner Losh ste_ifmedia_upd, ste_ifmedia_sts)) { 1056c8befdd5SWarner Losh device_printf(dev, "MII without any phy!\n"); 1057c8befdd5SWarner Losh error = ENXIO; 1058c8befdd5SWarner Losh goto fail; 1059c8befdd5SWarner Losh } 1060c8befdd5SWarner Losh 1061c8befdd5SWarner Losh ifp->if_softc = sc; 1062c8befdd5SWarner Losh if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1063c8befdd5SWarner Losh ifp->if_mtu = ETHERMTU; 1064c8befdd5SWarner Losh ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1065c8befdd5SWarner Losh ifp->if_ioctl = ste_ioctl; 1066c8befdd5SWarner Losh ifp->if_start = ste_start; 1067c8befdd5SWarner Losh ifp->if_init = ste_init; 1068c8befdd5SWarner Losh IFQ_SET_MAXLEN(&ifp->if_snd, STE_TX_LIST_CNT - 1); 1069c8befdd5SWarner Losh ifp->if_snd.ifq_drv_maxlen = STE_TX_LIST_CNT - 1; 1070c8befdd5SWarner Losh IFQ_SET_READY(&ifp->if_snd); 1071c8befdd5SWarner Losh 1072c8befdd5SWarner Losh sc->ste_tx_thresh = STE_TXSTART_THRESH; 1073c8befdd5SWarner Losh 1074c8befdd5SWarner Losh /* 1075c8befdd5SWarner Losh * Call MI attach routine. 1076c8befdd5SWarner Losh */ 1077c8befdd5SWarner Losh ether_ifattach(ifp, eaddr); 1078c8befdd5SWarner Losh 1079c8befdd5SWarner Losh /* 1080c8befdd5SWarner Losh * Tell the upper layer(s) we support long frames. 1081c8befdd5SWarner Losh */ 1082c8befdd5SWarner Losh ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 1083c8befdd5SWarner Losh ifp->if_capabilities |= IFCAP_VLAN_MTU; 1084c8befdd5SWarner Losh ifp->if_capenable = ifp->if_capabilities; 1085c8befdd5SWarner Losh #ifdef DEVICE_POLLING 1086c8befdd5SWarner Losh ifp->if_capabilities |= IFCAP_POLLING; 1087c8befdd5SWarner Losh #endif 1088c8befdd5SWarner Losh 1089c8befdd5SWarner Losh /* Hook interrupt last to avoid having to lock softc */ 1090c8befdd5SWarner Losh error = bus_setup_intr(dev, sc->ste_irq, INTR_TYPE_NET | INTR_MPSAFE, 1091c8befdd5SWarner Losh NULL, ste_intr, sc, &sc->ste_intrhand); 1092c8befdd5SWarner Losh 1093c8befdd5SWarner Losh if (error) { 1094c8befdd5SWarner Losh device_printf(dev, "couldn't set up irq\n"); 1095c8befdd5SWarner Losh ether_ifdetach(ifp); 1096c8befdd5SWarner Losh goto fail; 1097c8befdd5SWarner Losh } 1098c8befdd5SWarner Losh 1099c8befdd5SWarner Losh fail: 1100c8befdd5SWarner Losh if (error) 1101c8befdd5SWarner Losh ste_detach(dev); 1102c8befdd5SWarner Losh 1103c8befdd5SWarner Losh return (error); 1104c8befdd5SWarner Losh } 1105c8befdd5SWarner Losh 1106c8befdd5SWarner Losh /* 1107c8befdd5SWarner Losh * Shutdown hardware and free up resources. This can be called any 1108c8befdd5SWarner Losh * time after the mutex has been initialized. It is called in both 1109c8befdd5SWarner Losh * the error case in attach and the normal detach case so it needs 1110c8befdd5SWarner Losh * to be careful about only freeing resources that have actually been 1111c8befdd5SWarner Losh * allocated. 1112c8befdd5SWarner Losh */ 1113c8befdd5SWarner Losh static int 111460270842SPyun YongHyeon ste_detach(device_t dev) 1115c8befdd5SWarner Losh { 1116c8befdd5SWarner Losh struct ste_softc *sc; 1117c8befdd5SWarner Losh struct ifnet *ifp; 1118c8befdd5SWarner Losh 1119c8befdd5SWarner Losh sc = device_get_softc(dev); 1120c8befdd5SWarner Losh KASSERT(mtx_initialized(&sc->ste_mtx), ("ste mutex not initialized")); 1121c8befdd5SWarner Losh ifp = sc->ste_ifp; 1122c8befdd5SWarner Losh 1123c8befdd5SWarner Losh #ifdef DEVICE_POLLING 1124c8befdd5SWarner Losh if (ifp->if_capenable & IFCAP_POLLING) 1125c8befdd5SWarner Losh ether_poll_deregister(ifp); 1126c8befdd5SWarner Losh #endif 1127c8befdd5SWarner Losh 1128c8befdd5SWarner Losh /* These should only be active if attach succeeded */ 1129c8befdd5SWarner Losh if (device_is_attached(dev)) { 11307cf545d0SJohn Baldwin ether_ifdetach(ifp); 1131c8befdd5SWarner Losh STE_LOCK(sc); 1132c8befdd5SWarner Losh ste_stop(sc); 1133c8befdd5SWarner Losh STE_UNLOCK(sc); 113410f695eeSPyun YongHyeon callout_drain(&sc->ste_callout); 1135c8befdd5SWarner Losh } 1136c8befdd5SWarner Losh if (sc->ste_miibus) 1137c8befdd5SWarner Losh device_delete_child(dev, sc->ste_miibus); 1138c8befdd5SWarner Losh bus_generic_detach(dev); 1139c8befdd5SWarner Losh 1140c8befdd5SWarner Losh if (sc->ste_intrhand) 1141c8befdd5SWarner Losh bus_teardown_intr(dev, sc->ste_irq, sc->ste_intrhand); 1142c8befdd5SWarner Losh if (sc->ste_irq) 1143c8befdd5SWarner Losh bus_release_resource(dev, SYS_RES_IRQ, 0, sc->ste_irq); 1144c8befdd5SWarner Losh if (sc->ste_res) 1145c0270e60SPyun YongHyeon bus_release_resource(dev, sc->ste_res_type, sc->ste_res_id, 1146c0270e60SPyun YongHyeon sc->ste_res); 1147c8befdd5SWarner Losh 1148c8befdd5SWarner Losh if (ifp) 1149c8befdd5SWarner Losh if_free(ifp); 1150c8befdd5SWarner Losh 1151a1b2c209SPyun YongHyeon ste_dma_free(sc); 1152c8befdd5SWarner Losh mtx_destroy(&sc->ste_mtx); 1153c8befdd5SWarner Losh 1154c8befdd5SWarner Losh return (0); 1155c8befdd5SWarner Losh } 1156c8befdd5SWarner Losh 1157a1b2c209SPyun YongHyeon struct ste_dmamap_arg { 1158a1b2c209SPyun YongHyeon bus_addr_t ste_busaddr; 1159a1b2c209SPyun YongHyeon }; 1160a1b2c209SPyun YongHyeon 1161a1b2c209SPyun YongHyeon static void 1162a1b2c209SPyun YongHyeon ste_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 1163c8befdd5SWarner Losh { 1164a1b2c209SPyun YongHyeon struct ste_dmamap_arg *ctx; 1165c8befdd5SWarner Losh 1166a1b2c209SPyun YongHyeon if (error != 0) 1167a1b2c209SPyun YongHyeon return; 1168a1b2c209SPyun YongHyeon 1169a1b2c209SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs)); 1170a1b2c209SPyun YongHyeon 1171a1b2c209SPyun YongHyeon ctx = (struct ste_dmamap_arg *)arg; 1172a1b2c209SPyun YongHyeon ctx->ste_busaddr = segs[0].ds_addr; 1173c8befdd5SWarner Losh } 1174c8befdd5SWarner Losh 1175a1b2c209SPyun YongHyeon static int 1176a1b2c209SPyun YongHyeon ste_dma_alloc(struct ste_softc *sc) 1177a1b2c209SPyun YongHyeon { 1178a1b2c209SPyun YongHyeon struct ste_chain *txc; 1179a1b2c209SPyun YongHyeon struct ste_chain_onefrag *rxc; 1180a1b2c209SPyun YongHyeon struct ste_dmamap_arg ctx; 1181a1b2c209SPyun YongHyeon int error, i; 1182c8befdd5SWarner Losh 1183a1b2c209SPyun YongHyeon /* Create parent DMA tag. */ 1184a1b2c209SPyun YongHyeon error = bus_dma_tag_create( 1185a1b2c209SPyun YongHyeon bus_get_dma_tag(sc->ste_dev), /* parent */ 1186a1b2c209SPyun YongHyeon 1, 0, /* alignment, boundary */ 1187a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1188a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1189a1b2c209SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1190a1b2c209SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 1191a1b2c209SPyun YongHyeon 0, /* nsegments */ 1192a1b2c209SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1193a1b2c209SPyun YongHyeon 0, /* flags */ 1194a1b2c209SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1195a1b2c209SPyun YongHyeon &sc->ste_cdata.ste_parent_tag); 1196a1b2c209SPyun YongHyeon if (error != 0) { 1197a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1198a1b2c209SPyun YongHyeon "could not create parent DMA tag.\n"); 1199a1b2c209SPyun YongHyeon goto fail; 1200a1b2c209SPyun YongHyeon } 1201c8befdd5SWarner Losh 1202a1b2c209SPyun YongHyeon /* Create DMA tag for Tx descriptor list. */ 1203a1b2c209SPyun YongHyeon error = bus_dma_tag_create( 1204a1b2c209SPyun YongHyeon sc->ste_cdata.ste_parent_tag, /* parent */ 1205a1b2c209SPyun YongHyeon STE_DESC_ALIGN, 0, /* alignment, boundary */ 1206a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */ 1207a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1208a1b2c209SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1209a1b2c209SPyun YongHyeon STE_TX_LIST_SZ, /* maxsize */ 1210a1b2c209SPyun YongHyeon 1, /* nsegments */ 1211a1b2c209SPyun YongHyeon STE_TX_LIST_SZ, /* maxsegsize */ 1212a1b2c209SPyun YongHyeon 0, /* flags */ 1213a1b2c209SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1214a1b2c209SPyun YongHyeon &sc->ste_cdata.ste_tx_list_tag); 1215a1b2c209SPyun YongHyeon if (error != 0) { 1216a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1217a1b2c209SPyun YongHyeon "could not create Tx list DMA tag.\n"); 1218a1b2c209SPyun YongHyeon goto fail; 1219a1b2c209SPyun YongHyeon } 1220a1b2c209SPyun YongHyeon 1221a1b2c209SPyun YongHyeon /* Create DMA tag for Rx descriptor list. */ 1222a1b2c209SPyun YongHyeon error = bus_dma_tag_create( 1223a1b2c209SPyun YongHyeon sc->ste_cdata.ste_parent_tag, /* parent */ 1224a1b2c209SPyun YongHyeon STE_DESC_ALIGN, 0, /* alignment, boundary */ 1225a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */ 1226a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1227a1b2c209SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1228a1b2c209SPyun YongHyeon STE_RX_LIST_SZ, /* maxsize */ 1229a1b2c209SPyun YongHyeon 1, /* nsegments */ 1230a1b2c209SPyun YongHyeon STE_RX_LIST_SZ, /* maxsegsize */ 1231a1b2c209SPyun YongHyeon 0, /* flags */ 1232a1b2c209SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1233a1b2c209SPyun YongHyeon &sc->ste_cdata.ste_rx_list_tag); 1234a1b2c209SPyun YongHyeon if (error != 0) { 1235a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1236a1b2c209SPyun YongHyeon "could not create Rx list DMA tag.\n"); 1237a1b2c209SPyun YongHyeon goto fail; 1238a1b2c209SPyun YongHyeon } 1239a1b2c209SPyun YongHyeon 1240a1b2c209SPyun YongHyeon /* Create DMA tag for Tx buffers. */ 1241a1b2c209SPyun YongHyeon error = bus_dma_tag_create( 1242a1b2c209SPyun YongHyeon sc->ste_cdata.ste_parent_tag, /* parent */ 1243a1b2c209SPyun YongHyeon 1, 0, /* alignment, boundary */ 1244a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */ 1245a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1246a1b2c209SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1247a1b2c209SPyun YongHyeon MCLBYTES * STE_MAXFRAGS, /* maxsize */ 1248a1b2c209SPyun YongHyeon STE_MAXFRAGS, /* nsegments */ 1249a1b2c209SPyun YongHyeon MCLBYTES, /* maxsegsize */ 1250a1b2c209SPyun YongHyeon 0, /* flags */ 1251a1b2c209SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1252a1b2c209SPyun YongHyeon &sc->ste_cdata.ste_tx_tag); 1253a1b2c209SPyun YongHyeon if (error != 0) { 1254a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, "could not create Tx DMA tag.\n"); 1255a1b2c209SPyun YongHyeon goto fail; 1256a1b2c209SPyun YongHyeon } 1257a1b2c209SPyun YongHyeon 1258a1b2c209SPyun YongHyeon /* Create DMA tag for Rx buffers. */ 1259a1b2c209SPyun YongHyeon error = bus_dma_tag_create( 1260a1b2c209SPyun YongHyeon sc->ste_cdata.ste_parent_tag, /* parent */ 1261a1b2c209SPyun YongHyeon 1, 0, /* alignment, boundary */ 1262a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */ 1263a1b2c209SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1264a1b2c209SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1265a1b2c209SPyun YongHyeon MCLBYTES, /* maxsize */ 1266a1b2c209SPyun YongHyeon 1, /* nsegments */ 1267a1b2c209SPyun YongHyeon MCLBYTES, /* maxsegsize */ 1268a1b2c209SPyun YongHyeon 0, /* flags */ 1269a1b2c209SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1270a1b2c209SPyun YongHyeon &sc->ste_cdata.ste_rx_tag); 1271a1b2c209SPyun YongHyeon if (error != 0) { 1272a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, "could not create Rx DMA tag.\n"); 1273a1b2c209SPyun YongHyeon goto fail; 1274a1b2c209SPyun YongHyeon } 1275a1b2c209SPyun YongHyeon 1276a1b2c209SPyun YongHyeon /* Allocate DMA'able memory and load the DMA map for Tx list. */ 1277a1b2c209SPyun YongHyeon error = bus_dmamem_alloc(sc->ste_cdata.ste_tx_list_tag, 1278a1b2c209SPyun YongHyeon (void **)&sc->ste_ldata.ste_tx_list, 1279a1b2c209SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT, 1280a1b2c209SPyun YongHyeon &sc->ste_cdata.ste_tx_list_map); 1281a1b2c209SPyun YongHyeon if (error != 0) { 1282a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1283a1b2c209SPyun YongHyeon "could not allocate DMA'able memory for Tx list.\n"); 1284a1b2c209SPyun YongHyeon goto fail; 1285a1b2c209SPyun YongHyeon } 1286a1b2c209SPyun YongHyeon ctx.ste_busaddr = 0; 1287a1b2c209SPyun YongHyeon error = bus_dmamap_load(sc->ste_cdata.ste_tx_list_tag, 1288a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_map, sc->ste_ldata.ste_tx_list, 1289a1b2c209SPyun YongHyeon STE_TX_LIST_SZ, ste_dmamap_cb, &ctx, 0); 1290a1b2c209SPyun YongHyeon if (error != 0 || ctx.ste_busaddr == 0) { 1291a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1292a1b2c209SPyun YongHyeon "could not load DMA'able memory for Tx list.\n"); 1293a1b2c209SPyun YongHyeon goto fail; 1294a1b2c209SPyun YongHyeon } 1295a1b2c209SPyun YongHyeon sc->ste_ldata.ste_tx_list_paddr = ctx.ste_busaddr; 1296a1b2c209SPyun YongHyeon 1297a1b2c209SPyun YongHyeon /* Allocate DMA'able memory and load the DMA map for Rx list. */ 1298a1b2c209SPyun YongHyeon error = bus_dmamem_alloc(sc->ste_cdata.ste_rx_list_tag, 1299a1b2c209SPyun YongHyeon (void **)&sc->ste_ldata.ste_rx_list, 1300a1b2c209SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT, 1301a1b2c209SPyun YongHyeon &sc->ste_cdata.ste_rx_list_map); 1302a1b2c209SPyun YongHyeon if (error != 0) { 1303a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1304a1b2c209SPyun YongHyeon "could not allocate DMA'able memory for Rx list.\n"); 1305a1b2c209SPyun YongHyeon goto fail; 1306a1b2c209SPyun YongHyeon } 1307a1b2c209SPyun YongHyeon ctx.ste_busaddr = 0; 1308a1b2c209SPyun YongHyeon error = bus_dmamap_load(sc->ste_cdata.ste_rx_list_tag, 1309a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_list_map, sc->ste_ldata.ste_rx_list, 1310a1b2c209SPyun YongHyeon STE_RX_LIST_SZ, ste_dmamap_cb, &ctx, 0); 1311a1b2c209SPyun YongHyeon if (error != 0 || ctx.ste_busaddr == 0) { 1312a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1313a1b2c209SPyun YongHyeon "could not load DMA'able memory for Rx list.\n"); 1314a1b2c209SPyun YongHyeon goto fail; 1315a1b2c209SPyun YongHyeon } 1316a1b2c209SPyun YongHyeon sc->ste_ldata.ste_rx_list_paddr = ctx.ste_busaddr; 1317a1b2c209SPyun YongHyeon 1318a1b2c209SPyun YongHyeon /* Create DMA maps for Tx buffers. */ 1319a1b2c209SPyun YongHyeon for (i = 0; i < STE_TX_LIST_CNT; i++) { 1320a1b2c209SPyun YongHyeon txc = &sc->ste_cdata.ste_tx_chain[i]; 1321a1b2c209SPyun YongHyeon txc->ste_ptr = NULL; 1322a1b2c209SPyun YongHyeon txc->ste_mbuf = NULL; 1323a1b2c209SPyun YongHyeon txc->ste_next = NULL; 1324a1b2c209SPyun YongHyeon txc->ste_phys = 0; 1325a1b2c209SPyun YongHyeon txc->ste_map = NULL; 1326a1b2c209SPyun YongHyeon error = bus_dmamap_create(sc->ste_cdata.ste_tx_tag, 0, 1327a1b2c209SPyun YongHyeon &txc->ste_map); 1328a1b2c209SPyun YongHyeon if (error != 0) { 1329a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1330a1b2c209SPyun YongHyeon "could not create Tx dmamap.\n"); 1331a1b2c209SPyun YongHyeon goto fail; 1332a1b2c209SPyun YongHyeon } 1333a1b2c209SPyun YongHyeon } 1334a1b2c209SPyun YongHyeon /* Create DMA maps for Rx buffers. */ 1335a1b2c209SPyun YongHyeon if ((error = bus_dmamap_create(sc->ste_cdata.ste_rx_tag, 0, 1336a1b2c209SPyun YongHyeon &sc->ste_cdata.ste_rx_sparemap)) != 0) { 1337a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1338a1b2c209SPyun YongHyeon "could not create spare Rx dmamap.\n"); 1339a1b2c209SPyun YongHyeon goto fail; 1340a1b2c209SPyun YongHyeon } 1341a1b2c209SPyun YongHyeon for (i = 0; i < STE_RX_LIST_CNT; i++) { 1342a1b2c209SPyun YongHyeon rxc = &sc->ste_cdata.ste_rx_chain[i]; 1343a1b2c209SPyun YongHyeon rxc->ste_ptr = NULL; 1344a1b2c209SPyun YongHyeon rxc->ste_mbuf = NULL; 1345a1b2c209SPyun YongHyeon rxc->ste_next = NULL; 1346a1b2c209SPyun YongHyeon rxc->ste_map = NULL; 1347a1b2c209SPyun YongHyeon error = bus_dmamap_create(sc->ste_cdata.ste_rx_tag, 0, 1348a1b2c209SPyun YongHyeon &rxc->ste_map); 1349a1b2c209SPyun YongHyeon if (error != 0) { 1350a1b2c209SPyun YongHyeon device_printf(sc->ste_dev, 1351a1b2c209SPyun YongHyeon "could not create Rx dmamap.\n"); 1352a1b2c209SPyun YongHyeon goto fail; 1353a1b2c209SPyun YongHyeon } 1354a1b2c209SPyun YongHyeon } 1355a1b2c209SPyun YongHyeon 1356a1b2c209SPyun YongHyeon fail: 1357a1b2c209SPyun YongHyeon return (error); 1358a1b2c209SPyun YongHyeon } 1359a1b2c209SPyun YongHyeon 1360a1b2c209SPyun YongHyeon static void 1361a1b2c209SPyun YongHyeon ste_dma_free(struct ste_softc *sc) 1362a1b2c209SPyun YongHyeon { 1363a1b2c209SPyun YongHyeon struct ste_chain *txc; 1364a1b2c209SPyun YongHyeon struct ste_chain_onefrag *rxc; 1365a1b2c209SPyun YongHyeon int i; 1366a1b2c209SPyun YongHyeon 1367a1b2c209SPyun YongHyeon /* Tx buffers. */ 1368a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_tx_tag != NULL) { 1369a1b2c209SPyun YongHyeon for (i = 0; i < STE_TX_LIST_CNT; i++) { 1370a1b2c209SPyun YongHyeon txc = &sc->ste_cdata.ste_tx_chain[i]; 1371a1b2c209SPyun YongHyeon if (txc->ste_map != NULL) { 1372a1b2c209SPyun YongHyeon bus_dmamap_destroy(sc->ste_cdata.ste_tx_tag, 1373a1b2c209SPyun YongHyeon txc->ste_map); 1374a1b2c209SPyun YongHyeon txc->ste_map = NULL; 1375a1b2c209SPyun YongHyeon } 1376a1b2c209SPyun YongHyeon } 1377a1b2c209SPyun YongHyeon bus_dma_tag_destroy(sc->ste_cdata.ste_tx_tag); 1378a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_tag = NULL; 1379a1b2c209SPyun YongHyeon } 1380a1b2c209SPyun YongHyeon /* Rx buffers. */ 1381a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_rx_tag != NULL) { 1382a1b2c209SPyun YongHyeon for (i = 0; i < STE_RX_LIST_CNT; i++) { 1383a1b2c209SPyun YongHyeon rxc = &sc->ste_cdata.ste_rx_chain[i]; 1384a1b2c209SPyun YongHyeon if (rxc->ste_map != NULL) { 1385a1b2c209SPyun YongHyeon bus_dmamap_destroy(sc->ste_cdata.ste_rx_tag, 1386a1b2c209SPyun YongHyeon rxc->ste_map); 1387a1b2c209SPyun YongHyeon rxc->ste_map = NULL; 1388a1b2c209SPyun YongHyeon } 1389a1b2c209SPyun YongHyeon } 1390a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_rx_sparemap != NULL) { 1391a1b2c209SPyun YongHyeon bus_dmamap_destroy(sc->ste_cdata.ste_rx_tag, 1392a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_sparemap); 1393a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_sparemap = NULL; 1394a1b2c209SPyun YongHyeon } 1395a1b2c209SPyun YongHyeon bus_dma_tag_destroy(sc->ste_cdata.ste_rx_tag); 1396a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_tag = NULL; 1397a1b2c209SPyun YongHyeon } 1398a1b2c209SPyun YongHyeon /* Tx descriptor list. */ 1399a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_tx_list_tag != NULL) { 1400a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_tx_list_map != NULL) 1401a1b2c209SPyun YongHyeon bus_dmamap_unload(sc->ste_cdata.ste_tx_list_tag, 1402a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_map); 1403a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_tx_list_map != NULL && 1404a1b2c209SPyun YongHyeon sc->ste_ldata.ste_tx_list != NULL) 1405a1b2c209SPyun YongHyeon bus_dmamem_free(sc->ste_cdata.ste_tx_list_tag, 1406a1b2c209SPyun YongHyeon sc->ste_ldata.ste_tx_list, 1407a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_map); 1408a1b2c209SPyun YongHyeon sc->ste_ldata.ste_tx_list = NULL; 1409a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_map = NULL; 1410a1b2c209SPyun YongHyeon bus_dma_tag_destroy(sc->ste_cdata.ste_tx_list_tag); 1411a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_tag = NULL; 1412a1b2c209SPyun YongHyeon } 1413a1b2c209SPyun YongHyeon /* Rx descriptor list. */ 1414a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_rx_list_tag != NULL) { 1415a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_rx_list_map != NULL) 1416a1b2c209SPyun YongHyeon bus_dmamap_unload(sc->ste_cdata.ste_rx_list_tag, 1417a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_list_map); 1418a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_rx_list_map != NULL && 1419a1b2c209SPyun YongHyeon sc->ste_ldata.ste_rx_list != NULL) 1420a1b2c209SPyun YongHyeon bus_dmamem_free(sc->ste_cdata.ste_rx_list_tag, 1421a1b2c209SPyun YongHyeon sc->ste_ldata.ste_rx_list, 1422a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_list_map); 1423a1b2c209SPyun YongHyeon sc->ste_ldata.ste_rx_list = NULL; 1424a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_list_map = NULL; 1425a1b2c209SPyun YongHyeon bus_dma_tag_destroy(sc->ste_cdata.ste_rx_list_tag); 1426a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_list_tag = NULL; 1427a1b2c209SPyun YongHyeon } 1428a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_parent_tag != NULL) { 1429a1b2c209SPyun YongHyeon bus_dma_tag_destroy(sc->ste_cdata.ste_parent_tag); 1430a1b2c209SPyun YongHyeon sc->ste_cdata.ste_parent_tag = NULL; 1431a1b2c209SPyun YongHyeon } 1432a1b2c209SPyun YongHyeon } 1433a1b2c209SPyun YongHyeon 1434a1b2c209SPyun YongHyeon static int 1435a1b2c209SPyun YongHyeon ste_newbuf(struct ste_softc *sc, struct ste_chain_onefrag *rxc) 1436a1b2c209SPyun YongHyeon { 1437a1b2c209SPyun YongHyeon struct mbuf *m; 1438a1b2c209SPyun YongHyeon bus_dma_segment_t segs[1]; 1439a1b2c209SPyun YongHyeon bus_dmamap_t map; 1440a1b2c209SPyun YongHyeon int error, nsegs; 1441a1b2c209SPyun YongHyeon 1442a1b2c209SPyun YongHyeon m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1443a1b2c209SPyun YongHyeon if (m == NULL) 1444a1b2c209SPyun YongHyeon return (ENOBUFS); 1445a1b2c209SPyun YongHyeon m->m_len = m->m_pkthdr.len = MCLBYTES; 1446a1b2c209SPyun YongHyeon m_adj(m, ETHER_ALIGN); 1447a1b2c209SPyun YongHyeon 1448a1b2c209SPyun YongHyeon if ((error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_rx_tag, 1449a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_sparemap, m, segs, &nsegs, 0)) != 0) { 1450a1b2c209SPyun YongHyeon m_freem(m); 1451a1b2c209SPyun YongHyeon return (error); 1452a1b2c209SPyun YongHyeon } 1453a1b2c209SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs)); 1454a1b2c209SPyun YongHyeon 1455a1b2c209SPyun YongHyeon if (rxc->ste_mbuf != NULL) { 1456a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, rxc->ste_map, 1457a1b2c209SPyun YongHyeon BUS_DMASYNC_POSTREAD); 1458a1b2c209SPyun YongHyeon bus_dmamap_unload(sc->ste_cdata.ste_rx_tag, rxc->ste_map); 1459a1b2c209SPyun YongHyeon } 1460a1b2c209SPyun YongHyeon map = rxc->ste_map; 1461a1b2c209SPyun YongHyeon rxc->ste_map = sc->ste_cdata.ste_rx_sparemap; 1462a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_sparemap = map; 1463a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, rxc->ste_map, 1464a1b2c209SPyun YongHyeon BUS_DMASYNC_PREREAD); 1465a1b2c209SPyun YongHyeon rxc->ste_mbuf = m; 1466a1b2c209SPyun YongHyeon rxc->ste_ptr->ste_status = 0; 1467a1b2c209SPyun YongHyeon rxc->ste_ptr->ste_frag.ste_addr = htole32(segs[0].ds_addr); 1468a1b2c209SPyun YongHyeon rxc->ste_ptr->ste_frag.ste_len = htole32(segs[0].ds_len | 1469a1b2c209SPyun YongHyeon STE_FRAG_LAST); 1470c8befdd5SWarner Losh return (0); 1471c8befdd5SWarner Losh } 1472c8befdd5SWarner Losh 1473c8befdd5SWarner Losh static int 147460270842SPyun YongHyeon ste_init_rx_list(struct ste_softc *sc) 1475c8befdd5SWarner Losh { 1476c8befdd5SWarner Losh struct ste_chain_data *cd; 1477c8befdd5SWarner Losh struct ste_list_data *ld; 1478a1b2c209SPyun YongHyeon int error, i; 1479c8befdd5SWarner Losh 1480c8befdd5SWarner Losh cd = &sc->ste_cdata; 1481a1b2c209SPyun YongHyeon ld = &sc->ste_ldata; 1482a1b2c209SPyun YongHyeon bzero(ld->ste_rx_list, STE_RX_LIST_SZ); 1483c8befdd5SWarner Losh for (i = 0; i < STE_RX_LIST_CNT; i++) { 1484c8befdd5SWarner Losh cd->ste_rx_chain[i].ste_ptr = &ld->ste_rx_list[i]; 1485a1b2c209SPyun YongHyeon error = ste_newbuf(sc, &cd->ste_rx_chain[i]); 1486a1b2c209SPyun YongHyeon if (error != 0) 1487a1b2c209SPyun YongHyeon return (error); 1488c8befdd5SWarner Losh if (i == (STE_RX_LIST_CNT - 1)) { 1489a1b2c209SPyun YongHyeon cd->ste_rx_chain[i].ste_next = &cd->ste_rx_chain[0]; 1490a1b2c209SPyun YongHyeon ld->ste_rx_list[i].ste_next = ld->ste_rx_list_paddr + 1491a1b2c209SPyun YongHyeon (sizeof(struct ste_desc_onefrag) * 0); 1492c8befdd5SWarner Losh } else { 1493a1b2c209SPyun YongHyeon cd->ste_rx_chain[i].ste_next = &cd->ste_rx_chain[i + 1]; 1494a1b2c209SPyun YongHyeon ld->ste_rx_list[i].ste_next = ld->ste_rx_list_paddr + 1495a1b2c209SPyun YongHyeon (sizeof(struct ste_desc_onefrag) * (i + 1)); 1496c8befdd5SWarner Losh } 1497c8befdd5SWarner Losh } 1498c8befdd5SWarner Losh 1499c8befdd5SWarner Losh cd->ste_rx_head = &cd->ste_rx_chain[0]; 1500a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_rx_list_tag, 1501a1b2c209SPyun YongHyeon sc->ste_cdata.ste_rx_list_map, 1502a1b2c209SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1503c8befdd5SWarner Losh 1504c8befdd5SWarner Losh return (0); 1505c8befdd5SWarner Losh } 1506c8befdd5SWarner Losh 1507c8befdd5SWarner Losh static void 150860270842SPyun YongHyeon ste_init_tx_list(struct ste_softc *sc) 1509c8befdd5SWarner Losh { 1510c8befdd5SWarner Losh struct ste_chain_data *cd; 1511c8befdd5SWarner Losh struct ste_list_data *ld; 1512c8befdd5SWarner Losh int i; 1513c8befdd5SWarner Losh 1514c8befdd5SWarner Losh cd = &sc->ste_cdata; 1515a1b2c209SPyun YongHyeon ld = &sc->ste_ldata; 1516a1b2c209SPyun YongHyeon bzero(ld->ste_tx_list, STE_TX_LIST_SZ); 1517c8befdd5SWarner Losh for (i = 0; i < STE_TX_LIST_CNT; i++) { 1518c8befdd5SWarner Losh cd->ste_tx_chain[i].ste_ptr = &ld->ste_tx_list[i]; 1519a1b2c209SPyun YongHyeon cd->ste_tx_chain[i].ste_mbuf = NULL; 1520a1b2c209SPyun YongHyeon if (i == (STE_TX_LIST_CNT - 1)) { 1521a1b2c209SPyun YongHyeon cd->ste_tx_chain[i].ste_next = &cd->ste_tx_chain[0]; 1522a1b2c209SPyun YongHyeon cd->ste_tx_chain[i].ste_phys = htole32(STE_ADDR_LO( 1523a1b2c209SPyun YongHyeon ld->ste_tx_list_paddr + 1524a1b2c209SPyun YongHyeon (sizeof(struct ste_desc) * 0))); 1525a1b2c209SPyun YongHyeon } else { 1526a1b2c209SPyun YongHyeon cd->ste_tx_chain[i].ste_next = &cd->ste_tx_chain[i + 1]; 1527a1b2c209SPyun YongHyeon cd->ste_tx_chain[i].ste_phys = htole32(STE_ADDR_LO( 1528a1b2c209SPyun YongHyeon ld->ste_tx_list_paddr + 1529a1b2c209SPyun YongHyeon (sizeof(struct ste_desc) * (i + 1)))); 1530a1b2c209SPyun YongHyeon } 1531c8befdd5SWarner Losh } 1532c8befdd5SWarner Losh 1533a1b2c209SPyun YongHyeon cd->ste_last_tx = NULL; 1534c8befdd5SWarner Losh cd->ste_tx_prod = 0; 1535c8befdd5SWarner Losh cd->ste_tx_cons = 0; 1536a1b2c209SPyun YongHyeon cd->ste_tx_cnt = 0; 1537a1b2c209SPyun YongHyeon 1538a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag, 1539a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_map, 1540a1b2c209SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1541c8befdd5SWarner Losh } 1542c8befdd5SWarner Losh 1543c8befdd5SWarner Losh static void 154460270842SPyun YongHyeon ste_init(void *xsc) 1545c8befdd5SWarner Losh { 1546c8befdd5SWarner Losh struct ste_softc *sc; 1547c8befdd5SWarner Losh 1548c8befdd5SWarner Losh sc = xsc; 1549c8befdd5SWarner Losh STE_LOCK(sc); 1550c8befdd5SWarner Losh ste_init_locked(sc); 1551c8befdd5SWarner Losh STE_UNLOCK(sc); 1552c8befdd5SWarner Losh } 1553c8befdd5SWarner Losh 1554c8befdd5SWarner Losh static void 155560270842SPyun YongHyeon ste_init_locked(struct ste_softc *sc) 1556c8befdd5SWarner Losh { 1557c8befdd5SWarner Losh struct ifnet *ifp; 1558f2632c3bSPyun YongHyeon int i; 1559c8befdd5SWarner Losh 1560c8befdd5SWarner Losh STE_LOCK_ASSERT(sc); 1561c8befdd5SWarner Losh ifp = sc->ste_ifp; 1562c8befdd5SWarner Losh 156355d7003eSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 156455d7003eSPyun YongHyeon return; 156555d7003eSPyun YongHyeon 1566c8befdd5SWarner Losh ste_stop(sc); 15678d9f6dd9SPyun YongHyeon /* Reset the chip to a known state. */ 15688d9f6dd9SPyun YongHyeon ste_reset(sc); 1569c8befdd5SWarner Losh 1570c8befdd5SWarner Losh /* Init our MAC address */ 1571c8befdd5SWarner Losh for (i = 0; i < ETHER_ADDR_LEN; i += 2) { 1572c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_PAR0 + i, 1573c8befdd5SWarner Losh ((IF_LLADDR(sc->ste_ifp)[i] & 0xff) | 1574c8befdd5SWarner Losh IF_LLADDR(sc->ste_ifp)[i + 1] << 8)); 1575c8befdd5SWarner Losh } 1576c8befdd5SWarner Losh 1577c8befdd5SWarner Losh /* Init RX list */ 1578a1b2c209SPyun YongHyeon if (ste_init_rx_list(sc) != 0) { 1579c8befdd5SWarner Losh device_printf(sc->ste_dev, 1580c8befdd5SWarner Losh "initialization failed: no memory for RX buffers\n"); 1581c8befdd5SWarner Losh ste_stop(sc); 1582c8befdd5SWarner Losh return; 1583c8befdd5SWarner Losh } 1584c8befdd5SWarner Losh 1585c8befdd5SWarner Losh /* Set RX polling interval */ 1586c8befdd5SWarner Losh CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 64); 1587c8befdd5SWarner Losh 1588c8befdd5SWarner Losh /* Init TX descriptors */ 1589c8befdd5SWarner Losh ste_init_tx_list(sc); 1590c8befdd5SWarner Losh 1591c8befdd5SWarner Losh /* Set the TX freethresh value */ 1592c8befdd5SWarner Losh CSR_WRITE_1(sc, STE_TX_DMABURST_THRESH, STE_PACKET_SIZE >> 8); 1593c8befdd5SWarner Losh 1594c8befdd5SWarner Losh /* Set the TX start threshold for best performance. */ 1595c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_TX_STARTTHRESH, sc->ste_tx_thresh); 1596c8befdd5SWarner Losh 1597c8befdd5SWarner Losh /* Set the TX reclaim threshold. */ 1598c8befdd5SWarner Losh CSR_WRITE_1(sc, STE_TX_RECLAIM_THRESH, (STE_PACKET_SIZE >> 4)); 1599c8befdd5SWarner Losh 1600931ec15aSPyun YongHyeon /* Accept VLAN length packets */ 1601931ec15aSPyun YongHyeon CSR_WRITE_2(sc, STE_MAX_FRAMELEN, ETHER_MAX_LEN + ETHER_VLAN_ENCAP_LEN); 1602931ec15aSPyun YongHyeon 1603c8befdd5SWarner Losh /* Set up the RX filter. */ 1604931ec15aSPyun YongHyeon ste_rxfilter(sc); 1605c8befdd5SWarner Losh 1606c8befdd5SWarner Losh /* Load the address of the RX list. */ 1607c8befdd5SWarner Losh STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_STALL); 1608c8befdd5SWarner Losh ste_wait(sc); 1609c8befdd5SWarner Losh CSR_WRITE_4(sc, STE_RX_DMALIST_PTR, 1610a1b2c209SPyun YongHyeon STE_ADDR_LO(sc->ste_ldata.ste_rx_list_paddr)); 1611c8befdd5SWarner Losh STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL); 1612c8befdd5SWarner Losh STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_RXDMA_UNSTALL); 1613c8befdd5SWarner Losh 1614a1b2c209SPyun YongHyeon /* Set TX polling interval(defer until we TX first packet). */ 1615c8befdd5SWarner Losh CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0); 1616c8befdd5SWarner Losh 1617c8befdd5SWarner Losh /* Load address of the TX list */ 1618c8befdd5SWarner Losh STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL); 1619c8befdd5SWarner Losh ste_wait(sc); 1620c8befdd5SWarner Losh CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0); 1621c8befdd5SWarner Losh STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL); 1622c8befdd5SWarner Losh STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL); 1623c8befdd5SWarner Losh ste_wait(sc); 1624c8befdd5SWarner Losh 1625c8befdd5SWarner Losh /* Enable receiver and transmitter */ 1626c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_MACCTL0, 0); 1627c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_MACCTL1, 0); 1628c8befdd5SWarner Losh STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_TX_ENABLE); 1629c8befdd5SWarner Losh STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_RX_ENABLE); 1630c8befdd5SWarner Losh 1631c8befdd5SWarner Losh /* Enable stats counters. */ 1632c8befdd5SWarner Losh STE_SETBIT2(sc, STE_MACCTL1, STE_MACCTL1_STATS_ENABLE); 1633c8befdd5SWarner Losh 1634c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_ISR, 0xFFFF); 1635c8befdd5SWarner Losh #ifdef DEVICE_POLLING 1636c8befdd5SWarner Losh /* Disable interrupts if we are polling. */ 1637c8befdd5SWarner Losh if (ifp->if_capenable & IFCAP_POLLING) 1638c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_IMR, 0); 1639c8befdd5SWarner Losh else 1640c8befdd5SWarner Losh #endif 1641c8befdd5SWarner Losh /* Enable interrupts. */ 1642c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_IMR, STE_INTRS); 1643c8befdd5SWarner Losh 1644c8befdd5SWarner Losh ste_ifmedia_upd_locked(ifp); 1645c8befdd5SWarner Losh 1646c8befdd5SWarner Losh ifp->if_drv_flags |= IFF_DRV_RUNNING; 1647c8befdd5SWarner Losh ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1648c8befdd5SWarner Losh 164910f695eeSPyun YongHyeon callout_reset(&sc->ste_callout, hz, ste_tick, sc); 1650c8befdd5SWarner Losh } 1651c8befdd5SWarner Losh 1652c8befdd5SWarner Losh static void 165360270842SPyun YongHyeon ste_stop(struct ste_softc *sc) 1654c8befdd5SWarner Losh { 1655c8befdd5SWarner Losh struct ifnet *ifp; 1656a1b2c209SPyun YongHyeon struct ste_chain_onefrag *cur_rx; 1657a1b2c209SPyun YongHyeon struct ste_chain *cur_tx; 16588d9f6dd9SPyun YongHyeon uint32_t val; 1659f2632c3bSPyun YongHyeon int i; 1660c8befdd5SWarner Losh 1661c8befdd5SWarner Losh STE_LOCK_ASSERT(sc); 1662c8befdd5SWarner Losh ifp = sc->ste_ifp; 1663c8befdd5SWarner Losh 166410f695eeSPyun YongHyeon callout_stop(&sc->ste_callout); 166510f695eeSPyun YongHyeon sc->ste_timer = 0; 1666c8befdd5SWarner Losh ifp->if_drv_flags &= ~(IFF_DRV_RUNNING|IFF_DRV_OACTIVE); 1667c8befdd5SWarner Losh 1668c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_IMR, 0); 16698d9f6dd9SPyun YongHyeon /* Stop pending DMA. */ 16708d9f6dd9SPyun YongHyeon val = CSR_READ_4(sc, STE_DMACTL); 16718d9f6dd9SPyun YongHyeon val |= STE_DMACTL_TXDMA_STALL | STE_DMACTL_RXDMA_STALL; 16728d9f6dd9SPyun YongHyeon CSR_WRITE_4(sc, STE_DMACTL, val); 1673c8befdd5SWarner Losh ste_wait(sc); 16748d9f6dd9SPyun YongHyeon /* Disable auto-polling. */ 16758d9f6dd9SPyun YongHyeon CSR_WRITE_1(sc, STE_RX_DMAPOLL_PERIOD, 0); 16768d9f6dd9SPyun YongHyeon CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 0); 16778d9f6dd9SPyun YongHyeon /* Nullify DMA address to stop any further DMA. */ 16788d9f6dd9SPyun YongHyeon CSR_WRITE_4(sc, STE_RX_DMALIST_PTR, 0); 16798d9f6dd9SPyun YongHyeon CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 0); 16808d9f6dd9SPyun YongHyeon /* Stop TX/RX MAC. */ 16818d9f6dd9SPyun YongHyeon val = CSR_READ_2(sc, STE_MACCTL1); 16828d9f6dd9SPyun YongHyeon val |= STE_MACCTL1_TX_DISABLE | STE_MACCTL1_RX_DISABLE | 16838d9f6dd9SPyun YongHyeon STE_MACCTL1_STATS_DISABLE; 16848d9f6dd9SPyun YongHyeon CSR_WRITE_2(sc, STE_MACCTL1, val); 16858d9f6dd9SPyun YongHyeon for (i = 0; i < STE_TIMEOUT; i++) { 16868d9f6dd9SPyun YongHyeon DELAY(10); 16878d9f6dd9SPyun YongHyeon if ((CSR_READ_2(sc, STE_MACCTL1) & (STE_MACCTL1_TX_DISABLE | 16888d9f6dd9SPyun YongHyeon STE_MACCTL1_RX_DISABLE | STE_MACCTL1_STATS_DISABLE)) == 0) 16898d9f6dd9SPyun YongHyeon break; 16908d9f6dd9SPyun YongHyeon } 16918d9f6dd9SPyun YongHyeon if (i == STE_TIMEOUT) 16928d9f6dd9SPyun YongHyeon device_printf(sc->ste_dev, "Stopping MAC timed out\n"); 16938d9f6dd9SPyun YongHyeon /* Acknowledge any pending interrupts. */ 16948d9f6dd9SPyun YongHyeon CSR_READ_2(sc, STE_ISR_ACK); 16958d9f6dd9SPyun YongHyeon ste_stats_update(sc); 1696c8befdd5SWarner Losh 1697c8befdd5SWarner Losh for (i = 0; i < STE_RX_LIST_CNT; i++) { 1698a1b2c209SPyun YongHyeon cur_rx = &sc->ste_cdata.ste_rx_chain[i]; 1699a1b2c209SPyun YongHyeon if (cur_rx->ste_mbuf != NULL) { 1700a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_rx_tag, 1701a1b2c209SPyun YongHyeon cur_rx->ste_map, BUS_DMASYNC_POSTREAD); 1702a1b2c209SPyun YongHyeon bus_dmamap_unload(sc->ste_cdata.ste_rx_tag, 1703a1b2c209SPyun YongHyeon cur_rx->ste_map); 1704a1b2c209SPyun YongHyeon m_freem(cur_rx->ste_mbuf); 1705a1b2c209SPyun YongHyeon cur_rx->ste_mbuf = NULL; 1706c8befdd5SWarner Losh } 1707c8befdd5SWarner Losh } 1708c8befdd5SWarner Losh 1709c8befdd5SWarner Losh for (i = 0; i < STE_TX_LIST_CNT; i++) { 1710a1b2c209SPyun YongHyeon cur_tx = &sc->ste_cdata.ste_tx_chain[i]; 1711a1b2c209SPyun YongHyeon if (cur_tx->ste_mbuf != NULL) { 1712a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, 1713a1b2c209SPyun YongHyeon cur_tx->ste_map, BUS_DMASYNC_POSTWRITE); 1714a1b2c209SPyun YongHyeon bus_dmamap_unload(sc->ste_cdata.ste_tx_tag, 1715a1b2c209SPyun YongHyeon cur_tx->ste_map); 1716a1b2c209SPyun YongHyeon m_freem(cur_tx->ste_mbuf); 1717a1b2c209SPyun YongHyeon cur_tx->ste_mbuf = NULL; 1718c8befdd5SWarner Losh } 1719c8befdd5SWarner Losh } 1720c8befdd5SWarner Losh } 1721c8befdd5SWarner Losh 1722c8befdd5SWarner Losh static void 172360270842SPyun YongHyeon ste_reset(struct ste_softc *sc) 1724c8befdd5SWarner Losh { 172538c52cfdSPyun YongHyeon uint32_t ctl; 1726c8befdd5SWarner Losh int i; 1727c8befdd5SWarner Losh 172838c52cfdSPyun YongHyeon ctl = CSR_READ_4(sc, STE_ASICCTL); 172938c52cfdSPyun YongHyeon ctl |= STE_ASICCTL_GLOBAL_RESET | STE_ASICCTL_RX_RESET | 1730c8befdd5SWarner Losh STE_ASICCTL_TX_RESET | STE_ASICCTL_DMA_RESET | 1731c8befdd5SWarner Losh STE_ASICCTL_FIFO_RESET | STE_ASICCTL_NETWORK_RESET | 1732c8befdd5SWarner Losh STE_ASICCTL_AUTOINIT_RESET |STE_ASICCTL_HOST_RESET | 173338c52cfdSPyun YongHyeon STE_ASICCTL_EXTRESET_RESET; 173438c52cfdSPyun YongHyeon CSR_WRITE_4(sc, STE_ASICCTL, ctl); 173538c52cfdSPyun YongHyeon CSR_READ_4(sc, STE_ASICCTL); 173638c52cfdSPyun YongHyeon /* 173738c52cfdSPyun YongHyeon * Due to the need of accessing EEPROM controller can take 173838c52cfdSPyun YongHyeon * up to 1ms to complete the global reset. 173938c52cfdSPyun YongHyeon */ 174038c52cfdSPyun YongHyeon DELAY(1000); 1741c8befdd5SWarner Losh 1742c8befdd5SWarner Losh for (i = 0; i < STE_TIMEOUT; i++) { 1743c8befdd5SWarner Losh if (!(CSR_READ_4(sc, STE_ASICCTL) & STE_ASICCTL_RESET_BUSY)) 1744c8befdd5SWarner Losh break; 174538c52cfdSPyun YongHyeon DELAY(10); 1746c8befdd5SWarner Losh } 1747c8befdd5SWarner Losh 1748c8befdd5SWarner Losh if (i == STE_TIMEOUT) 1749c8befdd5SWarner Losh device_printf(sc->ste_dev, "global reset never completed\n"); 1750c8befdd5SWarner Losh } 1751c8befdd5SWarner Losh 175281598b3eSPyun YongHyeon static void 175381598b3eSPyun YongHyeon ste_restart_tx(struct ste_softc *sc) 175481598b3eSPyun YongHyeon { 175581598b3eSPyun YongHyeon uint16_t mac; 175681598b3eSPyun YongHyeon int i; 175781598b3eSPyun YongHyeon 175881598b3eSPyun YongHyeon for (i = 0; i < STE_TIMEOUT; i++) { 175981598b3eSPyun YongHyeon mac = CSR_READ_2(sc, STE_MACCTL1); 176081598b3eSPyun YongHyeon mac |= STE_MACCTL1_TX_ENABLE; 176181598b3eSPyun YongHyeon CSR_WRITE_2(sc, STE_MACCTL1, mac); 176281598b3eSPyun YongHyeon mac = CSR_READ_2(sc, STE_MACCTL1); 176381598b3eSPyun YongHyeon if ((mac & STE_MACCTL1_TX_ENABLED) != 0) 176481598b3eSPyun YongHyeon break; 176581598b3eSPyun YongHyeon DELAY(10); 176681598b3eSPyun YongHyeon } 176781598b3eSPyun YongHyeon 176881598b3eSPyun YongHyeon if (i == STE_TIMEOUT) 176981598b3eSPyun YongHyeon device_printf(sc->ste_dev, "starting Tx failed"); 177081598b3eSPyun YongHyeon } 177181598b3eSPyun YongHyeon 1772c8befdd5SWarner Losh static int 177360270842SPyun YongHyeon ste_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 1774c8befdd5SWarner Losh { 1775c8befdd5SWarner Losh struct ste_softc *sc; 1776c8befdd5SWarner Losh struct ifreq *ifr; 1777c8befdd5SWarner Losh struct mii_data *mii; 1778c8befdd5SWarner Losh int error = 0; 1779c8befdd5SWarner Losh 1780c8befdd5SWarner Losh sc = ifp->if_softc; 1781c8befdd5SWarner Losh ifr = (struct ifreq *)data; 1782c8befdd5SWarner Losh 1783c8befdd5SWarner Losh switch (command) { 1784c8befdd5SWarner Losh case SIOCSIFFLAGS: 1785c8befdd5SWarner Losh STE_LOCK(sc); 1786931ec15aSPyun YongHyeon if ((ifp->if_flags & IFF_UP) != 0) { 1787931ec15aSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0 && 1788931ec15aSPyun YongHyeon ((ifp->if_flags ^ sc->ste_if_flags) & 1789931ec15aSPyun YongHyeon (IFF_PROMISC | IFF_ALLMULTI)) != 0) 1790931ec15aSPyun YongHyeon ste_rxfilter(sc); 1791931ec15aSPyun YongHyeon else 1792c8befdd5SWarner Losh ste_init_locked(sc); 1793931ec15aSPyun YongHyeon } else if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 1794c8befdd5SWarner Losh ste_stop(sc); 1795c8befdd5SWarner Losh sc->ste_if_flags = ifp->if_flags; 1796c8befdd5SWarner Losh STE_UNLOCK(sc); 1797c8befdd5SWarner Losh break; 1798c8befdd5SWarner Losh case SIOCADDMULTI: 1799c8befdd5SWarner Losh case SIOCDELMULTI: 1800c8befdd5SWarner Losh STE_LOCK(sc); 1801931ec15aSPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 1802931ec15aSPyun YongHyeon ste_rxfilter(sc); 1803c8befdd5SWarner Losh STE_UNLOCK(sc); 1804c8befdd5SWarner Losh break; 1805c8befdd5SWarner Losh case SIOCGIFMEDIA: 1806c8befdd5SWarner Losh case SIOCSIFMEDIA: 1807c8befdd5SWarner Losh mii = device_get_softc(sc->ste_miibus); 1808c8befdd5SWarner Losh error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 1809c8befdd5SWarner Losh break; 1810c8befdd5SWarner Losh case SIOCSIFCAP: 1811c8befdd5SWarner Losh #ifdef DEVICE_POLLING 1812c8befdd5SWarner Losh if (ifr->ifr_reqcap & IFCAP_POLLING && 1813c8befdd5SWarner Losh !(ifp->if_capenable & IFCAP_POLLING)) { 1814c8befdd5SWarner Losh error = ether_poll_register(ste_poll, ifp); 1815c8befdd5SWarner Losh if (error) 1816c8befdd5SWarner Losh return (error); 1817c8befdd5SWarner Losh STE_LOCK(sc); 1818c8befdd5SWarner Losh /* Disable interrupts */ 1819c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_IMR, 0); 1820c8befdd5SWarner Losh ifp->if_capenable |= IFCAP_POLLING; 1821c8befdd5SWarner Losh STE_UNLOCK(sc); 1822c8befdd5SWarner Losh return (error); 1823c8befdd5SWarner Losh 1824c8befdd5SWarner Losh } 1825c8befdd5SWarner Losh if (!(ifr->ifr_reqcap & IFCAP_POLLING) && 1826c8befdd5SWarner Losh ifp->if_capenable & IFCAP_POLLING) { 1827c8befdd5SWarner Losh error = ether_poll_deregister(ifp); 1828c8befdd5SWarner Losh /* Enable interrupts. */ 1829c8befdd5SWarner Losh STE_LOCK(sc); 1830c8befdd5SWarner Losh CSR_WRITE_2(sc, STE_IMR, STE_INTRS); 1831c8befdd5SWarner Losh ifp->if_capenable &= ~IFCAP_POLLING; 1832c8befdd5SWarner Losh STE_UNLOCK(sc); 1833c8befdd5SWarner Losh return (error); 1834c8befdd5SWarner Losh } 1835c8befdd5SWarner Losh #endif /* DEVICE_POLLING */ 1836c8befdd5SWarner Losh break; 1837c8befdd5SWarner Losh default: 1838c8befdd5SWarner Losh error = ether_ioctl(ifp, command, data); 1839c8befdd5SWarner Losh break; 1840c8befdd5SWarner Losh } 1841c8befdd5SWarner Losh 1842c8befdd5SWarner Losh return (error); 1843c8befdd5SWarner Losh } 1844c8befdd5SWarner Losh 1845c8befdd5SWarner Losh static int 1846a1b2c209SPyun YongHyeon ste_encap(struct ste_softc *sc, struct mbuf **m_head, struct ste_chain *txc) 1847c8befdd5SWarner Losh { 1848a1b2c209SPyun YongHyeon struct ste_frag *frag; 1849c8befdd5SWarner Losh struct mbuf *m; 1850a1b2c209SPyun YongHyeon struct ste_desc *desc; 1851a1b2c209SPyun YongHyeon bus_dma_segment_t txsegs[STE_MAXFRAGS]; 1852a1b2c209SPyun YongHyeon int error, i, nsegs; 1853c8befdd5SWarner Losh 1854a1b2c209SPyun YongHyeon STE_LOCK_ASSERT(sc); 1855a1b2c209SPyun YongHyeon M_ASSERTPKTHDR((*m_head)); 1856c8befdd5SWarner Losh 1857a1b2c209SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_tx_tag, 1858a1b2c209SPyun YongHyeon txc->ste_map, *m_head, txsegs, &nsegs, 0); 1859a1b2c209SPyun YongHyeon if (error == EFBIG) { 1860a1b2c209SPyun YongHyeon m = m_collapse(*m_head, M_DONTWAIT, STE_MAXFRAGS); 1861a1b2c209SPyun YongHyeon if (m == NULL) { 1862a1b2c209SPyun YongHyeon m_freem(*m_head); 1863a1b2c209SPyun YongHyeon *m_head = NULL; 1864a1b2c209SPyun YongHyeon return (ENOMEM); 1865c8befdd5SWarner Losh } 1866a1b2c209SPyun YongHyeon *m_head = m; 1867a1b2c209SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->ste_cdata.ste_tx_tag, 1868a1b2c209SPyun YongHyeon txc->ste_map, *m_head, txsegs, &nsegs, 0); 1869a1b2c209SPyun YongHyeon if (error != 0) { 1870a1b2c209SPyun YongHyeon m_freem(*m_head); 1871a1b2c209SPyun YongHyeon *m_head = NULL; 1872a1b2c209SPyun YongHyeon return (error); 1873c8befdd5SWarner Losh } 1874a1b2c209SPyun YongHyeon } else if (error != 0) 1875a1b2c209SPyun YongHyeon return (error); 1876a1b2c209SPyun YongHyeon if (nsegs == 0) { 1877a1b2c209SPyun YongHyeon m_freem(*m_head); 1878a1b2c209SPyun YongHyeon *m_head = NULL; 1879a1b2c209SPyun YongHyeon return (EIO); 1880a1b2c209SPyun YongHyeon } 1881a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_tx_tag, txc->ste_map, 1882a1b2c209SPyun YongHyeon BUS_DMASYNC_PREWRITE); 1883c8befdd5SWarner Losh 1884a1b2c209SPyun YongHyeon desc = txc->ste_ptr; 1885a1b2c209SPyun YongHyeon for (i = 0; i < nsegs; i++) { 1886a1b2c209SPyun YongHyeon frag = &desc->ste_frags[i]; 1887a1b2c209SPyun YongHyeon frag->ste_addr = htole32(STE_ADDR_LO(txsegs[i].ds_addr)); 1888a1b2c209SPyun YongHyeon frag->ste_len = htole32(txsegs[i].ds_len); 1889a1b2c209SPyun YongHyeon } 1890a1b2c209SPyun YongHyeon desc->ste_frags[i - 1].ste_len |= htole32(STE_FRAG_LAST); 1891c8befdd5SWarner Losh /* 1892a1b2c209SPyun YongHyeon * Because we use Tx polling we can't chain multiple 1893a1b2c209SPyun YongHyeon * Tx descriptors here. Otherwise we race with controller. 1894c8befdd5SWarner Losh */ 1895a1b2c209SPyun YongHyeon desc->ste_next = 0; 1896a1b2c209SPyun YongHyeon desc->ste_ctl = htole32(STE_TXCTL_ALIGN_DIS | STE_TXCTL_DMAINTR); 1897a1b2c209SPyun YongHyeon txc->ste_mbuf = *m_head; 1898a1b2c209SPyun YongHyeon STE_INC(sc->ste_cdata.ste_tx_prod, STE_TX_LIST_CNT); 1899a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_cnt++; 1900c8befdd5SWarner Losh 1901c8befdd5SWarner Losh return (0); 1902c8befdd5SWarner Losh } 1903c8befdd5SWarner Losh 1904c8befdd5SWarner Losh static void 190560270842SPyun YongHyeon ste_start(struct ifnet *ifp) 1906c8befdd5SWarner Losh { 1907c8befdd5SWarner Losh struct ste_softc *sc; 1908c8befdd5SWarner Losh 1909c8befdd5SWarner Losh sc = ifp->if_softc; 1910c8befdd5SWarner Losh STE_LOCK(sc); 1911c8befdd5SWarner Losh ste_start_locked(ifp); 1912c8befdd5SWarner Losh STE_UNLOCK(sc); 1913c8befdd5SWarner Losh } 1914c8befdd5SWarner Losh 1915c8befdd5SWarner Losh static void 191660270842SPyun YongHyeon ste_start_locked(struct ifnet *ifp) 1917c8befdd5SWarner Losh { 1918c8befdd5SWarner Losh struct ste_softc *sc; 1919c8befdd5SWarner Losh struct ste_chain *cur_tx; 1920f2632c3bSPyun YongHyeon struct mbuf *m_head = NULL; 1921a1b2c209SPyun YongHyeon int enq; 1922c8befdd5SWarner Losh 1923c8befdd5SWarner Losh sc = ifp->if_softc; 1924c8befdd5SWarner Losh STE_LOCK_ASSERT(sc); 1925c8befdd5SWarner Losh 19264465097bSPyun YongHyeon if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 19274465097bSPyun YongHyeon IFF_DRV_RUNNING || (sc->ste_flags & STE_FLAG_LINK) == 0) 1928c8befdd5SWarner Losh return; 1929c8befdd5SWarner Losh 1930a1b2c209SPyun YongHyeon for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd);) { 1931a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_tx_cnt == STE_TX_LIST_CNT - 1) { 1932c8befdd5SWarner Losh /* 1933a1b2c209SPyun YongHyeon * Controller may have cached copy of the last used 1934a1b2c209SPyun YongHyeon * next ptr so we have to reserve one TFD to avoid 1935a1b2c209SPyun YongHyeon * TFD overruns. 1936c8befdd5SWarner Losh */ 1937c8befdd5SWarner Losh ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1938c8befdd5SWarner Losh break; 1939c8befdd5SWarner Losh } 1940c8befdd5SWarner Losh IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 1941c8befdd5SWarner Losh if (m_head == NULL) 1942c8befdd5SWarner Losh break; 1943a1b2c209SPyun YongHyeon cur_tx = &sc->ste_cdata.ste_tx_chain[sc->ste_cdata.ste_tx_prod]; 1944a1b2c209SPyun YongHyeon if (ste_encap(sc, &m_head, cur_tx) != 0) { 1945a1b2c209SPyun YongHyeon if (m_head == NULL) 1946c8befdd5SWarner Losh break; 1947a1b2c209SPyun YongHyeon IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 1948a1b2c209SPyun YongHyeon break; 1949a1b2c209SPyun YongHyeon } 1950a1b2c209SPyun YongHyeon if (sc->ste_cdata.ste_last_tx == NULL) { 1951a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag, 1952a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_map, 1953a1b2c209SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1954c8befdd5SWarner Losh STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_STALL); 1955c8befdd5SWarner Losh ste_wait(sc); 1956c8befdd5SWarner Losh CSR_WRITE_4(sc, STE_TX_DMALIST_PTR, 1957a1b2c209SPyun YongHyeon STE_ADDR_LO(sc->ste_ldata.ste_tx_list_paddr)); 1958c8befdd5SWarner Losh CSR_WRITE_1(sc, STE_TX_DMAPOLL_PERIOD, 64); 1959c8befdd5SWarner Losh STE_SETBIT4(sc, STE_DMACTL, STE_DMACTL_TXDMA_UNSTALL); 1960c8befdd5SWarner Losh ste_wait(sc); 1961c8befdd5SWarner Losh } else { 1962a1b2c209SPyun YongHyeon sc->ste_cdata.ste_last_tx->ste_ptr->ste_next = 1963a1b2c209SPyun YongHyeon sc->ste_cdata.ste_last_tx->ste_phys; 1964a1b2c209SPyun YongHyeon bus_dmamap_sync(sc->ste_cdata.ste_tx_list_tag, 1965a1b2c209SPyun YongHyeon sc->ste_cdata.ste_tx_list_map, 1966a1b2c209SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1967c8befdd5SWarner Losh } 1968a1b2c209SPyun YongHyeon sc->ste_cdata.ste_last_tx = cur_tx; 1969c8befdd5SWarner Losh 1970a1b2c209SPyun YongHyeon enq++; 1971c8befdd5SWarner Losh /* 1972c8befdd5SWarner Losh * If there's a BPF listener, bounce a copy of this frame 1973c8befdd5SWarner Losh * to him. 1974c8befdd5SWarner Losh */ 1975a1b2c209SPyun YongHyeon BPF_MTAP(ifp, m_head); 1976c8befdd5SWarner Losh } 1977a1b2c209SPyun YongHyeon 1978a1b2c209SPyun YongHyeon if (enq > 0) 1979a1b2c209SPyun YongHyeon sc->ste_timer = STE_TX_TIMEOUT; 1980c8befdd5SWarner Losh } 1981c8befdd5SWarner Losh 1982c8befdd5SWarner Losh static void 19837cf545d0SJohn Baldwin ste_watchdog(struct ste_softc *sc) 1984c8befdd5SWarner Losh { 19857cf545d0SJohn Baldwin struct ifnet *ifp; 1986c8befdd5SWarner Losh 19877cf545d0SJohn Baldwin ifp = sc->ste_ifp; 19887cf545d0SJohn Baldwin STE_LOCK_ASSERT(sc); 1989c8befdd5SWarner Losh 199010f695eeSPyun YongHyeon if (sc->ste_timer == 0 || --sc->ste_timer) 199110f695eeSPyun YongHyeon return; 199210f695eeSPyun YongHyeon 1993c8befdd5SWarner Losh ifp->if_oerrors++; 1994c8befdd5SWarner Losh if_printf(ifp, "watchdog timeout\n"); 1995c8befdd5SWarner Losh 1996c8befdd5SWarner Losh ste_txeof(sc); 199781598b3eSPyun YongHyeon ste_txeoc(sc); 1998a1b2c209SPyun YongHyeon ste_rxeof(sc, -1); 199955d7003eSPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2000c8befdd5SWarner Losh ste_init_locked(sc); 2001c8befdd5SWarner Losh 2002c8befdd5SWarner Losh if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2003c8befdd5SWarner Losh ste_start_locked(ifp); 2004c8befdd5SWarner Losh } 2005c8befdd5SWarner Losh 2006c8befdd5SWarner Losh static int 200760270842SPyun YongHyeon ste_shutdown(device_t dev) 2008c8befdd5SWarner Losh { 2009c8befdd5SWarner Losh struct ste_softc *sc; 2010c8befdd5SWarner Losh 2011c8befdd5SWarner Losh sc = device_get_softc(dev); 2012c8befdd5SWarner Losh 2013c8befdd5SWarner Losh STE_LOCK(sc); 2014c8befdd5SWarner Losh ste_stop(sc); 2015c8befdd5SWarner Losh STE_UNLOCK(sc); 2016c8befdd5SWarner Losh 2017c8befdd5SWarner Losh return (0); 2018c8befdd5SWarner Losh } 2019