1098ca2bdSWarner Losh /*- 2ce4946daSBill Paul * Copyright (c) 2001 Wind River Systems 3ce4946daSBill Paul * Copyright (c) 1997, 1998, 1999, 2000, 2001 4ce4946daSBill Paul * Bill Paul <wpaul@bsdi.com>. All rights reserved. 5ce4946daSBill Paul * 6ce4946daSBill Paul * Redistribution and use in source and binary forms, with or without 7ce4946daSBill Paul * modification, are permitted provided that the following conditions 8ce4946daSBill Paul * are met: 9ce4946daSBill Paul * 1. Redistributions of source code must retain the above copyright 10ce4946daSBill Paul * notice, this list of conditions and the following disclaimer. 11ce4946daSBill Paul * 2. Redistributions in binary form must reproduce the above copyright 12ce4946daSBill Paul * notice, this list of conditions and the following disclaimer in the 13ce4946daSBill Paul * documentation and/or other materials provided with the distribution. 14ce4946daSBill Paul * 3. All advertising materials mentioning features or use of this software 15ce4946daSBill Paul * must display the following acknowledgement: 16ce4946daSBill Paul * This product includes software developed by Bill Paul. 17ce4946daSBill Paul * 4. Neither the name of the author nor the names of any co-contributors 18ce4946daSBill Paul * may be used to endorse or promote products derived from this software 19ce4946daSBill Paul * without specific prior written permission. 20ce4946daSBill Paul * 21ce4946daSBill Paul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND 22ce4946daSBill Paul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23ce4946daSBill Paul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24ce4946daSBill Paul * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD 25ce4946daSBill Paul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26ce4946daSBill Paul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27ce4946daSBill Paul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28ce4946daSBill Paul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29ce4946daSBill Paul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30ce4946daSBill Paul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 31ce4946daSBill Paul * THE POSSIBILITY OF SUCH DAMAGE. 32ce4946daSBill Paul */ 33ce4946daSBill Paul 34aad970f1SDavid E. O'Brien #include <sys/cdefs.h> 35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 36aad970f1SDavid E. O'Brien 37ce4946daSBill Paul /* 38ce4946daSBill Paul * National Semiconductor DP83820/DP83821 gigabit ethernet driver 39ce4946daSBill Paul * for FreeBSD. Datasheets are available from: 40ce4946daSBill Paul * 41ce4946daSBill Paul * http://www.national.com/ds/DP/DP83820.pdf 42ce4946daSBill Paul * http://www.national.com/ds/DP/DP83821.pdf 43ce4946daSBill Paul * 44ce4946daSBill Paul * These chips are used on several low cost gigabit ethernet NICs 45ce4946daSBill Paul * sold by D-Link, Addtron, SMC and Asante. Both parts are 46ce4946daSBill Paul * virtually the same, except the 83820 is a 64-bit/32-bit part, 47ce4946daSBill Paul * while the 83821 is 32-bit only. 48ce4946daSBill Paul * 49ce4946daSBill Paul * Many cards also use National gigE transceivers, such as the 50ce4946daSBill Paul * DP83891, DP83861 and DP83862 gigPHYTER parts. The DP83861 datasheet 51ce4946daSBill Paul * contains a full register description that applies to all of these 52ce4946daSBill Paul * components: 53ce4946daSBill Paul * 54ce4946daSBill Paul * http://www.national.com/ds/DP/DP83861.pdf 55ce4946daSBill Paul * 56ce4946daSBill Paul * Written by Bill Paul <wpaul@bsdi.com> 57ce4946daSBill Paul * BSDi Open Source Solutions 58ce4946daSBill Paul */ 59ce4946daSBill Paul 60ce4946daSBill Paul /* 61ce4946daSBill Paul * The NatSemi DP83820 and 83821 controllers are enhanced versions 62ce4946daSBill Paul * of the NatSemi MacPHYTER 10/100 devices. They support 10, 100 63ce4946daSBill Paul * and 1000Mbps speeds with 1000baseX (ten bit interface), MII and GMII 64ce4946daSBill Paul * ports. Other features include 8K TX FIFO and 32K RX FIFO, TCP/IP 65ce4946daSBill Paul * hardware checksum offload (IPv4 only), VLAN tagging and filtering, 66ce4946daSBill Paul * priority TX and RX queues, a 2048 bit multicast hash filter, 4 RX pattern 67ce4946daSBill Paul * matching buffers, one perfect address filter buffer and interrupt 68ce4946daSBill Paul * moderation. The 83820 supports both 64-bit and 32-bit addressing 69ce4946daSBill Paul * and data transfers: the 64-bit support can be toggled on or off 70ce4946daSBill Paul * via software. This affects the size of certain fields in the DMA 71ce4946daSBill Paul * descriptors. 72ce4946daSBill Paul * 73cb2f755cSBill Paul * There are two bugs/misfeatures in the 83820/83821 that I have 74cb2f755cSBill Paul * discovered so far: 75cb2f755cSBill Paul * 76cb2f755cSBill Paul * - Receive buffers must be aligned on 64-bit boundaries, which means 77cb2f755cSBill Paul * you must resort to copying data in order to fix up the payload 78cb2f755cSBill Paul * alignment. 79cb2f755cSBill Paul * 80cb2f755cSBill Paul * - In order to transmit jumbo frames larger than 8170 bytes, you have 81cb2f755cSBill Paul * to turn off transmit checksum offloading, because the chip can't 82cb2f755cSBill Paul * compute the checksum on an outgoing frame unless it fits entirely 83cb2f755cSBill Paul * within the TX FIFO, which is only 8192 bytes in size. If you have 84cb2f755cSBill Paul * TX checksum offload enabled and you transmit attempt to transmit a 85cb2f755cSBill Paul * frame larger than 8170 bytes, the transmitter will wedge. 86cb2f755cSBill Paul * 87cb2f755cSBill Paul * To work around the latter problem, TX checksum offload is disabled 88cb2f755cSBill Paul * if the user selects an MTU larger than 8152 (8170 - 18). 89ce4946daSBill Paul */ 90ce4946daSBill Paul 91f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS 92f0796cd2SGleb Smirnoff #include "opt_device_polling.h" 93f0796cd2SGleb Smirnoff #endif 94f0796cd2SGleb Smirnoff 95ce4946daSBill Paul #include <sys/param.h> 96ce4946daSBill Paul #include <sys/systm.h> 97f6bc9430SPyun YongHyeon #include <sys/bus.h> 98f6bc9430SPyun YongHyeon #include <sys/endian.h> 99ce4946daSBill Paul #include <sys/kernel.h> 100f6bc9430SPyun YongHyeon #include <sys/lock.h> 101f6bc9430SPyun YongHyeon #include <sys/malloc.h> 102f6bc9430SPyun YongHyeon #include <sys/mbuf.h> 103f6bc9430SPyun YongHyeon #include <sys/module.h> 104f6bc9430SPyun YongHyeon #include <sys/mutex.h> 105f6bc9430SPyun YongHyeon #include <sys/rman.h> 106ce4946daSBill Paul #include <sys/socket.h> 107f6bc9430SPyun YongHyeon #include <sys/sockio.h> 108f6bc9430SPyun YongHyeon #include <sys/sysctl.h> 109ce4946daSBill Paul 110f6bc9430SPyun YongHyeon #include <net/bpf.h> 111ce4946daSBill Paul #include <net/if.h> 112ce4946daSBill Paul #include <net/if_arp.h> 113ce4946daSBill Paul #include <net/ethernet.h> 114ce4946daSBill Paul #include <net/if_dl.h> 115ce4946daSBill Paul #include <net/if_media.h> 116ce4946daSBill Paul #include <net/if_types.h> 117ce4946daSBill Paul #include <net/if_vlan_var.h> 118ce4946daSBill Paul 119ce4946daSBill Paul #include <dev/mii/mii.h> 120ce4946daSBill Paul #include <dev/mii/miivar.h> 121ce4946daSBill Paul 12238d8c994SWarner Losh #include <dev/pci/pcireg.h> 12338d8c994SWarner Losh #include <dev/pci/pcivar.h> 124ce4946daSBill Paul 125f6bc9430SPyun YongHyeon #include <machine/bus.h> 126ce4946daSBill Paul 1275da751e4SBill Paul #include <dev/nge/if_ngereg.h> 128ce4946daSBill Paul 129f6bc9430SPyun YongHyeon /* "device miibus" required. See GENERIC if you get errors here. */ 130f6bc9430SPyun YongHyeon #include "miibus_if.h" 131f6bc9430SPyun YongHyeon 132f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, pci, 1, 1, 1); 133f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, ether, 1, 1, 1); 134ce4946daSBill Paul MODULE_DEPEND(nge, miibus, 1, 1, 1); 135ce4946daSBill Paul 136ce4946daSBill Paul #define NGE_CSUM_FEATURES (CSUM_IP | CSUM_TCP | CSUM_UDP) 137ce4946daSBill Paul 138ce4946daSBill Paul /* 139ce4946daSBill Paul * Various supported device vendors/types and their names. 140ce4946daSBill Paul */ 141ce4946daSBill Paul static struct nge_type nge_devs[] = { 142ce4946daSBill Paul { NGE_VENDORID, NGE_DEVICEID, 143ce4946daSBill Paul "National Semiconductor Gigabit Ethernet" }, 144ce4946daSBill Paul { 0, 0, NULL } 145ce4946daSBill Paul }; 146ce4946daSBill Paul 147e51a25f8SAlfred Perlstein static int nge_probe(device_t); 148e51a25f8SAlfred Perlstein static int nge_attach(device_t); 149e51a25f8SAlfred Perlstein static int nge_detach(device_t); 150f6bc9430SPyun YongHyeon static int nge_shutdown(device_t); 151f6bc9430SPyun YongHyeon static int nge_suspend(device_t); 152f6bc9430SPyun YongHyeon static int nge_resume(device_t); 153ce4946daSBill Paul 154f6bc9430SPyun YongHyeon static __inline void nge_discard_rxbuf(struct nge_softc *, int); 155f6bc9430SPyun YongHyeon static int nge_newbuf(struct nge_softc *, int); 156f6bc9430SPyun YongHyeon static int nge_encap(struct nge_softc *, struct mbuf **); 157f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT 158ad6c618bSBill Paul static __inline void nge_fixup_rx(struct mbuf *); 159ad6c618bSBill Paul #endif 16056e13f2aSAttilio Rao static int nge_rxeof(struct nge_softc *); 161e51a25f8SAlfred Perlstein static void nge_txeof(struct nge_softc *); 162e51a25f8SAlfred Perlstein static void nge_intr(void *); 163e51a25f8SAlfred Perlstein static void nge_tick(void *); 164f6bc9430SPyun YongHyeon static void nge_stats_update(struct nge_softc *); 165e51a25f8SAlfred Perlstein static void nge_start(struct ifnet *); 166ad6c618bSBill Paul static void nge_start_locked(struct ifnet *); 167e51a25f8SAlfred Perlstein static int nge_ioctl(struct ifnet *, u_long, caddr_t); 168e51a25f8SAlfred Perlstein static void nge_init(void *); 169ad6c618bSBill Paul static void nge_init_locked(struct nge_softc *); 170f6bc9430SPyun YongHyeon static int nge_stop_mac(struct nge_softc *); 171e51a25f8SAlfred Perlstein static void nge_stop(struct nge_softc *); 172f6bc9430SPyun YongHyeon static void nge_wol(struct nge_softc *); 173f6bc9430SPyun YongHyeon static void nge_watchdog(struct nge_softc *); 174f6bc9430SPyun YongHyeon static int nge_mediachange(struct ifnet *); 175f6bc9430SPyun YongHyeon static void nge_mediastatus(struct ifnet *, struct ifmediareq *); 176ce4946daSBill Paul 177e51a25f8SAlfred Perlstein static void nge_delay(struct nge_softc *); 178e51a25f8SAlfred Perlstein static void nge_eeprom_idle(struct nge_softc *); 179e51a25f8SAlfred Perlstein static void nge_eeprom_putbyte(struct nge_softc *, int); 1803929ff51SPyun YongHyeon static void nge_eeprom_getword(struct nge_softc *, int, uint16_t *); 181f6bc9430SPyun YongHyeon static void nge_read_eeprom(struct nge_softc *, caddr_t, int, int); 182ce4946daSBill Paul 183e51a25f8SAlfred Perlstein static void nge_mii_sync(struct nge_softc *); 1843929ff51SPyun YongHyeon static void nge_mii_send(struct nge_softc *, uint32_t, int); 185e51a25f8SAlfred Perlstein static int nge_mii_readreg(struct nge_softc *, struct nge_mii_frame *); 186e51a25f8SAlfred Perlstein static int nge_mii_writereg(struct nge_softc *, struct nge_mii_frame *); 187ce4946daSBill Paul 188e51a25f8SAlfred Perlstein static int nge_miibus_readreg(device_t, int, int); 189e51a25f8SAlfred Perlstein static int nge_miibus_writereg(device_t, int, int, int); 190e51a25f8SAlfred Perlstein static void nge_miibus_statchg(device_t); 191ce4946daSBill Paul 192f6bc9430SPyun YongHyeon static void nge_rxfilter(struct nge_softc *); 193e51a25f8SAlfred Perlstein static void nge_reset(struct nge_softc *); 194f6bc9430SPyun YongHyeon static void nge_dmamap_cb(void *, bus_dma_segment_t *, int, int); 195f6bc9430SPyun YongHyeon static int nge_dma_alloc(struct nge_softc *); 196f6bc9430SPyun YongHyeon static void nge_dma_free(struct nge_softc *); 197e51a25f8SAlfred Perlstein static int nge_list_rx_init(struct nge_softc *); 198e51a25f8SAlfred Perlstein static int nge_list_tx_init(struct nge_softc *); 199f6bc9430SPyun YongHyeon static void nge_sysctl_node(struct nge_softc *); 200f6bc9430SPyun YongHyeon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int); 201f6bc9430SPyun YongHyeon static int sysctl_hw_nge_int_holdoff(SYSCTL_HANDLER_ARGS); 202ce4946daSBill Paul 203ce4946daSBill Paul static device_method_t nge_methods[] = { 204ce4946daSBill Paul /* Device interface */ 205ce4946daSBill Paul DEVMETHOD(device_probe, nge_probe), 206ce4946daSBill Paul DEVMETHOD(device_attach, nge_attach), 207ce4946daSBill Paul DEVMETHOD(device_detach, nge_detach), 208ce4946daSBill Paul DEVMETHOD(device_shutdown, nge_shutdown), 209f6bc9430SPyun YongHyeon DEVMETHOD(device_suspend, nge_suspend), 210f6bc9430SPyun YongHyeon DEVMETHOD(device_resume, nge_resume), 211ce4946daSBill Paul 212ce4946daSBill Paul /* bus interface */ 213ce4946daSBill Paul DEVMETHOD(bus_print_child, bus_generic_print_child), 214ce4946daSBill Paul DEVMETHOD(bus_driver_added, bus_generic_driver_added), 215ce4946daSBill Paul 216ce4946daSBill Paul /* MII interface */ 217ce4946daSBill Paul DEVMETHOD(miibus_readreg, nge_miibus_readreg), 218ce4946daSBill Paul DEVMETHOD(miibus_writereg, nge_miibus_writereg), 219ce4946daSBill Paul DEVMETHOD(miibus_statchg, nge_miibus_statchg), 220ce4946daSBill Paul 221f6bc9430SPyun YongHyeon { NULL, NULL } 222ce4946daSBill Paul }; 223ce4946daSBill Paul 224ce4946daSBill Paul static driver_t nge_driver = { 225ce4946daSBill Paul "nge", 226ce4946daSBill Paul nge_methods, 227ce4946daSBill Paul sizeof(struct nge_softc) 228ce4946daSBill Paul }; 229ce4946daSBill Paul 230ce4946daSBill Paul static devclass_t nge_devclass; 231ce4946daSBill Paul 232f246e4a1SMatthew N. Dodd DRIVER_MODULE(nge, pci, nge_driver, nge_devclass, 0, 0); 233ce4946daSBill Paul DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0); 234ce4946daSBill Paul 235ce4946daSBill Paul #define NGE_SETBIT(sc, reg, x) \ 236ce4946daSBill Paul CSR_WRITE_4(sc, reg, \ 237ce4946daSBill Paul CSR_READ_4(sc, reg) | (x)) 238ce4946daSBill Paul 239ce4946daSBill Paul #define NGE_CLRBIT(sc, reg, x) \ 240ce4946daSBill Paul CSR_WRITE_4(sc, reg, \ 241ce4946daSBill Paul CSR_READ_4(sc, reg) & ~(x)) 242ce4946daSBill Paul 243ce4946daSBill Paul #define SIO_SET(x) \ 24429f19445SAlfred Perlstein CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x)) 245ce4946daSBill Paul 246ce4946daSBill Paul #define SIO_CLR(x) \ 24729f19445SAlfred Perlstein CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x)) 248ce4946daSBill Paul 249eaabec55SAlfred Perlstein static void 250284c81cbSPyun YongHyeon nge_delay(struct nge_softc *sc) 251ce4946daSBill Paul { 252ce4946daSBill Paul int idx; 253ce4946daSBill Paul 254ce4946daSBill Paul for (idx = (300 / 33) + 1; idx > 0; idx--) 255ce4946daSBill Paul CSR_READ_4(sc, NGE_CSR); 256ce4946daSBill Paul } 257ce4946daSBill Paul 258eaabec55SAlfred Perlstein static void 259284c81cbSPyun YongHyeon nge_eeprom_idle(struct nge_softc *sc) 260ce4946daSBill Paul { 2612cf2d799SPyun YongHyeon int i; 262ce4946daSBill Paul 263ce4946daSBill Paul SIO_SET(NGE_MEAR_EE_CSEL); 264ce4946daSBill Paul nge_delay(sc); 265ce4946daSBill Paul SIO_SET(NGE_MEAR_EE_CLK); 266ce4946daSBill Paul nge_delay(sc); 267ce4946daSBill Paul 268ce4946daSBill Paul for (i = 0; i < 25; i++) { 269ce4946daSBill Paul SIO_CLR(NGE_MEAR_EE_CLK); 270ce4946daSBill Paul nge_delay(sc); 271ce4946daSBill Paul SIO_SET(NGE_MEAR_EE_CLK); 272ce4946daSBill Paul nge_delay(sc); 273ce4946daSBill Paul } 274ce4946daSBill Paul 275ce4946daSBill Paul SIO_CLR(NGE_MEAR_EE_CLK); 276ce4946daSBill Paul nge_delay(sc); 277ce4946daSBill Paul SIO_CLR(NGE_MEAR_EE_CSEL); 278ce4946daSBill Paul nge_delay(sc); 279ce4946daSBill Paul CSR_WRITE_4(sc, NGE_MEAR, 0x00000000); 280ce4946daSBill Paul } 281ce4946daSBill Paul 282ce4946daSBill Paul /* 283ce4946daSBill Paul * Send a read command and address to the EEPROM, check for ACK. 284ce4946daSBill Paul */ 285eaabec55SAlfred Perlstein static void 286284c81cbSPyun YongHyeon nge_eeprom_putbyte(struct nge_softc *sc, int addr) 287ce4946daSBill Paul { 2882cf2d799SPyun YongHyeon int d, i; 289ce4946daSBill Paul 290ce4946daSBill Paul d = addr | NGE_EECMD_READ; 291ce4946daSBill Paul 292ce4946daSBill Paul /* 293ce4946daSBill Paul * Feed in each bit and stobe the clock. 294ce4946daSBill Paul */ 295ce4946daSBill Paul for (i = 0x400; i; i >>= 1) { 296ce4946daSBill Paul if (d & i) { 297ce4946daSBill Paul SIO_SET(NGE_MEAR_EE_DIN); 298ce4946daSBill Paul } else { 299ce4946daSBill Paul SIO_CLR(NGE_MEAR_EE_DIN); 300ce4946daSBill Paul } 301ce4946daSBill Paul nge_delay(sc); 302ce4946daSBill Paul SIO_SET(NGE_MEAR_EE_CLK); 303ce4946daSBill Paul nge_delay(sc); 304ce4946daSBill Paul SIO_CLR(NGE_MEAR_EE_CLK); 305ce4946daSBill Paul nge_delay(sc); 306ce4946daSBill Paul } 307ce4946daSBill Paul } 308ce4946daSBill Paul 309ce4946daSBill Paul /* 310ce4946daSBill Paul * Read a word of data stored in the EEPROM at address 'addr.' 311ce4946daSBill Paul */ 312eaabec55SAlfred Perlstein static void 3133929ff51SPyun YongHyeon nge_eeprom_getword(struct nge_softc *sc, int addr, uint16_t *dest) 314ce4946daSBill Paul { 3152cf2d799SPyun YongHyeon int i; 3163929ff51SPyun YongHyeon uint16_t word = 0; 317ce4946daSBill Paul 318ce4946daSBill Paul /* Force EEPROM to idle state. */ 319ce4946daSBill Paul nge_eeprom_idle(sc); 320ce4946daSBill Paul 321ce4946daSBill Paul /* Enter EEPROM access mode. */ 322ce4946daSBill Paul nge_delay(sc); 323ce4946daSBill Paul SIO_CLR(NGE_MEAR_EE_CLK); 324ce4946daSBill Paul nge_delay(sc); 325ce4946daSBill Paul SIO_SET(NGE_MEAR_EE_CSEL); 326ce4946daSBill Paul nge_delay(sc); 327ce4946daSBill Paul 328ce4946daSBill Paul /* 329ce4946daSBill Paul * Send address of word we want to read. 330ce4946daSBill Paul */ 331ce4946daSBill Paul nge_eeprom_putbyte(sc, addr); 332ce4946daSBill Paul 333ce4946daSBill Paul /* 334ce4946daSBill Paul * Start reading bits from EEPROM. 335ce4946daSBill Paul */ 336ce4946daSBill Paul for (i = 0x8000; i; i >>= 1) { 337ce4946daSBill Paul SIO_SET(NGE_MEAR_EE_CLK); 338ce4946daSBill Paul nge_delay(sc); 339ce4946daSBill Paul if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT) 340ce4946daSBill Paul word |= i; 341ce4946daSBill Paul nge_delay(sc); 342ce4946daSBill Paul SIO_CLR(NGE_MEAR_EE_CLK); 343ce4946daSBill Paul nge_delay(sc); 344ce4946daSBill Paul } 345ce4946daSBill Paul 346ce4946daSBill Paul /* Turn off EEPROM access mode. */ 347ce4946daSBill Paul nge_eeprom_idle(sc); 348ce4946daSBill Paul 349ce4946daSBill Paul *dest = word; 350ce4946daSBill Paul } 351ce4946daSBill Paul 352ce4946daSBill Paul /* 353ce4946daSBill Paul * Read a sequence of words from the EEPROM. 354ce4946daSBill Paul */ 355eaabec55SAlfred Perlstein static void 356f6bc9430SPyun YongHyeon nge_read_eeprom(struct nge_softc *sc, caddr_t dest, int off, int cnt) 357ce4946daSBill Paul { 358ce4946daSBill Paul int i; 3593929ff51SPyun YongHyeon uint16_t word = 0, *ptr; 360ce4946daSBill Paul 361ce4946daSBill Paul for (i = 0; i < cnt; i++) { 362ce4946daSBill Paul nge_eeprom_getword(sc, off + i, &word); 3633929ff51SPyun YongHyeon ptr = (uint16_t *)(dest + (i * 2)); 364ce4946daSBill Paul *ptr = word; 365ce4946daSBill Paul } 366ce4946daSBill Paul } 367ce4946daSBill Paul 368ce4946daSBill Paul /* 369ce4946daSBill Paul * Sync the PHYs by setting data bit and strobing the clock 32 times. 370ce4946daSBill Paul */ 371eaabec55SAlfred Perlstein static void 372284c81cbSPyun YongHyeon nge_mii_sync(struct nge_softc *sc) 373ce4946daSBill Paul { 3742cf2d799SPyun YongHyeon int i; 375ce4946daSBill Paul 376ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_DIR|NGE_MEAR_MII_DATA); 377ce4946daSBill Paul 378ce4946daSBill Paul for (i = 0; i < 32; i++) { 379ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_CLK); 380ce4946daSBill Paul DELAY(1); 381ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_CLK); 382ce4946daSBill Paul DELAY(1); 383ce4946daSBill Paul } 384ce4946daSBill Paul } 385ce4946daSBill Paul 386ce4946daSBill Paul /* 387ce4946daSBill Paul * Clock a series of bits through the MII. 388ce4946daSBill Paul */ 389eaabec55SAlfred Perlstein static void 3903929ff51SPyun YongHyeon nge_mii_send(struct nge_softc *sc, uint32_t bits, int cnt) 391ce4946daSBill Paul { 392ce4946daSBill Paul int i; 393ce4946daSBill Paul 394ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_CLK); 395ce4946daSBill Paul 396ce4946daSBill Paul for (i = (0x1 << (cnt - 1)); i; i >>= 1) { 397ce4946daSBill Paul if (bits & i) { 398ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_DATA); 399ce4946daSBill Paul } else { 400ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_DATA); 401ce4946daSBill Paul } 402ce4946daSBill Paul DELAY(1); 403ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_CLK); 404ce4946daSBill Paul DELAY(1); 405ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_CLK); 406ce4946daSBill Paul } 407ce4946daSBill Paul } 408ce4946daSBill Paul 409ce4946daSBill Paul /* 410ce4946daSBill Paul * Read an PHY register through the MII. 411ce4946daSBill Paul */ 412eaabec55SAlfred Perlstein static int 413284c81cbSPyun YongHyeon nge_mii_readreg(struct nge_softc *sc, struct nge_mii_frame *frame) 414ce4946daSBill Paul { 415ad6c618bSBill Paul int i, ack; 416ce4946daSBill Paul 417ce4946daSBill Paul /* 418ce4946daSBill Paul * Set up frame for RX. 419ce4946daSBill Paul */ 420ce4946daSBill Paul frame->mii_stdelim = NGE_MII_STARTDELIM; 421ce4946daSBill Paul frame->mii_opcode = NGE_MII_READOP; 422ce4946daSBill Paul frame->mii_turnaround = 0; 423ce4946daSBill Paul frame->mii_data = 0; 424ce4946daSBill Paul 425ce4946daSBill Paul CSR_WRITE_4(sc, NGE_MEAR, 0); 426ce4946daSBill Paul 427ce4946daSBill Paul /* 428ce4946daSBill Paul * Turn on data xmit. 429ce4946daSBill Paul */ 430ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_DIR); 431ce4946daSBill Paul 432ce4946daSBill Paul nge_mii_sync(sc); 433ce4946daSBill Paul 434ce4946daSBill Paul /* 435ce4946daSBill Paul * Send command/address info. 436ce4946daSBill Paul */ 437ce4946daSBill Paul nge_mii_send(sc, frame->mii_stdelim, 2); 438ce4946daSBill Paul nge_mii_send(sc, frame->mii_opcode, 2); 439ce4946daSBill Paul nge_mii_send(sc, frame->mii_phyaddr, 5); 440ce4946daSBill Paul nge_mii_send(sc, frame->mii_regaddr, 5); 441ce4946daSBill Paul 442ce4946daSBill Paul /* Idle bit */ 443ce4946daSBill Paul SIO_CLR((NGE_MEAR_MII_CLK|NGE_MEAR_MII_DATA)); 444ce4946daSBill Paul DELAY(1); 445ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_CLK); 446ce4946daSBill Paul DELAY(1); 447ce4946daSBill Paul 448ce4946daSBill Paul /* Turn off xmit. */ 449ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_DIR); 450ce4946daSBill Paul /* Check for ack */ 451ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_CLK); 452ce4946daSBill Paul DELAY(1); 453e808cf62SMartin Blapp ack = CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA; 454ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_CLK); 455ce4946daSBill Paul DELAY(1); 456ce4946daSBill Paul 457ce4946daSBill Paul /* 458ce4946daSBill Paul * Now try reading data bits. If the ack failed, we still 459ce4946daSBill Paul * need to clock through 16 cycles to keep the PHY(s) in sync. 460ce4946daSBill Paul */ 461ce4946daSBill Paul if (ack) { 462ce4946daSBill Paul for (i = 0; i < 16; i++) { 463ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_CLK); 464ce4946daSBill Paul DELAY(1); 465ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_CLK); 466ce4946daSBill Paul DELAY(1); 467ce4946daSBill Paul } 468ce4946daSBill Paul goto fail; 469ce4946daSBill Paul } 470ce4946daSBill Paul 471ce4946daSBill Paul for (i = 0x8000; i; i >>= 1) { 472ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_CLK); 473ce4946daSBill Paul DELAY(1); 474ce4946daSBill Paul if (!ack) { 475ce4946daSBill Paul if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA) 476ce4946daSBill Paul frame->mii_data |= i; 477ce4946daSBill Paul DELAY(1); 478ce4946daSBill Paul } 479ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_CLK); 480ce4946daSBill Paul DELAY(1); 481ce4946daSBill Paul } 482ce4946daSBill Paul 483ce4946daSBill Paul fail: 484ce4946daSBill Paul 485ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_CLK); 486ce4946daSBill Paul DELAY(1); 487ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_CLK); 488ce4946daSBill Paul DELAY(1); 489ce4946daSBill Paul 490ce4946daSBill Paul if (ack) 491ce4946daSBill Paul return (1); 492ce4946daSBill Paul return (0); 493ce4946daSBill Paul } 494ce4946daSBill Paul 495ce4946daSBill Paul /* 496ce4946daSBill Paul * Write to a PHY register through the MII. 497ce4946daSBill Paul */ 498eaabec55SAlfred Perlstein static int 499284c81cbSPyun YongHyeon nge_mii_writereg(struct nge_softc *sc, struct nge_mii_frame *frame) 500ce4946daSBill Paul { 501ce4946daSBill Paul 502ce4946daSBill Paul /* 503ce4946daSBill Paul * Set up frame for TX. 504ce4946daSBill Paul */ 505ce4946daSBill Paul 506ce4946daSBill Paul frame->mii_stdelim = NGE_MII_STARTDELIM; 507ce4946daSBill Paul frame->mii_opcode = NGE_MII_WRITEOP; 508ce4946daSBill Paul frame->mii_turnaround = NGE_MII_TURNAROUND; 509ce4946daSBill Paul 510ce4946daSBill Paul /* 511ce4946daSBill Paul * Turn on data output. 512ce4946daSBill Paul */ 513ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_DIR); 514ce4946daSBill Paul 515ce4946daSBill Paul nge_mii_sync(sc); 516ce4946daSBill Paul 517ce4946daSBill Paul nge_mii_send(sc, frame->mii_stdelim, 2); 518ce4946daSBill Paul nge_mii_send(sc, frame->mii_opcode, 2); 519ce4946daSBill Paul nge_mii_send(sc, frame->mii_phyaddr, 5); 520ce4946daSBill Paul nge_mii_send(sc, frame->mii_regaddr, 5); 521ce4946daSBill Paul nge_mii_send(sc, frame->mii_turnaround, 2); 522ce4946daSBill Paul nge_mii_send(sc, frame->mii_data, 16); 523ce4946daSBill Paul 524ce4946daSBill Paul /* Idle bit. */ 525ce4946daSBill Paul SIO_SET(NGE_MEAR_MII_CLK); 526ce4946daSBill Paul DELAY(1); 527ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_CLK); 528ce4946daSBill Paul DELAY(1); 529ce4946daSBill Paul 530ce4946daSBill Paul /* 531ce4946daSBill Paul * Turn off xmit. 532ce4946daSBill Paul */ 533ce4946daSBill Paul SIO_CLR(NGE_MEAR_MII_DIR); 534ce4946daSBill Paul 535ce4946daSBill Paul return (0); 536ce4946daSBill Paul } 537ce4946daSBill Paul 538eaabec55SAlfred Perlstein static int 539284c81cbSPyun YongHyeon nge_miibus_readreg(device_t dev, int phy, int reg) 540ce4946daSBill Paul { 541ce4946daSBill Paul struct nge_softc *sc; 542ce4946daSBill Paul struct nge_mii_frame frame; 543f6bc9430SPyun YongHyeon int rv; 544ce4946daSBill Paul 545ce4946daSBill Paul sc = device_get_softc(dev); 546f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_TBI) != 0) { 547f6bc9430SPyun YongHyeon /* Pretend PHY is at address 0. */ 548f6bc9430SPyun YongHyeon if (phy != 0) 549f6bc9430SPyun YongHyeon return (0); 550f6bc9430SPyun YongHyeon switch (reg) { 551f6bc9430SPyun YongHyeon case MII_BMCR: 552f6bc9430SPyun YongHyeon reg = NGE_TBI_BMCR; 553f6bc9430SPyun YongHyeon break; 554f6bc9430SPyun YongHyeon case MII_BMSR: 555f6bc9430SPyun YongHyeon /* 83820/83821 has different bit layout for BMSR. */ 556f6bc9430SPyun YongHyeon rv = BMSR_ANEG | BMSR_EXTCAP | BMSR_EXTSTAT; 557f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_TBI_BMSR); 558f6bc9430SPyun YongHyeon if ((reg & NGE_TBIBMSR_ANEG_DONE) != 0) 559f6bc9430SPyun YongHyeon rv |= BMSR_ACOMP; 560f6bc9430SPyun YongHyeon if ((reg & NGE_TBIBMSR_LINKSTAT) != 0) 561f6bc9430SPyun YongHyeon rv |= BMSR_LINK; 562f6bc9430SPyun YongHyeon return (rv); 563f6bc9430SPyun YongHyeon case MII_ANAR: 564f6bc9430SPyun YongHyeon reg = NGE_TBI_ANAR; 565f6bc9430SPyun YongHyeon break; 566f6bc9430SPyun YongHyeon case MII_ANLPAR: 567f6bc9430SPyun YongHyeon reg = NGE_TBI_ANLPAR; 568f6bc9430SPyun YongHyeon break; 569f6bc9430SPyun YongHyeon case MII_ANER: 570f6bc9430SPyun YongHyeon reg = NGE_TBI_ANER; 571f6bc9430SPyun YongHyeon break; 572f6bc9430SPyun YongHyeon case MII_EXTSR: 573f6bc9430SPyun YongHyeon reg = NGE_TBI_ESR; 574f6bc9430SPyun YongHyeon break; 575f6bc9430SPyun YongHyeon case MII_PHYIDR1: 576f6bc9430SPyun YongHyeon case MII_PHYIDR2: 577f6bc9430SPyun YongHyeon return (0); 578f6bc9430SPyun YongHyeon default: 579f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 580f6bc9430SPyun YongHyeon "bad phy register read : %d\n", reg); 581f6bc9430SPyun YongHyeon return (0); 582f6bc9430SPyun YongHyeon } 583f6bc9430SPyun YongHyeon return (CSR_READ_4(sc, reg)); 584f6bc9430SPyun YongHyeon } 585ce4946daSBill Paul 586ce4946daSBill Paul bzero((char *)&frame, sizeof(frame)); 587ce4946daSBill Paul 588ce4946daSBill Paul frame.mii_phyaddr = phy; 589ce4946daSBill Paul frame.mii_regaddr = reg; 590ce4946daSBill Paul nge_mii_readreg(sc, &frame); 591ce4946daSBill Paul 592ce4946daSBill Paul return (frame.mii_data); 593ce4946daSBill Paul } 594ce4946daSBill Paul 595eaabec55SAlfred Perlstein static int 596284c81cbSPyun YongHyeon nge_miibus_writereg(device_t dev, int phy, int reg, int data) 597ce4946daSBill Paul { 598ce4946daSBill Paul struct nge_softc *sc; 599ce4946daSBill Paul struct nge_mii_frame frame; 600ce4946daSBill Paul 601ce4946daSBill Paul sc = device_get_softc(dev); 602f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_TBI) != 0) { 603f6bc9430SPyun YongHyeon /* Pretend PHY is at address 0. */ 604f6bc9430SPyun YongHyeon if (phy != 0) 605f6bc9430SPyun YongHyeon return (0); 606f6bc9430SPyun YongHyeon switch (reg) { 607f6bc9430SPyun YongHyeon case MII_BMCR: 608f6bc9430SPyun YongHyeon reg = NGE_TBI_BMCR; 609f6bc9430SPyun YongHyeon break; 610f6bc9430SPyun YongHyeon case MII_BMSR: 611f6bc9430SPyun YongHyeon return (0); 612f6bc9430SPyun YongHyeon case MII_ANAR: 613f6bc9430SPyun YongHyeon reg = NGE_TBI_ANAR; 614f6bc9430SPyun YongHyeon break; 615f6bc9430SPyun YongHyeon case MII_ANLPAR: 616f6bc9430SPyun YongHyeon reg = NGE_TBI_ANLPAR; 617f6bc9430SPyun YongHyeon break; 618f6bc9430SPyun YongHyeon case MII_ANER: 619f6bc9430SPyun YongHyeon reg = NGE_TBI_ANER; 620f6bc9430SPyun YongHyeon break; 621f6bc9430SPyun YongHyeon case MII_EXTSR: 622f6bc9430SPyun YongHyeon reg = NGE_TBI_ESR; 623f6bc9430SPyun YongHyeon break; 624f6bc9430SPyun YongHyeon case MII_PHYIDR1: 625f6bc9430SPyun YongHyeon case MII_PHYIDR2: 626f6bc9430SPyun YongHyeon return (0); 627f6bc9430SPyun YongHyeon default: 628f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 629f6bc9430SPyun YongHyeon "bad phy register write : %d\n", reg); 630f6bc9430SPyun YongHyeon return (0); 631f6bc9430SPyun YongHyeon } 632f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, reg, data); 633f6bc9430SPyun YongHyeon return (0); 634f6bc9430SPyun YongHyeon } 635ce4946daSBill Paul 636ce4946daSBill Paul bzero((char *)&frame, sizeof(frame)); 637ce4946daSBill Paul 638ce4946daSBill Paul frame.mii_phyaddr = phy; 639ce4946daSBill Paul frame.mii_regaddr = reg; 640ce4946daSBill Paul frame.mii_data = data; 641ce4946daSBill Paul nge_mii_writereg(sc, &frame); 642ce4946daSBill Paul 643ce4946daSBill Paul return (0); 644ce4946daSBill Paul } 645ce4946daSBill Paul 646f6bc9430SPyun YongHyeon /* 647f6bc9430SPyun YongHyeon * media status/link state change handler. 648f6bc9430SPyun YongHyeon */ 649eaabec55SAlfred Perlstein static void 650284c81cbSPyun YongHyeon nge_miibus_statchg(device_t dev) 651ce4946daSBill Paul { 652ce4946daSBill Paul struct nge_softc *sc; 653ce4946daSBill Paul struct mii_data *mii; 654f6bc9430SPyun YongHyeon struct ifnet *ifp; 655f6bc9430SPyun YongHyeon struct nge_txdesc *txd; 656f6bc9430SPyun YongHyeon uint32_t done, reg, status; 657f6bc9430SPyun YongHyeon int i; 658ce4946daSBill Paul 659ce4946daSBill Paul sc = device_get_softc(dev); 660f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 6611f548804SDoug Ambrisko 662ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 663f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 664f6bc9430SPyun YongHyeon if (mii == NULL || ifp == NULL || 665f6bc9430SPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 666f6bc9430SPyun YongHyeon return; 667ce4946daSBill Paul 668f6bc9430SPyun YongHyeon sc->nge_flags &= ~NGE_FLAG_LINK; 669f6bc9430SPyun YongHyeon if ((mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) == 670f6bc9430SPyun YongHyeon (IFM_AVALID | IFM_ACTIVE)) { 671f6bc9430SPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) { 672f6bc9430SPyun YongHyeon case IFM_10_T: 673f6bc9430SPyun YongHyeon case IFM_100_TX: 674f6bc9430SPyun YongHyeon case IFM_1000_T: 675f6bc9430SPyun YongHyeon case IFM_1000_SX: 676f6bc9430SPyun YongHyeon case IFM_1000_LX: 677f6bc9430SPyun YongHyeon case IFM_1000_CX: 678f6bc9430SPyun YongHyeon sc->nge_flags |= NGE_FLAG_LINK; 679f6bc9430SPyun YongHyeon break; 680f6bc9430SPyun YongHyeon default: 681f6bc9430SPyun YongHyeon break; 682f6bc9430SPyun YongHyeon } 683f6bc9430SPyun YongHyeon } 684f6bc9430SPyun YongHyeon 685f6bc9430SPyun YongHyeon /* Stop Tx/Rx MACs. */ 686f6bc9430SPyun YongHyeon if (nge_stop_mac(sc) == ETIMEDOUT) 687f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 688f6bc9430SPyun YongHyeon "%s: unable to stop Tx/Rx MAC\n", __func__); 689f6bc9430SPyun YongHyeon nge_txeof(sc); 690f6bc9430SPyun YongHyeon nge_rxeof(sc); 691f6bc9430SPyun YongHyeon if (sc->nge_head != NULL) { 692f6bc9430SPyun YongHyeon m_freem(sc->nge_head); 693f6bc9430SPyun YongHyeon sc->nge_head = sc->nge_tail = NULL; 694f6bc9430SPyun YongHyeon } 695f6bc9430SPyun YongHyeon 696f6bc9430SPyun YongHyeon /* Release queued frames. */ 697f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TX_RING_CNT; i++) { 698f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[i]; 699f6bc9430SPyun YongHyeon if (txd->tx_m != NULL) { 700f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, 701f6bc9430SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 702f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, 703f6bc9430SPyun YongHyeon txd->tx_dmamap); 704f6bc9430SPyun YongHyeon m_freem(txd->tx_m); 705f6bc9430SPyun YongHyeon txd->tx_m = NULL; 706f6bc9430SPyun YongHyeon } 707f6bc9430SPyun YongHyeon } 708f6bc9430SPyun YongHyeon 709f6bc9430SPyun YongHyeon /* Program MAC with resolved speed/duplex. */ 710f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_LINK) != 0) { 711f6bc9430SPyun YongHyeon if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) { 712ce4946daSBill Paul NGE_SETBIT(sc, NGE_TX_CFG, 713ce4946daSBill Paul (NGE_TXCFG_IGN_HBEAT | NGE_TXCFG_IGN_CARR)); 714ce4946daSBill Paul NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 715f6bc9430SPyun YongHyeon #ifdef notyet 716f6bc9430SPyun YongHyeon /* Enable flow-control. */ 717f6bc9430SPyun YongHyeon if ((IFM_OPTIONS(mii->mii_media_active) & 718f6bc9430SPyun YongHyeon (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) != 0) 719f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_PAUSECSR, 720f6bc9430SPyun YongHyeon NGE_PAUSECSR_PAUSE_ENB); 721f6bc9430SPyun YongHyeon #endif 722ce4946daSBill Paul } else { 723ce4946daSBill Paul NGE_CLRBIT(sc, NGE_TX_CFG, 724ce4946daSBill Paul (NGE_TXCFG_IGN_HBEAT | NGE_TXCFG_IGN_CARR)); 725ce4946daSBill Paul NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX); 726f6bc9430SPyun YongHyeon NGE_CLRBIT(sc, NGE_PAUSECSR, NGE_PAUSECSR_PAUSE_ENB); 727f6bc9430SPyun YongHyeon } 728f6bc9430SPyun YongHyeon /* If we have a 1000Mbps link, set the mode_1000 bit. */ 729f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_CFG); 730f6bc9430SPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) { 731f6bc9430SPyun YongHyeon case IFM_1000_SX: 732f6bc9430SPyun YongHyeon case IFM_1000_LX: 733f6bc9430SPyun YongHyeon case IFM_1000_CX: 734f6bc9430SPyun YongHyeon case IFM_1000_T: 735f6bc9430SPyun YongHyeon reg |= NGE_CFG_MODE_1000; 736f6bc9430SPyun YongHyeon break; 737f6bc9430SPyun YongHyeon default: 738f6bc9430SPyun YongHyeon reg &= ~NGE_CFG_MODE_1000; 739f6bc9430SPyun YongHyeon break; 740f6bc9430SPyun YongHyeon } 741f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CFG, reg); 742f6bc9430SPyun YongHyeon 743f6bc9430SPyun YongHyeon /* Reset Tx/Rx MAC. */ 744f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_CSR); 745f6bc9430SPyun YongHyeon reg |= NGE_CSR_TX_RESET | NGE_CSR_RX_RESET; 746f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CSR, reg); 747f6bc9430SPyun YongHyeon /* Check the completion of reset. */ 748f6bc9430SPyun YongHyeon done = 0; 749f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TIMEOUT; i++) { 750f6bc9430SPyun YongHyeon DELAY(1); 751f6bc9430SPyun YongHyeon status = CSR_READ_4(sc, NGE_ISR); 752f6bc9430SPyun YongHyeon if ((status & NGE_ISR_RX_RESET_DONE) != 0) 753f6bc9430SPyun YongHyeon done |= NGE_ISR_RX_RESET_DONE; 754f6bc9430SPyun YongHyeon if ((status & NGE_ISR_TX_RESET_DONE) != 0) 755f6bc9430SPyun YongHyeon done |= NGE_ISR_TX_RESET_DONE; 756f6bc9430SPyun YongHyeon if (done == 757f6bc9430SPyun YongHyeon (NGE_ISR_TX_RESET_DONE | NGE_ISR_RX_RESET_DONE)) 758f6bc9430SPyun YongHyeon break; 759f6bc9430SPyun YongHyeon } 760f6bc9430SPyun YongHyeon if (i == NGE_TIMEOUT) 761f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 762f6bc9430SPyun YongHyeon "%s: unable to reset Tx/Rx MAC\n", __func__); 763f6bc9430SPyun YongHyeon /* Reuse Rx buffer and reset consumer pointer. */ 764f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_cons = 0; 765f6bc9430SPyun YongHyeon /* 766f6bc9430SPyun YongHyeon * It seems that resetting Rx/Tx MAC results in 767f6bc9430SPyun YongHyeon * resetting Tx/Rx descriptor pointer registers such 768f6bc9430SPyun YongHyeon * that reloading Tx/Rx lists address are needed. 769f6bc9430SPyun YongHyeon */ 770f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 771f6bc9430SPyun YongHyeon NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr)); 772f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 773f6bc9430SPyun YongHyeon NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr)); 774f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI, 775f6bc9430SPyun YongHyeon NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr)); 776f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO, 777f6bc9430SPyun YongHyeon NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr)); 778f6bc9430SPyun YongHyeon /* Reinitialize Tx buffers. */ 779f6bc9430SPyun YongHyeon nge_list_tx_init(sc); 780f6bc9430SPyun YongHyeon 781f6bc9430SPyun YongHyeon /* Restart Rx MAC. */ 782f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_CSR); 783f6bc9430SPyun YongHyeon reg |= NGE_CSR_RX_ENABLE; 784f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CSR, reg); 785f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TIMEOUT; i++) { 786f6bc9430SPyun YongHyeon if ((CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RX_ENABLE) != 0) 787f6bc9430SPyun YongHyeon break; 788f6bc9430SPyun YongHyeon DELAY(1); 789f6bc9430SPyun YongHyeon } 790f6bc9430SPyun YongHyeon if (i == NGE_TIMEOUT) 791f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 792f6bc9430SPyun YongHyeon "%s: unable to restart Rx MAC\n", __func__); 793ce4946daSBill Paul } 794ce4946daSBill Paul 795f6bc9430SPyun YongHyeon /* Data LED off for TBI mode */ 796f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_TBI) != 0) 797f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_GPIO, 798f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT); 799ce4946daSBill Paul } 800ce4946daSBill Paul 801eaabec55SAlfred Perlstein static void 802f6bc9430SPyun YongHyeon nge_rxfilter(struct nge_softc *sc) 803ce4946daSBill Paul { 804ce4946daSBill Paul struct ifnet *ifp; 805ce4946daSBill Paul struct ifmultiaddr *ifma; 806f6bc9430SPyun YongHyeon uint32_t h, i, rxfilt; 807ce4946daSBill Paul int bit, index; 808ce4946daSBill Paul 809ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 810fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 811ce4946daSBill Paul 812f6bc9430SPyun YongHyeon /* Make sure to stop Rx filtering. */ 813f6bc9430SPyun YongHyeon rxfilt = CSR_READ_4(sc, NGE_RXFILT_CTL); 814f6bc9430SPyun YongHyeon rxfilt &= ~NGE_RXFILTCTL_ENABLE; 815f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt); 816f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_RXFILT_CTL); 817f6bc9430SPyun YongHyeon 818f6bc9430SPyun YongHyeon rxfilt &= ~(NGE_RXFILTCTL_ALLMULTI | NGE_RXFILTCTL_ALLPHYS); 819f6bc9430SPyun YongHyeon rxfilt &= ~NGE_RXFILTCTL_BROAD; 820f6bc9430SPyun YongHyeon /* 821f6bc9430SPyun YongHyeon * We don't want to use the hash table for matching unicast 822f6bc9430SPyun YongHyeon * addresses. 823f6bc9430SPyun YongHyeon */ 824f6bc9430SPyun YongHyeon rxfilt &= ~(NGE_RXFILTCTL_MCHASH | NGE_RXFILTCTL_UCHASH); 825f6bc9430SPyun YongHyeon 826f6bc9430SPyun YongHyeon /* 827f6bc9430SPyun YongHyeon * For the NatSemi chip, we have to explicitly enable the 828f6bc9430SPyun YongHyeon * reception of ARP frames, as well as turn on the 'perfect 829f6bc9430SPyun YongHyeon * match' filter where we store the station address, otherwise 830f6bc9430SPyun YongHyeon * we won't receive unicasts meant for this host. 831f6bc9430SPyun YongHyeon */ 832f6bc9430SPyun YongHyeon rxfilt |= NGE_RXFILTCTL_ARP | NGE_RXFILTCTL_PERFECT; 833f6bc9430SPyun YongHyeon 834f6bc9430SPyun YongHyeon /* 835f6bc9430SPyun YongHyeon * Set the capture broadcast bit to capture broadcast frames. 836f6bc9430SPyun YongHyeon */ 837f6bc9430SPyun YongHyeon if ((ifp->if_flags & IFF_BROADCAST) != 0) 838f6bc9430SPyun YongHyeon rxfilt |= NGE_RXFILTCTL_BROAD; 839f6bc9430SPyun YongHyeon 840f6bc9430SPyun YongHyeon if ((ifp->if_flags & IFF_PROMISC) != 0 || 841f6bc9430SPyun YongHyeon (ifp->if_flags & IFF_ALLMULTI) != 0) { 842f6bc9430SPyun YongHyeon rxfilt |= NGE_RXFILTCTL_ALLMULTI; 843f6bc9430SPyun YongHyeon if ((ifp->if_flags & IFF_PROMISC) != 0) 844f6bc9430SPyun YongHyeon rxfilt |= NGE_RXFILTCTL_ALLPHYS; 845f6bc9430SPyun YongHyeon goto done; 846ce4946daSBill Paul } 847ce4946daSBill Paul 848ce4946daSBill Paul /* 849ce4946daSBill Paul * We have to explicitly enable the multicast hash table 850ce4946daSBill Paul * on the NatSemi chip if we want to use it, which we do. 851ce4946daSBill Paul */ 852f6bc9430SPyun YongHyeon rxfilt |= NGE_RXFILTCTL_MCHASH; 853ce4946daSBill Paul 854ce4946daSBill Paul /* first, zot all the existing hash bits */ 855ce4946daSBill Paul for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) { 856ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i); 857ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0); 858ce4946daSBill Paul } 859ce4946daSBill Paul 860ce4946daSBill Paul /* 861ce4946daSBill Paul * From the 11 bits returned by the crc routine, the top 7 862ce4946daSBill Paul * bits represent the 16-bit word in the mcast hash table 863ce4946daSBill Paul * that needs to be updated, and the lower 4 bits represent 864ce4946daSBill Paul * which bit within that byte needs to be set. 865ce4946daSBill Paul */ 866eb956cd0SRobert Watson if_maddr_rlock(ifp); 867ce4946daSBill Paul TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) { 868ce4946daSBill Paul if (ifma->ifma_addr->sa_family != AF_LINK) 869ce4946daSBill Paul continue; 8700e939c0cSChristian Weisgerber h = ether_crc32_be(LLADDR((struct sockaddr_dl *) 8710e939c0cSChristian Weisgerber ifma->ifma_addr), ETHER_ADDR_LEN) >> 21; 872ce4946daSBill Paul index = (h >> 4) & 0x7F; 873ce4946daSBill Paul bit = h & 0xF; 874ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_CTL, 875ce4946daSBill Paul NGE_FILTADDR_MCAST_LO + (index * 2)); 876ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit)); 877ce4946daSBill Paul } 878eb956cd0SRobert Watson if_maddr_runlock(ifp); 879ce4946daSBill Paul 880f6bc9430SPyun YongHyeon done: 881f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt); 882f6bc9430SPyun YongHyeon /* Turn the receive filter on. */ 883f6bc9430SPyun YongHyeon rxfilt |= NGE_RXFILTCTL_ENABLE; 884f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt); 885f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_RXFILT_CTL); 886ce4946daSBill Paul } 887ce4946daSBill Paul 888eaabec55SAlfred Perlstein static void 889284c81cbSPyun YongHyeon nge_reset(struct nge_softc *sc) 890ce4946daSBill Paul { 891f6bc9430SPyun YongHyeon uint32_t v; 8922cf2d799SPyun YongHyeon int i; 893ce4946daSBill Paul 894ce4946daSBill Paul NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET); 895ce4946daSBill Paul 896ce4946daSBill Paul for (i = 0; i < NGE_TIMEOUT; i++) { 897ce4946daSBill Paul if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET)) 898ce4946daSBill Paul break; 899f6bc9430SPyun YongHyeon DELAY(1); 900ce4946daSBill Paul } 901ce4946daSBill Paul 902ce4946daSBill Paul if (i == NGE_TIMEOUT) 9036b9f5c94SGleb Smirnoff device_printf(sc->nge_dev, "reset never completed\n"); 904ce4946daSBill Paul 905ce4946daSBill Paul /* Wait a little while for the chip to get its brains in order. */ 906ce4946daSBill Paul DELAY(1000); 907ce4946daSBill Paul 908ce4946daSBill Paul /* 909ce4946daSBill Paul * If this is a NetSemi chip, make sure to clear 910ce4946daSBill Paul * PME mode. 911ce4946daSBill Paul */ 912ce4946daSBill Paul CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS); 913ce4946daSBill Paul CSR_WRITE_4(sc, NGE_CLKRUN, 0); 914f6bc9430SPyun YongHyeon 915f6bc9430SPyun YongHyeon /* Clear WOL events which may interfere normal Rx filter opertaion. */ 916f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_WOLCSR, 0); 917f6bc9430SPyun YongHyeon 918f6bc9430SPyun YongHyeon /* 919f6bc9430SPyun YongHyeon * Only DP83820 supports 64bits addressing/data transfers and 920f6bc9430SPyun YongHyeon * 64bit addressing requires different descriptor structures. 921f6bc9430SPyun YongHyeon * To make it simple, disable 64bit addressing/data transfers. 922f6bc9430SPyun YongHyeon */ 923f6bc9430SPyun YongHyeon v = CSR_READ_4(sc, NGE_CFG); 924f6bc9430SPyun YongHyeon v &= ~(NGE_CFG_64BIT_ADDR_ENB | NGE_CFG_64BIT_DATA_ENB); 925f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CFG, v); 926ce4946daSBill Paul } 927ce4946daSBill Paul 928ce4946daSBill Paul /* 929d64ada50SJens Schweikhardt * Probe for a NatSemi chip. Check the PCI vendor and device 930ce4946daSBill Paul * IDs against our list and return a device name if we find a match. 931ce4946daSBill Paul */ 932eaabec55SAlfred Perlstein static int 933284c81cbSPyun YongHyeon nge_probe(device_t dev) 934ce4946daSBill Paul { 935ce4946daSBill Paul struct nge_type *t; 936ce4946daSBill Paul 937ce4946daSBill Paul t = nge_devs; 938ce4946daSBill Paul 939ce4946daSBill Paul while (t->nge_name != NULL) { 940ce4946daSBill Paul if ((pci_get_vendor(dev) == t->nge_vid) && 941ce4946daSBill Paul (pci_get_device(dev) == t->nge_did)) { 942ce4946daSBill Paul device_set_desc(dev, t->nge_name); 9436b9907e7SWarner Losh return (BUS_PROBE_DEFAULT); 944ce4946daSBill Paul } 945ce4946daSBill Paul t++; 946ce4946daSBill Paul } 947ce4946daSBill Paul 948ce4946daSBill Paul return (ENXIO); 949ce4946daSBill Paul } 950ce4946daSBill Paul 951ce4946daSBill Paul /* 952ce4946daSBill Paul * Attach the interface. Allocate softc structures, do ifmedia 953ce4946daSBill Paul * setup and ethernet/BPF attach. 954ce4946daSBill Paul */ 955eaabec55SAlfred Perlstein static int 956284c81cbSPyun YongHyeon nge_attach(device_t dev) 957ce4946daSBill Paul { 958f6bc9430SPyun YongHyeon uint8_t eaddr[ETHER_ADDR_LEN]; 959f6bc9430SPyun YongHyeon uint16_t ea[ETHER_ADDR_LEN/2], ea_temp, reg; 960ce4946daSBill Paul struct nge_softc *sc; 961f6bc9430SPyun YongHyeon struct ifnet *ifp; 962f6bc9430SPyun YongHyeon int error, i, rid; 963ce4946daSBill Paul 964f6bc9430SPyun YongHyeon error = 0; 965ce4946daSBill Paul sc = device_get_softc(dev); 9666b9f5c94SGleb Smirnoff sc->nge_dev = dev; 967ce4946daSBill Paul 968ad6c618bSBill Paul NGE_LOCK_INIT(sc, device_get_nameunit(dev)); 969646abee6SJohn Baldwin callout_init_mtx(&sc->nge_stat_ch, &sc->nge_mtx, 0); 970646abee6SJohn Baldwin 971ce4946daSBill Paul /* 972ce4946daSBill Paul * Map control/status registers. 973ce4946daSBill Paul */ 974ce4946daSBill Paul pci_enable_busmaster(dev); 975ce4946daSBill Paul 976f6bc9430SPyun YongHyeon #ifdef NGE_USEIOSPACE 977f6bc9430SPyun YongHyeon sc->nge_res_type = SYS_RES_IOPORT; 978f6bc9430SPyun YongHyeon sc->nge_res_id = PCIR_BAR(0); 979f6bc9430SPyun YongHyeon #else 980f6bc9430SPyun YongHyeon sc->nge_res_type = SYS_RES_MEMORY; 981f6bc9430SPyun YongHyeon sc->nge_res_id = PCIR_BAR(1); 982f6bc9430SPyun YongHyeon #endif 983f6bc9430SPyun YongHyeon sc->nge_res = bus_alloc_resource_any(dev, sc->nge_res_type, 984f6bc9430SPyun YongHyeon &sc->nge_res_id, RF_ACTIVE); 985ce4946daSBill Paul 986ce4946daSBill Paul if (sc->nge_res == NULL) { 987f6bc9430SPyun YongHyeon if (sc->nge_res_type == SYS_RES_MEMORY) { 988f6bc9430SPyun YongHyeon sc->nge_res_type = SYS_RES_IOPORT; 989f6bc9430SPyun YongHyeon sc->nge_res_id = PCIR_BAR(0); 990f6bc9430SPyun YongHyeon } else { 991f6bc9430SPyun YongHyeon sc->nge_res_type = SYS_RES_MEMORY; 992f6bc9430SPyun YongHyeon sc->nge_res_id = PCIR_BAR(1); 993ce4946daSBill Paul } 994f6bc9430SPyun YongHyeon sc->nge_res = bus_alloc_resource_any(dev, sc->nge_res_type, 995f6bc9430SPyun YongHyeon &sc->nge_res_id, RF_ACTIVE); 996f6bc9430SPyun YongHyeon if (sc->nge_res == NULL) { 997f6bc9430SPyun YongHyeon device_printf(dev, "couldn't allocate %s resources\n", 998f6bc9430SPyun YongHyeon sc->nge_res_type == SYS_RES_MEMORY ? "memory" : 999f6bc9430SPyun YongHyeon "I/O"); 1000f6bc9430SPyun YongHyeon NGE_LOCK_DESTROY(sc); 1001f6bc9430SPyun YongHyeon return (ENXIO); 1002f6bc9430SPyun YongHyeon } 1003f6bc9430SPyun YongHyeon } 1004ce4946daSBill Paul 1005ce4946daSBill Paul /* Allocate interrupt */ 1006ce4946daSBill Paul rid = 0; 10075f96beb9SNate Lawson sc->nge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 1008ce4946daSBill Paul RF_SHAREABLE | RF_ACTIVE); 1009ce4946daSBill Paul 1010ce4946daSBill Paul if (sc->nge_irq == NULL) { 1011646abee6SJohn Baldwin device_printf(dev, "couldn't map interrupt\n"); 1012ce4946daSBill Paul error = ENXIO; 1013ce4946daSBill Paul goto fail; 1014ce4946daSBill Paul } 1015ce4946daSBill Paul 1016f6bc9430SPyun YongHyeon /* Enable MWI. */ 1017f6bc9430SPyun YongHyeon reg = pci_read_config(dev, PCIR_COMMAND, 2); 1018f6bc9430SPyun YongHyeon reg |= PCIM_CMD_MWRICEN; 1019f6bc9430SPyun YongHyeon pci_write_config(dev, PCIR_COMMAND, reg, 2); 1020f6bc9430SPyun YongHyeon 1021ce4946daSBill Paul /* Reset the adapter. */ 1022ce4946daSBill Paul nge_reset(sc); 1023ce4946daSBill Paul 1024ce4946daSBill Paul /* 1025ce4946daSBill Paul * Get station address from the EEPROM. 1026ce4946daSBill Paul */ 1027f6bc9430SPyun YongHyeon nge_read_eeprom(sc, (caddr_t)ea, NGE_EE_NODEADDR, 3); 1028f6bc9430SPyun YongHyeon for (i = 0; i < ETHER_ADDR_LEN / 2; i++) 1029f6bc9430SPyun YongHyeon ea[i] = le16toh(ea[i]); 1030f6bc9430SPyun YongHyeon ea_temp = ea[0]; 1031f6bc9430SPyun YongHyeon ea[0] = ea[2]; 1032f6bc9430SPyun YongHyeon ea[2] = ea_temp; 1033f6bc9430SPyun YongHyeon bcopy(ea, eaddr, sizeof(eaddr)); 1034ce4946daSBill Paul 1035f6bc9430SPyun YongHyeon if (nge_dma_alloc(sc) != 0) { 1036ce4946daSBill Paul error = ENXIO; 1037ce4946daSBill Paul goto fail; 1038ce4946daSBill Paul } 1039ce4946daSBill Paul 1040f6bc9430SPyun YongHyeon nge_sysctl_node(sc); 1041f6bc9430SPyun YongHyeon 1042fc74a9f9SBrooks Davis ifp = sc->nge_ifp = if_alloc(IFT_ETHER); 1043fc74a9f9SBrooks Davis if (ifp == NULL) { 1044f6bc9430SPyun YongHyeon device_printf(dev, "can not allocate ifnet structure\n"); 1045fc74a9f9SBrooks Davis error = ENOSPC; 1046fc74a9f9SBrooks Davis goto fail; 1047fc74a9f9SBrooks Davis } 1048ce4946daSBill Paul ifp->if_softc = sc; 10499bf40edeSBrooks Davis if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 1050ad6c618bSBill Paul ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 1051ce4946daSBill Paul ifp->if_ioctl = nge_ioctl; 1052ce4946daSBill Paul ifp->if_start = nge_start; 1053ce4946daSBill Paul ifp->if_init = nge_init; 1054f6bc9430SPyun YongHyeon ifp->if_snd.ifq_drv_maxlen = NGE_TX_RING_CNT - 1; 1055f6bc9430SPyun YongHyeon IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen); 1056f6bc9430SPyun YongHyeon IFQ_SET_READY(&ifp->if_snd); 1057ce4946daSBill Paul ifp->if_hwassist = NGE_CSUM_FEATURES; 1058f6bc9430SPyun YongHyeon ifp->if_capabilities = IFCAP_HWCSUM; 1059ce4946daSBill Paul /* 1060f6bc9430SPyun YongHyeon * It seems that some hardwares doesn't provide 3.3V auxiliary 1061f6bc9430SPyun YongHyeon * supply(3VAUX) to drive PME such that checking PCI power 1062f6bc9430SPyun YongHyeon * management capability is necessary. 1063ce4946daSBill Paul */ 10643b0a4aefSJohn Baldwin if (pci_find_cap(sc->nge_dev, PCIY_PMG, &i) == 0) 1065f6bc9430SPyun YongHyeon ifp->if_capabilities |= IFCAP_WOL; 1066f6bc9430SPyun YongHyeon ifp->if_capenable = ifp->if_capabilities; 1067f6bc9430SPyun YongHyeon 1068f6bc9430SPyun YongHyeon if ((CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) != 0) { 1069f6bc9430SPyun YongHyeon sc->nge_flags |= NGE_FLAG_TBI; 10701f548804SDoug Ambrisko device_printf(dev, "Using TBI\n"); 1071f6bc9430SPyun YongHyeon /* Configure GPIO. */ 10721f548804SDoug Ambrisko CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO) 10731f548804SDoug Ambrisko | NGE_GPIO_GP4_OUT 10741f548804SDoug Ambrisko | NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB 10751f548804SDoug Ambrisko | NGE_GPIO_GP3_OUTENB 10761f548804SDoug Ambrisko | NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN); 1077ce4946daSBill Paul } 1078f6bc9430SPyun YongHyeon 1079f6bc9430SPyun YongHyeon /* 1080f6bc9430SPyun YongHyeon * Do MII setup. 1081f6bc9430SPyun YongHyeon */ 1082d6c65d27SMarius Strobl error = mii_attach(dev, &sc->nge_miibus, ifp, nge_mediachange, 1083d6c65d27SMarius Strobl nge_mediastatus, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0); 1084f6bc9430SPyun YongHyeon if (error != 0) { 1085d6c65d27SMarius Strobl device_printf(dev, "attaching PHYs failed\n"); 1086f6bc9430SPyun YongHyeon goto fail; 10871f548804SDoug Ambrisko } 1088ce4946daSBill Paul 1089ce4946daSBill Paul /* 1090ce4946daSBill Paul * Call MI attach routine. 1091ce4946daSBill Paul */ 1092673d9191SSam Leffler ether_ifattach(ifp, eaddr); 1093ad6c618bSBill Paul 1094f6bc9430SPyun YongHyeon /* VLAN capability setup. */ 1095f6bc9430SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING; 1096f6bc9430SPyun YongHyeon ifp->if_capabilities |= IFCAP_VLAN_HWCSUM; 1097f6bc9430SPyun YongHyeon ifp->if_capenable = ifp->if_capabilities; 1098f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING 1099f6bc9430SPyun YongHyeon ifp->if_capabilities |= IFCAP_POLLING; 1100f6bc9430SPyun YongHyeon #endif 1101f6bc9430SPyun YongHyeon /* 1102f6bc9430SPyun YongHyeon * Tell the upper layer(s) we support long frames. 1103f6bc9430SPyun YongHyeon * Must appear after the call to ether_ifattach() because 1104f6bc9430SPyun YongHyeon * ether_ifattach() sets ifi_hdrlen to the default value. 1105f6bc9430SPyun YongHyeon */ 1106f6bc9430SPyun YongHyeon ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header); 1107f6bc9430SPyun YongHyeon 1108ad6c618bSBill Paul /* 1109ad6c618bSBill Paul * Hookup IRQ last. 1110ad6c618bSBill Paul */ 1111ad6c618bSBill Paul error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET | INTR_MPSAFE, 1112ef544f63SPaolo Pisati NULL, nge_intr, sc, &sc->nge_intrhand); 1113ad6c618bSBill Paul if (error) { 1114646abee6SJohn Baldwin device_printf(dev, "couldn't set up irq\n"); 1115646abee6SJohn Baldwin goto fail; 1116ad6c618bSBill Paul } 1117ce4946daSBill Paul 1118646abee6SJohn Baldwin fail: 1119f6bc9430SPyun YongHyeon if (error != 0) 1120f6bc9430SPyun YongHyeon nge_detach(dev); 1121ce4946daSBill Paul return (error); 1122ce4946daSBill Paul } 1123ce4946daSBill Paul 1124eaabec55SAlfred Perlstein static int 1125284c81cbSPyun YongHyeon nge_detach(device_t dev) 1126ce4946daSBill Paul { 1127ce4946daSBill Paul struct nge_softc *sc; 1128ce4946daSBill Paul struct ifnet *ifp; 1129ce4946daSBill Paul 1130ce4946daSBill Paul sc = device_get_softc(dev); 1131fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 1132ce4946daSBill Paul 113340929967SGleb Smirnoff #ifdef DEVICE_POLLING 1134f6bc9430SPyun YongHyeon if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING) 113540929967SGleb Smirnoff ether_poll_deregister(ifp); 113640929967SGleb Smirnoff #endif 1137f6bc9430SPyun YongHyeon 1138f6bc9430SPyun YongHyeon if (device_is_attached(dev)) { 1139ad6c618bSBill Paul NGE_LOCK(sc); 1140f6bc9430SPyun YongHyeon sc->nge_flags |= NGE_FLAG_DETACH; 1141ce4946daSBill Paul nge_stop(sc); 1142ad6c618bSBill Paul NGE_UNLOCK(sc); 1143646abee6SJohn Baldwin callout_drain(&sc->nge_stat_ch); 1144f6bc9430SPyun YongHyeon if (ifp != NULL) 1145673d9191SSam Leffler ether_ifdetach(ifp); 11461f548804SDoug Ambrisko } 1147ce4946daSBill Paul 1148f6bc9430SPyun YongHyeon if (sc->nge_miibus != NULL) { 1149f6bc9430SPyun YongHyeon device_delete_child(dev, sc->nge_miibus); 1150f6bc9430SPyun YongHyeon sc->nge_miibus = NULL; 1151f6bc9430SPyun YongHyeon } 1152f6bc9430SPyun YongHyeon bus_generic_detach(dev); 1153f6bc9430SPyun YongHyeon if (sc->nge_intrhand != NULL) 1154f6bc9430SPyun YongHyeon bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand); 1155f6bc9430SPyun YongHyeon if (sc->nge_irq != NULL) 1156f6bc9430SPyun YongHyeon bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq); 1157f6bc9430SPyun YongHyeon if (sc->nge_res != NULL) 1158f6bc9430SPyun YongHyeon bus_release_resource(dev, sc->nge_res_type, sc->nge_res_id, 1159f6bc9430SPyun YongHyeon sc->nge_res); 1160f6bc9430SPyun YongHyeon 1161f6bc9430SPyun YongHyeon nge_dma_free(sc); 1162f6bc9430SPyun YongHyeon if (ifp != NULL) 1163ad4f426eSWarner Losh if_free(ifp); 1164ce4946daSBill Paul 11656ba160b6SBill Paul NGE_LOCK_DESTROY(sc); 11666ba160b6SBill Paul 1167ce4946daSBill Paul return (0); 1168ce4946daSBill Paul } 1169ce4946daSBill Paul 1170f6bc9430SPyun YongHyeon struct nge_dmamap_arg { 1171f6bc9430SPyun YongHyeon bus_addr_t nge_busaddr; 1172f6bc9430SPyun YongHyeon }; 1173f6bc9430SPyun YongHyeon 1174f6bc9430SPyun YongHyeon static void 1175f6bc9430SPyun YongHyeon nge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1176f6bc9430SPyun YongHyeon { 1177f6bc9430SPyun YongHyeon struct nge_dmamap_arg *ctx; 1178f6bc9430SPyun YongHyeon 1179f6bc9430SPyun YongHyeon if (error != 0) 1180f6bc9430SPyun YongHyeon return; 1181f6bc9430SPyun YongHyeon ctx = arg; 1182f6bc9430SPyun YongHyeon ctx->nge_busaddr = segs[0].ds_addr; 1183f6bc9430SPyun YongHyeon } 1184f6bc9430SPyun YongHyeon 1185f6bc9430SPyun YongHyeon static int 1186f6bc9430SPyun YongHyeon nge_dma_alloc(struct nge_softc *sc) 1187f6bc9430SPyun YongHyeon { 1188f6bc9430SPyun YongHyeon struct nge_dmamap_arg ctx; 1189f6bc9430SPyun YongHyeon struct nge_txdesc *txd; 1190f6bc9430SPyun YongHyeon struct nge_rxdesc *rxd; 1191f6bc9430SPyun YongHyeon int error, i; 1192f6bc9430SPyun YongHyeon 1193f6bc9430SPyun YongHyeon /* Create parent DMA tag. */ 1194f6bc9430SPyun YongHyeon error = bus_dma_tag_create( 1195f6bc9430SPyun YongHyeon bus_get_dma_tag(sc->nge_dev), /* parent */ 1196f6bc9430SPyun YongHyeon 1, 0, /* alignment, boundary */ 1197f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR_32BIT, /* lowaddr */ 1198f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1199f6bc9430SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1200f6bc9430SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsize */ 1201f6bc9430SPyun YongHyeon 0, /* nsegments */ 1202f6bc9430SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */ 1203f6bc9430SPyun YongHyeon 0, /* flags */ 1204f6bc9430SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1205f6bc9430SPyun YongHyeon &sc->nge_cdata.nge_parent_tag); 1206f6bc9430SPyun YongHyeon if (error != 0) { 1207f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, "failed to create parent DMA tag\n"); 1208f6bc9430SPyun YongHyeon goto fail; 1209f6bc9430SPyun YongHyeon } 1210f6bc9430SPyun YongHyeon /* Create tag for Tx ring. */ 1211f6bc9430SPyun YongHyeon error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */ 1212f6bc9430SPyun YongHyeon NGE_RING_ALIGN, 0, /* alignment, boundary */ 1213f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */ 1214f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1215f6bc9430SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1216f6bc9430SPyun YongHyeon NGE_TX_RING_SIZE, /* maxsize */ 1217f6bc9430SPyun YongHyeon 1, /* nsegments */ 1218f6bc9430SPyun YongHyeon NGE_TX_RING_SIZE, /* maxsegsize */ 1219f6bc9430SPyun YongHyeon 0, /* flags */ 1220f6bc9430SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1221f6bc9430SPyun YongHyeon &sc->nge_cdata.nge_tx_ring_tag); 1222f6bc9430SPyun YongHyeon if (error != 0) { 1223f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, "failed to create Tx ring DMA tag\n"); 1224f6bc9430SPyun YongHyeon goto fail; 1225f6bc9430SPyun YongHyeon } 1226f6bc9430SPyun YongHyeon 1227f6bc9430SPyun YongHyeon /* Create tag for Rx ring. */ 1228f6bc9430SPyun YongHyeon error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */ 1229f6bc9430SPyun YongHyeon NGE_RING_ALIGN, 0, /* alignment, boundary */ 1230f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */ 1231f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1232f6bc9430SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1233f6bc9430SPyun YongHyeon NGE_RX_RING_SIZE, /* maxsize */ 1234f6bc9430SPyun YongHyeon 1, /* nsegments */ 1235f6bc9430SPyun YongHyeon NGE_RX_RING_SIZE, /* maxsegsize */ 1236f6bc9430SPyun YongHyeon 0, /* flags */ 1237f6bc9430SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1238f6bc9430SPyun YongHyeon &sc->nge_cdata.nge_rx_ring_tag); 1239f6bc9430SPyun YongHyeon if (error != 0) { 1240f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 1241f6bc9430SPyun YongHyeon "failed to create Rx ring DMA tag\n"); 1242f6bc9430SPyun YongHyeon goto fail; 1243f6bc9430SPyun YongHyeon } 1244f6bc9430SPyun YongHyeon 1245f6bc9430SPyun YongHyeon /* Create tag for Tx buffers. */ 1246f6bc9430SPyun YongHyeon error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */ 1247f6bc9430SPyun YongHyeon 1, 0, /* alignment, boundary */ 1248f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */ 1249f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1250f6bc9430SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1251f6bc9430SPyun YongHyeon MCLBYTES * NGE_MAXTXSEGS, /* maxsize */ 1252f6bc9430SPyun YongHyeon NGE_MAXTXSEGS, /* nsegments */ 1253f6bc9430SPyun YongHyeon MCLBYTES, /* maxsegsize */ 1254f6bc9430SPyun YongHyeon 0, /* flags */ 1255f6bc9430SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1256f6bc9430SPyun YongHyeon &sc->nge_cdata.nge_tx_tag); 1257f6bc9430SPyun YongHyeon if (error != 0) { 1258f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, "failed to create Tx DMA tag\n"); 1259f6bc9430SPyun YongHyeon goto fail; 1260f6bc9430SPyun YongHyeon } 1261f6bc9430SPyun YongHyeon 1262f6bc9430SPyun YongHyeon /* Create tag for Rx buffers. */ 1263f6bc9430SPyun YongHyeon error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */ 1264f6bc9430SPyun YongHyeon NGE_RX_ALIGN, 0, /* alignment, boundary */ 1265f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */ 1266f6bc9430SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */ 1267f6bc9430SPyun YongHyeon NULL, NULL, /* filter, filterarg */ 1268f6bc9430SPyun YongHyeon MCLBYTES, /* maxsize */ 1269f6bc9430SPyun YongHyeon 1, /* nsegments */ 1270f6bc9430SPyun YongHyeon MCLBYTES, /* maxsegsize */ 1271f6bc9430SPyun YongHyeon 0, /* flags */ 1272f6bc9430SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */ 1273f6bc9430SPyun YongHyeon &sc->nge_cdata.nge_rx_tag); 1274f6bc9430SPyun YongHyeon if (error != 0) { 1275f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, "failed to create Rx DMA tag\n"); 1276f6bc9430SPyun YongHyeon goto fail; 1277f6bc9430SPyun YongHyeon } 1278f6bc9430SPyun YongHyeon 1279f6bc9430SPyun YongHyeon /* Allocate DMA'able memory and load the DMA map for Tx ring. */ 1280f6bc9430SPyun YongHyeon error = bus_dmamem_alloc(sc->nge_cdata.nge_tx_ring_tag, 1281f6bc9430SPyun YongHyeon (void **)&sc->nge_rdata.nge_tx_ring, BUS_DMA_WAITOK | 1282f6bc9430SPyun YongHyeon BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->nge_cdata.nge_tx_ring_map); 1283f6bc9430SPyun YongHyeon if (error != 0) { 1284f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 1285f6bc9430SPyun YongHyeon "failed to allocate DMA'able memory for Tx ring\n"); 1286f6bc9430SPyun YongHyeon goto fail; 1287f6bc9430SPyun YongHyeon } 1288f6bc9430SPyun YongHyeon 1289f6bc9430SPyun YongHyeon ctx.nge_busaddr = 0; 1290f6bc9430SPyun YongHyeon error = bus_dmamap_load(sc->nge_cdata.nge_tx_ring_tag, 1291f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map, sc->nge_rdata.nge_tx_ring, 1292f6bc9430SPyun YongHyeon NGE_TX_RING_SIZE, nge_dmamap_cb, &ctx, 0); 1293f6bc9430SPyun YongHyeon if (error != 0 || ctx.nge_busaddr == 0) { 1294f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 1295f6bc9430SPyun YongHyeon "failed to load DMA'able memory for Tx ring\n"); 1296f6bc9430SPyun YongHyeon goto fail; 1297f6bc9430SPyun YongHyeon } 1298f6bc9430SPyun YongHyeon sc->nge_rdata.nge_tx_ring_paddr = ctx.nge_busaddr; 1299f6bc9430SPyun YongHyeon 1300f6bc9430SPyun YongHyeon /* Allocate DMA'able memory and load the DMA map for Rx ring. */ 1301f6bc9430SPyun YongHyeon error = bus_dmamem_alloc(sc->nge_cdata.nge_rx_ring_tag, 1302f6bc9430SPyun YongHyeon (void **)&sc->nge_rdata.nge_rx_ring, BUS_DMA_WAITOK | 1303f6bc9430SPyun YongHyeon BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->nge_cdata.nge_rx_ring_map); 1304f6bc9430SPyun YongHyeon if (error != 0) { 1305f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 1306f6bc9430SPyun YongHyeon "failed to allocate DMA'able memory for Rx ring\n"); 1307f6bc9430SPyun YongHyeon goto fail; 1308f6bc9430SPyun YongHyeon } 1309f6bc9430SPyun YongHyeon 1310f6bc9430SPyun YongHyeon ctx.nge_busaddr = 0; 1311f6bc9430SPyun YongHyeon error = bus_dmamap_load(sc->nge_cdata.nge_rx_ring_tag, 1312f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map, sc->nge_rdata.nge_rx_ring, 1313f6bc9430SPyun YongHyeon NGE_RX_RING_SIZE, nge_dmamap_cb, &ctx, 0); 1314f6bc9430SPyun YongHyeon if (error != 0 || ctx.nge_busaddr == 0) { 1315f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 1316f6bc9430SPyun YongHyeon "failed to load DMA'able memory for Rx ring\n"); 1317f6bc9430SPyun YongHyeon goto fail; 1318f6bc9430SPyun YongHyeon } 1319f6bc9430SPyun YongHyeon sc->nge_rdata.nge_rx_ring_paddr = ctx.nge_busaddr; 1320f6bc9430SPyun YongHyeon 1321f6bc9430SPyun YongHyeon /* Create DMA maps for Tx buffers. */ 1322f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TX_RING_CNT; i++) { 1323f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[i]; 1324f6bc9430SPyun YongHyeon txd->tx_m = NULL; 1325f6bc9430SPyun YongHyeon txd->tx_dmamap = NULL; 1326f6bc9430SPyun YongHyeon error = bus_dmamap_create(sc->nge_cdata.nge_tx_tag, 0, 1327f6bc9430SPyun YongHyeon &txd->tx_dmamap); 1328f6bc9430SPyun YongHyeon if (error != 0) { 1329f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 1330f6bc9430SPyun YongHyeon "failed to create Tx dmamap\n"); 1331f6bc9430SPyun YongHyeon goto fail; 1332f6bc9430SPyun YongHyeon } 1333f6bc9430SPyun YongHyeon } 1334f6bc9430SPyun YongHyeon /* Create DMA maps for Rx buffers. */ 1335f6bc9430SPyun YongHyeon if ((error = bus_dmamap_create(sc->nge_cdata.nge_rx_tag, 0, 1336f6bc9430SPyun YongHyeon &sc->nge_cdata.nge_rx_sparemap)) != 0) { 1337f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 1338f6bc9430SPyun YongHyeon "failed to create spare Rx dmamap\n"); 1339f6bc9430SPyun YongHyeon goto fail; 1340f6bc9430SPyun YongHyeon } 1341f6bc9430SPyun YongHyeon for (i = 0; i < NGE_RX_RING_CNT; i++) { 1342f6bc9430SPyun YongHyeon rxd = &sc->nge_cdata.nge_rxdesc[i]; 1343f6bc9430SPyun YongHyeon rxd->rx_m = NULL; 1344f6bc9430SPyun YongHyeon rxd->rx_dmamap = NULL; 1345f6bc9430SPyun YongHyeon error = bus_dmamap_create(sc->nge_cdata.nge_rx_tag, 0, 1346f6bc9430SPyun YongHyeon &rxd->rx_dmamap); 1347f6bc9430SPyun YongHyeon if (error != 0) { 1348f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 1349f6bc9430SPyun YongHyeon "failed to create Rx dmamap\n"); 1350f6bc9430SPyun YongHyeon goto fail; 1351f6bc9430SPyun YongHyeon } 1352f6bc9430SPyun YongHyeon } 1353f6bc9430SPyun YongHyeon 1354f6bc9430SPyun YongHyeon fail: 1355f6bc9430SPyun YongHyeon return (error); 1356f6bc9430SPyun YongHyeon } 1357f6bc9430SPyun YongHyeon 1358f6bc9430SPyun YongHyeon static void 1359f6bc9430SPyun YongHyeon nge_dma_free(struct nge_softc *sc) 1360f6bc9430SPyun YongHyeon { 1361f6bc9430SPyun YongHyeon struct nge_txdesc *txd; 1362f6bc9430SPyun YongHyeon struct nge_rxdesc *rxd; 1363f6bc9430SPyun YongHyeon int i; 1364f6bc9430SPyun YongHyeon 1365f6bc9430SPyun YongHyeon /* Tx ring. */ 1366f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_tx_ring_tag) { 1367f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_tx_ring_map) 1368f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_tx_ring_tag, 1369f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map); 1370f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_tx_ring_map && 1371f6bc9430SPyun YongHyeon sc->nge_rdata.nge_tx_ring) 1372f6bc9430SPyun YongHyeon bus_dmamem_free(sc->nge_cdata.nge_tx_ring_tag, 1373f6bc9430SPyun YongHyeon sc->nge_rdata.nge_tx_ring, 1374f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map); 1375f6bc9430SPyun YongHyeon sc->nge_rdata.nge_tx_ring = NULL; 1376f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map = NULL; 1377f6bc9430SPyun YongHyeon bus_dma_tag_destroy(sc->nge_cdata.nge_tx_ring_tag); 1378f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_tag = NULL; 1379f6bc9430SPyun YongHyeon } 1380f6bc9430SPyun YongHyeon /* Rx ring. */ 1381f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_rx_ring_tag) { 1382f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_rx_ring_map) 1383f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_rx_ring_tag, 1384f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map); 1385f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_rx_ring_map && 1386f6bc9430SPyun YongHyeon sc->nge_rdata.nge_rx_ring) 1387f6bc9430SPyun YongHyeon bus_dmamem_free(sc->nge_cdata.nge_rx_ring_tag, 1388f6bc9430SPyun YongHyeon sc->nge_rdata.nge_rx_ring, 1389f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map); 1390f6bc9430SPyun YongHyeon sc->nge_rdata.nge_rx_ring = NULL; 1391f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map = NULL; 1392f6bc9430SPyun YongHyeon bus_dma_tag_destroy(sc->nge_cdata.nge_rx_ring_tag); 1393f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_tag = NULL; 1394f6bc9430SPyun YongHyeon } 1395f6bc9430SPyun YongHyeon /* Tx buffers. */ 1396f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_tx_tag) { 1397f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TX_RING_CNT; i++) { 1398f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[i]; 1399f6bc9430SPyun YongHyeon if (txd->tx_dmamap) { 1400f6bc9430SPyun YongHyeon bus_dmamap_destroy(sc->nge_cdata.nge_tx_tag, 1401f6bc9430SPyun YongHyeon txd->tx_dmamap); 1402f6bc9430SPyun YongHyeon txd->tx_dmamap = NULL; 1403f6bc9430SPyun YongHyeon } 1404f6bc9430SPyun YongHyeon } 1405f6bc9430SPyun YongHyeon bus_dma_tag_destroy(sc->nge_cdata.nge_tx_tag); 1406f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_tag = NULL; 1407f6bc9430SPyun YongHyeon } 1408f6bc9430SPyun YongHyeon /* Rx buffers. */ 1409f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_rx_tag) { 1410f6bc9430SPyun YongHyeon for (i = 0; i < NGE_RX_RING_CNT; i++) { 1411f6bc9430SPyun YongHyeon rxd = &sc->nge_cdata.nge_rxdesc[i]; 1412f6bc9430SPyun YongHyeon if (rxd->rx_dmamap) { 1413f6bc9430SPyun YongHyeon bus_dmamap_destroy(sc->nge_cdata.nge_rx_tag, 1414f6bc9430SPyun YongHyeon rxd->rx_dmamap); 1415f6bc9430SPyun YongHyeon rxd->rx_dmamap = NULL; 1416f6bc9430SPyun YongHyeon } 1417f6bc9430SPyun YongHyeon } 1418f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_rx_sparemap) { 1419f6bc9430SPyun YongHyeon bus_dmamap_destroy(sc->nge_cdata.nge_rx_tag, 1420f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_sparemap); 1421f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_sparemap = 0; 1422f6bc9430SPyun YongHyeon } 1423f6bc9430SPyun YongHyeon bus_dma_tag_destroy(sc->nge_cdata.nge_rx_tag); 1424f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_tag = NULL; 1425f6bc9430SPyun YongHyeon } 1426f6bc9430SPyun YongHyeon 1427f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_parent_tag) { 1428f6bc9430SPyun YongHyeon bus_dma_tag_destroy(sc->nge_cdata.nge_parent_tag); 1429f6bc9430SPyun YongHyeon sc->nge_cdata.nge_parent_tag = NULL; 1430f6bc9430SPyun YongHyeon } 1431f6bc9430SPyun YongHyeon } 1432f6bc9430SPyun YongHyeon 1433ce4946daSBill Paul /* 1434ce4946daSBill Paul * Initialize the transmit descriptors. 1435ce4946daSBill Paul */ 1436eaabec55SAlfred Perlstein static int 1437284c81cbSPyun YongHyeon nge_list_tx_init(struct nge_softc *sc) 1438ce4946daSBill Paul { 1439f6bc9430SPyun YongHyeon struct nge_ring_data *rd; 1440f6bc9430SPyun YongHyeon struct nge_txdesc *txd; 1441f6bc9430SPyun YongHyeon bus_addr_t addr; 1442ce4946daSBill Paul int i; 1443ce4946daSBill Paul 1444f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_prod = 0; 1445f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_cons = 0; 1446f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_cnt = 0; 1447ce4946daSBill Paul 1448f6bc9430SPyun YongHyeon rd = &sc->nge_rdata; 1449f6bc9430SPyun YongHyeon bzero(rd->nge_tx_ring, sizeof(struct nge_desc) * NGE_TX_RING_CNT); 1450f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TX_RING_CNT; i++) { 1451f6bc9430SPyun YongHyeon if (i == NGE_TX_RING_CNT - 1) 1452f6bc9430SPyun YongHyeon addr = NGE_TX_RING_ADDR(sc, 0); 1453f6bc9430SPyun YongHyeon else 1454f6bc9430SPyun YongHyeon addr = NGE_TX_RING_ADDR(sc, i + 1); 1455f6bc9430SPyun YongHyeon rd->nge_tx_ring[i].nge_next = htole32(NGE_ADDR_LO(addr)); 1456f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[i]; 1457f6bc9430SPyun YongHyeon txd->tx_m = NULL; 1458ce4946daSBill Paul } 1459ce4946daSBill Paul 1460f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag, 1461f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map, 1462f6bc9430SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1463ce4946daSBill Paul 1464ce4946daSBill Paul return (0); 1465ce4946daSBill Paul } 1466ce4946daSBill Paul 1467ce4946daSBill Paul /* 1468ce4946daSBill Paul * Initialize the RX descriptors and allocate mbufs for them. Note that 1469ce4946daSBill Paul * we arrange the descriptors in a closed ring, so that the last descriptor 1470ce4946daSBill Paul * points back to the first. 1471ce4946daSBill Paul */ 1472eaabec55SAlfred Perlstein static int 1473284c81cbSPyun YongHyeon nge_list_rx_init(struct nge_softc *sc) 1474ce4946daSBill Paul { 1475f6bc9430SPyun YongHyeon struct nge_ring_data *rd; 1476f6bc9430SPyun YongHyeon bus_addr_t addr; 1477ce4946daSBill Paul int i; 1478ce4946daSBill Paul 1479f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_cons = 0; 1480ad6c618bSBill Paul sc->nge_head = sc->nge_tail = NULL; 1481ce4946daSBill Paul 1482f6bc9430SPyun YongHyeon rd = &sc->nge_rdata; 1483f6bc9430SPyun YongHyeon bzero(rd->nge_rx_ring, sizeof(struct nge_desc) * NGE_RX_RING_CNT); 1484f6bc9430SPyun YongHyeon for (i = 0; i < NGE_RX_RING_CNT; i++) { 1485f6bc9430SPyun YongHyeon if (nge_newbuf(sc, i) != 0) 1486f6bc9430SPyun YongHyeon return (ENOBUFS); 1487f6bc9430SPyun YongHyeon if (i == NGE_RX_RING_CNT - 1) 1488f6bc9430SPyun YongHyeon addr = NGE_RX_RING_ADDR(sc, 0); 1489f6bc9430SPyun YongHyeon else 1490f6bc9430SPyun YongHyeon addr = NGE_RX_RING_ADDR(sc, i + 1); 1491f6bc9430SPyun YongHyeon rd->nge_rx_ring[i].nge_next = htole32(NGE_ADDR_LO(addr)); 1492f6bc9430SPyun YongHyeon } 1493f6bc9430SPyun YongHyeon 1494f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag, 1495f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map, 1496f6bc9430SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1497f6bc9430SPyun YongHyeon 1498ce4946daSBill Paul return (0); 1499ce4946daSBill Paul } 1500ce4946daSBill Paul 1501f6bc9430SPyun YongHyeon static __inline void 1502f6bc9430SPyun YongHyeon nge_discard_rxbuf(struct nge_softc *sc, int idx) 1503f6bc9430SPyun YongHyeon { 1504f6bc9430SPyun YongHyeon struct nge_desc *desc; 1505f6bc9430SPyun YongHyeon 1506f6bc9430SPyun YongHyeon desc = &sc->nge_rdata.nge_rx_ring[idx]; 1507f6bc9430SPyun YongHyeon desc->nge_cmdsts = htole32(MCLBYTES - sizeof(uint64_t)); 1508f6bc9430SPyun YongHyeon desc->nge_extsts = 0; 1509f6bc9430SPyun YongHyeon } 1510f6bc9430SPyun YongHyeon 1511ce4946daSBill Paul /* 1512ce4946daSBill Paul * Initialize an RX descriptor and attach an MBUF cluster. 1513ce4946daSBill Paul */ 1514eaabec55SAlfred Perlstein static int 1515f6bc9430SPyun YongHyeon nge_newbuf(struct nge_softc *sc, int idx) 1516ce4946daSBill Paul { 1517f6bc9430SPyun YongHyeon struct nge_desc *desc; 1518f6bc9430SPyun YongHyeon struct nge_rxdesc *rxd; 1519f6bc9430SPyun YongHyeon struct mbuf *m; 1520f6bc9430SPyun YongHyeon bus_dma_segment_t segs[1]; 1521f6bc9430SPyun YongHyeon bus_dmamap_t map; 1522f6bc9430SPyun YongHyeon int nsegs; 1523ce4946daSBill Paul 15246c772336SSam Leffler m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 15256c772336SSam Leffler if (m == NULL) 1526ce4946daSBill Paul return (ENOBUFS); 1527ad6c618bSBill Paul m->m_len = m->m_pkthdr.len = MCLBYTES; 15283929ff51SPyun YongHyeon m_adj(m, sizeof(uint64_t)); 1529ce4946daSBill Paul 1530f6bc9430SPyun YongHyeon if (bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_rx_tag, 1531f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_sparemap, m, segs, &nsegs, 0) != 0) { 1532f6bc9430SPyun YongHyeon m_freem(m); 1533f6bc9430SPyun YongHyeon return (ENOBUFS); 1534f6bc9430SPyun YongHyeon } 1535f6bc9430SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs)); 1536f6bc9430SPyun YongHyeon 1537f6bc9430SPyun YongHyeon rxd = &sc->nge_cdata.nge_rxdesc[idx]; 1538f6bc9430SPyun YongHyeon if (rxd->rx_m != NULL) { 1539f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap, 1540f6bc9430SPyun YongHyeon BUS_DMASYNC_POSTREAD); 1541f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap); 1542f6bc9430SPyun YongHyeon } 1543f6bc9430SPyun YongHyeon map = rxd->rx_dmamap; 1544f6bc9430SPyun YongHyeon rxd->rx_dmamap = sc->nge_cdata.nge_rx_sparemap; 1545f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_sparemap = map; 1546f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap, 1547f6bc9430SPyun YongHyeon BUS_DMASYNC_PREREAD); 1548f6bc9430SPyun YongHyeon rxd->rx_m = m; 1549f6bc9430SPyun YongHyeon desc = &sc->nge_rdata.nge_rx_ring[idx]; 1550f6bc9430SPyun YongHyeon desc->nge_ptr = htole32(NGE_ADDR_LO(segs[0].ds_addr)); 1551f6bc9430SPyun YongHyeon desc->nge_cmdsts = htole32(segs[0].ds_len); 1552f6bc9430SPyun YongHyeon desc->nge_extsts = 0; 1553ce4946daSBill Paul 1554ce4946daSBill Paul return (0); 1555ce4946daSBill Paul } 1556ce4946daSBill Paul 1557f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT 1558ad6c618bSBill Paul static __inline void 1559284c81cbSPyun YongHyeon nge_fixup_rx(struct mbuf *m) 1560ce4946daSBill Paul { 1561ce4946daSBill Paul int i; 1562ad6c618bSBill Paul uint16_t *src, *dst; 1563ce4946daSBill Paul 1564ad6c618bSBill Paul src = mtod(m, uint16_t *); 1565ad6c618bSBill Paul dst = src - 1; 1566ce4946daSBill Paul 1567ad6c618bSBill Paul for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++) 1568ad6c618bSBill Paul *dst++ = *src++; 1569ce4946daSBill Paul 1570ad6c618bSBill Paul m->m_data -= ETHER_ALIGN; 1571ce4946daSBill Paul } 1572ad6c618bSBill Paul #endif 1573ad6c618bSBill Paul 1574ce4946daSBill Paul /* 1575ce4946daSBill Paul * A frame has been uploaded: pass the resulting mbuf chain up to 1576ce4946daSBill Paul * the higher level protocols. 1577ce4946daSBill Paul */ 157856e13f2aSAttilio Rao static int 1579284c81cbSPyun YongHyeon nge_rxeof(struct nge_softc *sc) 1580ce4946daSBill Paul { 1581ce4946daSBill Paul struct mbuf *m; 1582ce4946daSBill Paul struct ifnet *ifp; 1583ce4946daSBill Paul struct nge_desc *cur_rx; 1584f6bc9430SPyun YongHyeon struct nge_rxdesc *rxd; 158556e13f2aSAttilio Rao int cons, prog, rx_npkts, total_len; 1586f6bc9430SPyun YongHyeon uint32_t cmdsts, extsts; 1587ce4946daSBill Paul 1588ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 1589f6bc9430SPyun YongHyeon 1590fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 1591f6bc9430SPyun YongHyeon cons = sc->nge_cdata.nge_rx_cons; 159256e13f2aSAttilio Rao rx_npkts = 0; 1593ce4946daSBill Paul 1594f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag, 1595f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map, 1596f6bc9430SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1597ce4946daSBill Paul 1598f6bc9430SPyun YongHyeon for (prog = 0; prog < NGE_RX_RING_CNT && 1599f6bc9430SPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0; 1600f6bc9430SPyun YongHyeon NGE_INC(cons, NGE_RX_RING_CNT)) { 1601196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING 160240929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) { 1603196c0df6SHidetoshi Shimokawa if (sc->rxcycles <= 0) 1604196c0df6SHidetoshi Shimokawa break; 1605196c0df6SHidetoshi Shimokawa sc->rxcycles--; 1606196c0df6SHidetoshi Shimokawa } 160740929967SGleb Smirnoff #endif 1608f6bc9430SPyun YongHyeon cur_rx = &sc->nge_rdata.nge_rx_ring[cons]; 1609f6bc9430SPyun YongHyeon cmdsts = le32toh(cur_rx->nge_cmdsts); 1610f6bc9430SPyun YongHyeon extsts = le32toh(cur_rx->nge_extsts); 1611f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_OWN) == 0) 1612f6bc9430SPyun YongHyeon break; 1613f6bc9430SPyun YongHyeon prog++; 1614f6bc9430SPyun YongHyeon rxd = &sc->nge_cdata.nge_rxdesc[cons]; 1615f6bc9430SPyun YongHyeon m = rxd->rx_m; 1616f6bc9430SPyun YongHyeon total_len = cmdsts & NGE_CMDSTS_BUFLEN; 1617196c0df6SHidetoshi Shimokawa 1618f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_MORE) != 0) { 1619f6bc9430SPyun YongHyeon if (nge_newbuf(sc, cons) != 0) { 1620f6bc9430SPyun YongHyeon ifp->if_iqdrops++; 1621f6bc9430SPyun YongHyeon if (sc->nge_head != NULL) { 1622f6bc9430SPyun YongHyeon m_freem(sc->nge_head); 1623f6bc9430SPyun YongHyeon sc->nge_head = sc->nge_tail = NULL; 1624f6bc9430SPyun YongHyeon } 1625f6bc9430SPyun YongHyeon nge_discard_rxbuf(sc, cons); 1626f6bc9430SPyun YongHyeon continue; 1627f6bc9430SPyun YongHyeon } 1628ad6c618bSBill Paul m->m_len = total_len; 1629ad6c618bSBill Paul if (sc->nge_head == NULL) { 1630ad6c618bSBill Paul m->m_pkthdr.len = total_len; 1631ad6c618bSBill Paul sc->nge_head = sc->nge_tail = m; 1632ad6c618bSBill Paul } else { 1633ad6c618bSBill Paul m->m_flags &= ~M_PKTHDR; 1634ad6c618bSBill Paul sc->nge_head->m_pkthdr.len += total_len; 1635ad6c618bSBill Paul sc->nge_tail->m_next = m; 1636ad6c618bSBill Paul sc->nge_tail = m; 1637ad6c618bSBill Paul } 1638ad6c618bSBill Paul continue; 1639ad6c618bSBill Paul } 1640ad6c618bSBill Paul 1641ce4946daSBill Paul /* 1642ce4946daSBill Paul * If an error occurs, update stats, clear the 1643ce4946daSBill Paul * status word and leave the mbuf cluster in place: 1644ce4946daSBill Paul * it should simply get re-used next time this descriptor 1645ce4946daSBill Paul * comes up in the ring. 1646ce4946daSBill Paul */ 1647f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) { 1648f6bc9430SPyun YongHyeon if ((cmdsts & NGE_RXSTAT_RUNT) && 1649f6bc9430SPyun YongHyeon total_len >= (ETHER_MIN_LEN - ETHER_CRC_LEN - 4)) { 1650f6bc9430SPyun YongHyeon /* 1651f6bc9430SPyun YongHyeon * Work-around hardware bug, accept runt frames 1652f6bc9430SPyun YongHyeon * if its length is larger than or equal to 56. 1653f6bc9430SPyun YongHyeon */ 1654f6bc9430SPyun YongHyeon } else { 1655f6bc9430SPyun YongHyeon /* 1656f6bc9430SPyun YongHyeon * Input error counters are updated by hardware. 1657f6bc9430SPyun YongHyeon */ 1658ad6c618bSBill Paul if (sc->nge_head != NULL) { 1659ad6c618bSBill Paul m_freem(sc->nge_head); 1660ad6c618bSBill Paul sc->nge_head = sc->nge_tail = NULL; 1661ad6c618bSBill Paul } 1662f6bc9430SPyun YongHyeon nge_discard_rxbuf(sc, cons); 1663ce4946daSBill Paul continue; 1664ce4946daSBill Paul } 1665f6bc9430SPyun YongHyeon } 1666ce4946daSBill Paul 1667ad6c618bSBill Paul /* Try conjure up a replacement mbuf. */ 1668ad6c618bSBill Paul 1669f6bc9430SPyun YongHyeon if (nge_newbuf(sc, cons) != 0) { 1670f6bc9430SPyun YongHyeon ifp->if_iqdrops++; 1671ad6c618bSBill Paul if (sc->nge_head != NULL) { 1672ad6c618bSBill Paul m_freem(sc->nge_head); 1673ad6c618bSBill Paul sc->nge_head = sc->nge_tail = NULL; 1674ad6c618bSBill Paul } 1675f6bc9430SPyun YongHyeon nge_discard_rxbuf(sc, cons); 1676ad6c618bSBill Paul continue; 1677ad6c618bSBill Paul } 1678ad6c618bSBill Paul 1679f6bc9430SPyun YongHyeon /* Chain received mbufs. */ 1680ad6c618bSBill Paul if (sc->nge_head != NULL) { 1681ad6c618bSBill Paul m->m_len = total_len; 1682ad6c618bSBill Paul m->m_flags &= ~M_PKTHDR; 1683ad6c618bSBill Paul sc->nge_tail->m_next = m; 1684ad6c618bSBill Paul m = sc->nge_head; 1685ad6c618bSBill Paul m->m_pkthdr.len += total_len; 1686ad6c618bSBill Paul sc->nge_head = sc->nge_tail = NULL; 1687ad6c618bSBill Paul } else 1688ad6c618bSBill Paul m->m_pkthdr.len = m->m_len = total_len; 1689ad6c618bSBill Paul 1690ce4946daSBill Paul /* 1691ce4946daSBill Paul * Ok. NatSemi really screwed up here. This is the 1692ce4946daSBill Paul * only gigE chip I know of with alignment constraints 1693ce4946daSBill Paul * on receive buffers. RX buffers must be 64-bit aligned. 1694ce4946daSBill Paul */ 1695962315f6SBill Paul /* 1696962315f6SBill Paul * By popular demand, ignore the alignment problems 1697f6bc9430SPyun YongHyeon * on the non-strict alignment platform. The performance hit 1698962315f6SBill Paul * incurred due to unaligned accesses is much smaller 1699962315f6SBill Paul * than the hit produced by forcing buffer copies all 1700962315f6SBill Paul * the time, especially with jumbo frames. We still 1701962315f6SBill Paul * need to fix up the alignment everywhere else though. 1702962315f6SBill Paul */ 1703f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT 1704ad6c618bSBill Paul nge_fixup_rx(m); 1705962315f6SBill Paul #endif 1706ad6c618bSBill Paul m->m_pkthdr.rcvif = ifp; 1707f6bc9430SPyun YongHyeon ifp->if_ipackets++; 1708ce4946daSBill Paul 1709f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) { 1710ce4946daSBill Paul /* Do IP checksum checking. */ 1711f6bc9430SPyun YongHyeon if ((extsts & NGE_RXEXTSTS_IPPKT) != 0) 1712ce4946daSBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1713f6bc9430SPyun YongHyeon if ((extsts & NGE_RXEXTSTS_IPCSUMERR) == 0) 171401702579SBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 17152195de46SBill Paul if ((extsts & NGE_RXEXTSTS_TCPPKT && 17162195de46SBill Paul !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) || 17172195de46SBill Paul (extsts & NGE_RXEXTSTS_UDPPKT && 17182195de46SBill Paul !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) { 17192195de46SBill Paul m->m_pkthdr.csum_flags |= 17202195de46SBill Paul CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1721c9215605SBill Paul m->m_pkthdr.csum_data = 0xffff; 17222195de46SBill Paul } 1723f6bc9430SPyun YongHyeon } 1724ce4946daSBill Paul 1725ce4946daSBill Paul /* 1726ce4946daSBill Paul * If we received a packet with a vlan tag, pass it 1727ce4946daSBill Paul * to vlan_input() instead of ether_input(). 1728ce4946daSBill Paul */ 1729f6bc9430SPyun YongHyeon if ((extsts & NGE_RXEXTSTS_VLANPKT) != 0 && 1730f6bc9430SPyun YongHyeon (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) { 173178ba57b9SAndre Oppermann m->m_pkthdr.ether_vtag = 1732f6bc9430SPyun YongHyeon bswap16(extsts & NGE_RXEXTSTS_VTCI); 173378ba57b9SAndre Oppermann m->m_flags |= M_VLANTAG; 1734ce4946daSBill Paul } 1735ad6c618bSBill Paul NGE_UNLOCK(sc); 1736673d9191SSam Leffler (*ifp->if_input)(ifp, m); 1737ad6c618bSBill Paul NGE_LOCK(sc); 173856e13f2aSAttilio Rao rx_npkts++; 1739ce4946daSBill Paul } 1740ce4946daSBill Paul 1741f6bc9430SPyun YongHyeon if (prog > 0) { 1742f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_cons = cons; 1743f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag, 1744f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map, 1745f6bc9430SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1746f6bc9430SPyun YongHyeon } 174756e13f2aSAttilio Rao return (rx_npkts); 1748ce4946daSBill Paul } 1749ce4946daSBill Paul 1750ce4946daSBill Paul /* 1751ce4946daSBill Paul * A frame was downloaded to the chip. It's safe for us to clean up 1752ce4946daSBill Paul * the list buffers. 1753ce4946daSBill Paul */ 1754eaabec55SAlfred Perlstein static void 1755284c81cbSPyun YongHyeon nge_txeof(struct nge_softc *sc) 1756ce4946daSBill Paul { 17571e73ec7dSRuslan Ermilov struct nge_desc *cur_tx; 1758f6bc9430SPyun YongHyeon struct nge_txdesc *txd; 1759ce4946daSBill Paul struct ifnet *ifp; 1760f6bc9430SPyun YongHyeon uint32_t cmdsts; 1761f6bc9430SPyun YongHyeon int cons, prod; 1762ce4946daSBill Paul 1763ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 1764fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 1765ce4946daSBill Paul 1766f6bc9430SPyun YongHyeon cons = sc->nge_cdata.nge_tx_cons; 1767f6bc9430SPyun YongHyeon prod = sc->nge_cdata.nge_tx_prod; 1768f6bc9430SPyun YongHyeon if (cons == prod) 1769f6bc9430SPyun YongHyeon return; 1770f6bc9430SPyun YongHyeon 1771f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag, 1772f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map, 1773f6bc9430SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1774f6bc9430SPyun YongHyeon 1775ce4946daSBill Paul /* 1776ce4946daSBill Paul * Go through our tx list and free mbufs for those 1777ce4946daSBill Paul * frames that have been transmitted. 1778ce4946daSBill Paul */ 1779f6bc9430SPyun YongHyeon for (; cons != prod; NGE_INC(cons, NGE_TX_RING_CNT)) { 1780f6bc9430SPyun YongHyeon cur_tx = &sc->nge_rdata.nge_tx_ring[cons]; 1781f6bc9430SPyun YongHyeon cmdsts = le32toh(cur_tx->nge_cmdsts); 1782f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_OWN) != 0) 1783ce4946daSBill Paul break; 1784ce4946daSBill Paul sc->nge_cdata.nge_tx_cnt--; 178513f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1786f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_MORE) != 0) 1787f6bc9430SPyun YongHyeon continue; 1788f6bc9430SPyun YongHyeon 1789f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[cons]; 1790f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap, 1791f6bc9430SPyun YongHyeon BUS_DMASYNC_POSTWRITE); 1792f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap); 1793f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) { 1794f6bc9430SPyun YongHyeon ifp->if_oerrors++; 1795f6bc9430SPyun YongHyeon if ((cmdsts & NGE_TXSTAT_EXCESSCOLLS) != 0) 1796f6bc9430SPyun YongHyeon ifp->if_collisions++; 1797f6bc9430SPyun YongHyeon if ((cmdsts & NGE_TXSTAT_OUTOFWINCOLL) != 0) 1798f6bc9430SPyun YongHyeon ifp->if_collisions++; 1799f6bc9430SPyun YongHyeon } else 1800f6bc9430SPyun YongHyeon ifp->if_opackets++; 1801f6bc9430SPyun YongHyeon 1802f6bc9430SPyun YongHyeon ifp->if_collisions += (cmdsts & NGE_TXSTAT_COLLCNT) >> 16; 1803f6bc9430SPyun YongHyeon KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!\n", 1804f6bc9430SPyun YongHyeon __func__)); 1805f6bc9430SPyun YongHyeon m_freem(txd->tx_m); 1806f6bc9430SPyun YongHyeon txd->tx_m = NULL; 1807ce4946daSBill Paul } 1808ce4946daSBill Paul 1809f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_cons = cons; 1810f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_tx_cnt == 0) 1811f6bc9430SPyun YongHyeon sc->nge_watchdog_timer = 0; 1812ce4946daSBill Paul } 1813ce4946daSBill Paul 1814eaabec55SAlfred Perlstein static void 1815284c81cbSPyun YongHyeon nge_tick(void *xsc) 1816ce4946daSBill Paul { 1817ce4946daSBill Paul struct nge_softc *sc; 1818ad6c618bSBill Paul struct mii_data *mii; 1819ad6c618bSBill Paul 1820646abee6SJohn Baldwin sc = xsc; 1821ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 1822ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 1823ce4946daSBill Paul mii_tick(mii); 1824f6bc9430SPyun YongHyeon /* 1825f6bc9430SPyun YongHyeon * For PHYs that does not reset established link, it is 1826f6bc9430SPyun YongHyeon * necessary to check whether driver still have a valid 1827f6bc9430SPyun YongHyeon * link(e.g link state change callback is not called). 1828f6bc9430SPyun YongHyeon * Otherwise, driver think it lost link because driver 1829f6bc9430SPyun YongHyeon * initialization routine clears link state flag. 1830f6bc9430SPyun YongHyeon */ 1831f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_LINK) == 0) 1832f6bc9430SPyun YongHyeon nge_miibus_statchg(sc->nge_dev); 1833f6bc9430SPyun YongHyeon nge_stats_update(sc); 1834f6bc9430SPyun YongHyeon nge_watchdog(sc); 1835ad6c618bSBill Paul callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc); 1836ce4946daSBill Paul } 1837ce4946daSBill Paul 1838f6bc9430SPyun YongHyeon static void 1839f6bc9430SPyun YongHyeon nge_stats_update(struct nge_softc *sc) 1840f6bc9430SPyun YongHyeon { 1841f6bc9430SPyun YongHyeon struct ifnet *ifp; 1842f6bc9430SPyun YongHyeon struct nge_stats now, *stats, *nstats; 1843f6bc9430SPyun YongHyeon 1844f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 1845f6bc9430SPyun YongHyeon 1846f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 1847f6bc9430SPyun YongHyeon stats = &now; 1848f6bc9430SPyun YongHyeon stats->rx_pkts_errs = 1849f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRPKT) & 0xFFFF; 1850f6bc9430SPyun YongHyeon stats->rx_crc_errs = 1851f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRFCS) & 0xFFFF; 1852f6bc9430SPyun YongHyeon stats->rx_fifo_oflows = 1853f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRMISSEDPKT) & 0xFFFF; 1854f6bc9430SPyun YongHyeon stats->rx_align_errs = 1855f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRALIGN) & 0xFFFF; 1856f6bc9430SPyun YongHyeon stats->rx_sym_errs = 1857f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRSYM) & 0xFFFF; 1858f6bc9430SPyun YongHyeon stats->rx_pkts_jumbos = 1859f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRGIANT) & 0xFFFF; 1860f6bc9430SPyun YongHyeon stats->rx_len_errs = 1861f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRRANGLEN) & 0xFFFF; 1862f6bc9430SPyun YongHyeon stats->rx_unctl_frames = 1863f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXBADOPCODE) & 0xFFFF; 1864f6bc9430SPyun YongHyeon stats->rx_pause = 1865f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXPAUSEPKTS) & 0xFFFF; 1866f6bc9430SPyun YongHyeon stats->tx_pause = 1867f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_TXPAUSEPKTS) & 0xFFFF; 1868f6bc9430SPyun YongHyeon stats->tx_seq_errs = 1869f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_TXERRSQE) & 0xFF; 1870f6bc9430SPyun YongHyeon 1871f6bc9430SPyun YongHyeon /* 1872f6bc9430SPyun YongHyeon * Since we've accept errored frames exclude Rx length errors. 1873f6bc9430SPyun YongHyeon */ 1874f6bc9430SPyun YongHyeon ifp->if_ierrors += stats->rx_pkts_errs + stats->rx_crc_errs + 1875f6bc9430SPyun YongHyeon stats->rx_fifo_oflows + stats->rx_sym_errs; 1876f6bc9430SPyun YongHyeon 1877f6bc9430SPyun YongHyeon nstats = &sc->nge_stats; 1878f6bc9430SPyun YongHyeon nstats->rx_pkts_errs += stats->rx_pkts_errs; 1879f6bc9430SPyun YongHyeon nstats->rx_crc_errs += stats->rx_crc_errs; 1880f6bc9430SPyun YongHyeon nstats->rx_fifo_oflows += stats->rx_fifo_oflows; 1881f6bc9430SPyun YongHyeon nstats->rx_align_errs += stats->rx_align_errs; 1882f6bc9430SPyun YongHyeon nstats->rx_sym_errs += stats->rx_sym_errs; 1883f6bc9430SPyun YongHyeon nstats->rx_pkts_jumbos += stats->rx_pkts_jumbos; 1884f6bc9430SPyun YongHyeon nstats->rx_len_errs += stats->rx_len_errs; 1885f6bc9430SPyun YongHyeon nstats->rx_unctl_frames += stats->rx_unctl_frames; 1886f6bc9430SPyun YongHyeon nstats->rx_pause += stats->rx_pause; 1887f6bc9430SPyun YongHyeon nstats->tx_pause += stats->tx_pause; 1888f6bc9430SPyun YongHyeon nstats->tx_seq_errs += stats->tx_seq_errs; 1889f6bc9430SPyun YongHyeon } 1890f6bc9430SPyun YongHyeon 1891196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING 1892196c0df6SHidetoshi Shimokawa static poll_handler_t nge_poll; 1893196c0df6SHidetoshi Shimokawa 189456e13f2aSAttilio Rao static int 1895196c0df6SHidetoshi Shimokawa nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1896196c0df6SHidetoshi Shimokawa { 1897f6bc9430SPyun YongHyeon struct nge_softc *sc; 189856e13f2aSAttilio Rao int rx_npkts = 0; 1899f6bc9430SPyun YongHyeon 1900f6bc9430SPyun YongHyeon sc = ifp->if_softc; 1901196c0df6SHidetoshi Shimokawa 1902ad6c618bSBill Paul NGE_LOCK(sc); 1903f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1904ad6c618bSBill Paul NGE_UNLOCK(sc); 190556e13f2aSAttilio Rao return (rx_npkts); 1906196c0df6SHidetoshi Shimokawa } 1907196c0df6SHidetoshi Shimokawa 1908196c0df6SHidetoshi Shimokawa /* 1909196c0df6SHidetoshi Shimokawa * On the nge, reading the status register also clears it. 1910196c0df6SHidetoshi Shimokawa * So before returning to intr mode we must make sure that all 1911196c0df6SHidetoshi Shimokawa * possible pending sources of interrupts have been served. 1912196c0df6SHidetoshi Shimokawa * In practice this means run to completion the *eof routines, 1913f6bc9430SPyun YongHyeon * and then call the interrupt routine. 1914196c0df6SHidetoshi Shimokawa */ 1915196c0df6SHidetoshi Shimokawa sc->rxcycles = count; 191656e13f2aSAttilio Rao rx_npkts = nge_rxeof(sc); 1917196c0df6SHidetoshi Shimokawa nge_txeof(sc); 1918f6bc9430SPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1919ad6c618bSBill Paul nge_start_locked(ifp); 1920196c0df6SHidetoshi Shimokawa 1921196c0df6SHidetoshi Shimokawa if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) { 19223929ff51SPyun YongHyeon uint32_t status; 1923196c0df6SHidetoshi Shimokawa 1924196c0df6SHidetoshi Shimokawa /* Reading the ISR register clears all interrupts. */ 1925196c0df6SHidetoshi Shimokawa status = CSR_READ_4(sc, NGE_ISR); 1926196c0df6SHidetoshi Shimokawa 1927f6bc9430SPyun YongHyeon if ((status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW)) != 0) 192856e13f2aSAttilio Rao rx_npkts += nge_rxeof(sc); 1929196c0df6SHidetoshi Shimokawa 1930f6bc9430SPyun YongHyeon if ((status & NGE_ISR_RX_IDLE) != 0) 1931196c0df6SHidetoshi Shimokawa NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 1932196c0df6SHidetoshi Shimokawa 1933f6bc9430SPyun YongHyeon if ((status & NGE_ISR_SYSERR) != 0) { 1934f6bc9430SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1935ad6c618bSBill Paul nge_init_locked(sc); 1936196c0df6SHidetoshi Shimokawa } 1937196c0df6SHidetoshi Shimokawa } 1938ad6c618bSBill Paul NGE_UNLOCK(sc); 193956e13f2aSAttilio Rao return (rx_npkts); 1940196c0df6SHidetoshi Shimokawa } 1941196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */ 1942196c0df6SHidetoshi Shimokawa 1943eaabec55SAlfred Perlstein static void 1944284c81cbSPyun YongHyeon nge_intr(void *arg) 1945ce4946daSBill Paul { 1946ce4946daSBill Paul struct nge_softc *sc; 1947ce4946daSBill Paul struct ifnet *ifp; 19483929ff51SPyun YongHyeon uint32_t status; 1949ce4946daSBill Paul 1950f6bc9430SPyun YongHyeon sc = (struct nge_softc *)arg; 1951fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 1952ce4946daSBill Paul 1953ad6c618bSBill Paul NGE_LOCK(sc); 1954196c0df6SHidetoshi Shimokawa 1955f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_SUSPENDED) != 0) 1956f6bc9430SPyun YongHyeon goto done_locked; 1957f6bc9430SPyun YongHyeon 1958f6bc9430SPyun YongHyeon /* Reading the ISR register clears all interrupts. */ 1959f6bc9430SPyun YongHyeon status = CSR_READ_4(sc, NGE_ISR); 1960f6bc9430SPyun YongHyeon if (status == 0xffffffff || (status & NGE_INTRS) == 0) 1961f6bc9430SPyun YongHyeon goto done_locked; 1962f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING 1963f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_POLLING) != 0) 1964f6bc9430SPyun YongHyeon goto done_locked; 1965f6bc9430SPyun YongHyeon #endif 1966f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1967f6bc9430SPyun YongHyeon goto done_locked; 1968ce4946daSBill Paul 1969ce4946daSBill Paul /* Disable interrupts. */ 1970ce4946daSBill Paul CSR_WRITE_4(sc, NGE_IER, 0); 1971ce4946daSBill Paul 19721f548804SDoug Ambrisko /* Data LED on for TBI mode */ 1973f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_TBI) != 0) 1974f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_GPIO, 1975f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_GPIO) | NGE_GPIO_GP3_OUT); 19761f548804SDoug Ambrisko 1977f6bc9430SPyun YongHyeon for (; (status & NGE_INTRS) != 0;) { 1978f6bc9430SPyun YongHyeon if ((status & (NGE_ISR_TX_DESC_OK | NGE_ISR_TX_ERR | 1979f6bc9430SPyun YongHyeon NGE_ISR_TX_OK | NGE_ISR_TX_IDLE)) != 0) 1980ce4946daSBill Paul nge_txeof(sc); 1981ce4946daSBill Paul 1982f6bc9430SPyun YongHyeon if ((status & (NGE_ISR_RX_DESC_OK | NGE_ISR_RX_ERR | 1983f6bc9430SPyun YongHyeon NGE_ISR_RX_OFLOW | NGE_ISR_RX_FIFO_OFLOW | 1984f6bc9430SPyun YongHyeon NGE_ISR_RX_IDLE | NGE_ISR_RX_OK)) != 0) 1985ce4946daSBill Paul nge_rxeof(sc); 1986ff7ed9f7SPoul-Henning Kamp 1987f6bc9430SPyun YongHyeon if ((status & NGE_ISR_RX_IDLE) != 0) 1988ff7ed9f7SPoul-Henning Kamp NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 1989ff7ed9f7SPoul-Henning Kamp 1990f6bc9430SPyun YongHyeon if ((status & NGE_ISR_SYSERR) != 0) { 199113f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1992ad6c618bSBill Paul nge_init_locked(sc); 1993ce4946daSBill Paul } 1994f6bc9430SPyun YongHyeon /* Reading the ISR register clears all interrupts. */ 1995f6bc9430SPyun YongHyeon status = CSR_READ_4(sc, NGE_ISR); 1996ce4946daSBill Paul } 1997ce4946daSBill Paul 1998ce4946daSBill Paul /* Re-enable interrupts. */ 1999ce4946daSBill Paul CSR_WRITE_4(sc, NGE_IER, 1); 2000ce4946daSBill Paul 2001f6bc9430SPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2002ad6c618bSBill Paul nge_start_locked(ifp); 2003ce4946daSBill Paul 20041f548804SDoug Ambrisko /* Data LED off for TBI mode */ 2005f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_TBI) != 0) 2006f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_GPIO, 2007f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT); 20081f548804SDoug Ambrisko 2009f6bc9430SPyun YongHyeon done_locked: 2010ad6c618bSBill Paul NGE_UNLOCK(sc); 2011ce4946daSBill Paul } 2012ce4946daSBill Paul 2013ce4946daSBill Paul /* 2014ce4946daSBill Paul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 2015ce4946daSBill Paul * pointers to the fragment pointers. 2016ce4946daSBill Paul */ 2017eaabec55SAlfred Perlstein static int 2018f6bc9430SPyun YongHyeon nge_encap(struct nge_softc *sc, struct mbuf **m_head) 2019ce4946daSBill Paul { 2020f6bc9430SPyun YongHyeon struct nge_txdesc *txd, *txd_last; 2021f6bc9430SPyun YongHyeon struct nge_desc *desc; 2022ce4946daSBill Paul struct mbuf *m; 2023f6bc9430SPyun YongHyeon bus_dmamap_t map; 2024f6bc9430SPyun YongHyeon bus_dma_segment_t txsegs[NGE_MAXTXSEGS]; 2025f6bc9430SPyun YongHyeon int error, i, nsegs, prod, si; 2026ce4946daSBill Paul 2027f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2028ce4946daSBill Paul 2029f6bc9430SPyun YongHyeon m = *m_head; 2030f6bc9430SPyun YongHyeon prod = sc->nge_cdata.nge_tx_prod; 2031f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[prod]; 2032f6bc9430SPyun YongHyeon txd_last = txd; 2033f6bc9430SPyun YongHyeon map = txd->tx_dmamap; 2034f6bc9430SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag, map, 2035f6bc9430SPyun YongHyeon *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT); 2036f6bc9430SPyun YongHyeon if (error == EFBIG) { 2037f6bc9430SPyun YongHyeon m = m_collapse(*m_head, M_DONTWAIT, NGE_MAXTXSEGS); 2038f6bc9430SPyun YongHyeon if (m == NULL) { 2039f6bc9430SPyun YongHyeon m_freem(*m_head); 2040f6bc9430SPyun YongHyeon *m_head = NULL; 2041ce4946daSBill Paul return (ENOBUFS); 2042ce4946daSBill Paul } 2043f6bc9430SPyun YongHyeon *m_head = m; 2044f6bc9430SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag, 2045f6bc9430SPyun YongHyeon map, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT); 2046f6bc9430SPyun YongHyeon if (error != 0) { 2047f6bc9430SPyun YongHyeon m_freem(*m_head); 2048f6bc9430SPyun YongHyeon *m_head = NULL; 2049f6bc9430SPyun YongHyeon return (error); 2050f6bc9430SPyun YongHyeon } 2051f6bc9430SPyun YongHyeon } else if (error != 0) 2052f6bc9430SPyun YongHyeon return (error); 2053f6bc9430SPyun YongHyeon if (nsegs == 0) { 2054f6bc9430SPyun YongHyeon m_freem(*m_head); 2055f6bc9430SPyun YongHyeon *m_head = NULL; 2056f6bc9430SPyun YongHyeon return (EIO); 2057ce4946daSBill Paul } 2058ce4946daSBill Paul 2059f6bc9430SPyun YongHyeon /* Check number of available descriptors. */ 2060f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_tx_cnt + nsegs >= (NGE_TX_RING_CNT - 1)) { 2061f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, map); 2062ce4946daSBill Paul return (ENOBUFS); 2063ce4946daSBill Paul } 2064ce4946daSBill Paul 2065f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, map, BUS_DMASYNC_PREWRITE); 2066ce4946daSBill Paul 2067f6bc9430SPyun YongHyeon si = prod; 2068f6bc9430SPyun YongHyeon for (i = 0; i < nsegs; i++) { 2069f6bc9430SPyun YongHyeon desc = &sc->nge_rdata.nge_tx_ring[prod]; 2070f6bc9430SPyun YongHyeon desc->nge_ptr = htole32(NGE_ADDR_LO(txsegs[i].ds_addr)); 2071f6bc9430SPyun YongHyeon if (i == 0) 2072f6bc9430SPyun YongHyeon desc->nge_cmdsts = htole32(txsegs[i].ds_len | 2073f6bc9430SPyun YongHyeon NGE_CMDSTS_MORE); 2074f6bc9430SPyun YongHyeon else 2075f6bc9430SPyun YongHyeon desc->nge_cmdsts = htole32(txsegs[i].ds_len | 2076f6bc9430SPyun YongHyeon NGE_CMDSTS_MORE | NGE_CMDSTS_OWN); 2077f6bc9430SPyun YongHyeon desc->nge_extsts = 0; 2078f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_cnt++; 2079f6bc9430SPyun YongHyeon NGE_INC(prod, NGE_TX_RING_CNT); 2080f6bc9430SPyun YongHyeon } 2081f6bc9430SPyun YongHyeon /* Update producer index. */ 2082f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_prod = prod; 2083f6bc9430SPyun YongHyeon 2084f6bc9430SPyun YongHyeon prod = (prod + NGE_TX_RING_CNT - 1) % NGE_TX_RING_CNT; 2085f6bc9430SPyun YongHyeon desc = &sc->nge_rdata.nge_tx_ring[prod]; 2086f6bc9430SPyun YongHyeon /* Check if we have a VLAN tag to insert. */ 2087f6bc9430SPyun YongHyeon if ((m->m_flags & M_VLANTAG) != 0) 2088f6bc9430SPyun YongHyeon desc->nge_extsts |= htole32(NGE_TXEXTSTS_VLANPKT | 2089f6bc9430SPyun YongHyeon bswap16(m->m_pkthdr.ether_vtag)); 2090f6bc9430SPyun YongHyeon /* Set EOP on the last desciptor. */ 2091f6bc9430SPyun YongHyeon desc->nge_cmdsts &= htole32(~NGE_CMDSTS_MORE); 2092f6bc9430SPyun YongHyeon 2093f6bc9430SPyun YongHyeon /* Set checksum offload in the first descriptor. */ 2094f6bc9430SPyun YongHyeon desc = &sc->nge_rdata.nge_tx_ring[si]; 2095f6bc9430SPyun YongHyeon if ((m->m_pkthdr.csum_flags & NGE_CSUM_FEATURES) != 0) { 2096f6bc9430SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0) 2097f6bc9430SPyun YongHyeon desc->nge_extsts |= htole32(NGE_TXEXTSTS_IPCSUM); 2098f6bc9430SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0) 2099f6bc9430SPyun YongHyeon desc->nge_extsts |= htole32(NGE_TXEXTSTS_TCPCSUM); 2100f6bc9430SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0) 2101f6bc9430SPyun YongHyeon desc->nge_extsts |= htole32(NGE_TXEXTSTS_UDPCSUM); 2102f6bc9430SPyun YongHyeon } 2103f6bc9430SPyun YongHyeon /* Lastly, turn the first descriptor ownership to hardware. */ 2104f6bc9430SPyun YongHyeon desc->nge_cmdsts |= htole32(NGE_CMDSTS_OWN); 2105f6bc9430SPyun YongHyeon 2106f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[prod]; 2107f6bc9430SPyun YongHyeon map = txd_last->tx_dmamap; 2108f6bc9430SPyun YongHyeon txd_last->tx_dmamap = txd->tx_dmamap; 2109f6bc9430SPyun YongHyeon txd->tx_dmamap = map; 2110f6bc9430SPyun YongHyeon txd->tx_m = m; 2111ce4946daSBill Paul 2112ce4946daSBill Paul return (0); 2113ce4946daSBill Paul } 2114ce4946daSBill Paul 2115ce4946daSBill Paul /* 2116ce4946daSBill Paul * Main transmit routine. To avoid having to do mbuf copies, we put pointers 2117ce4946daSBill Paul * to the mbuf data regions directly in the transmit lists. We also save a 2118ce4946daSBill Paul * copy of the pointers since the transmit list fragment pointers are 2119ce4946daSBill Paul * physical addresses. 2120ce4946daSBill Paul */ 2121ce4946daSBill Paul 2122eaabec55SAlfred Perlstein static void 2123284c81cbSPyun YongHyeon nge_start(struct ifnet *ifp) 2124ce4946daSBill Paul { 2125ce4946daSBill Paul struct nge_softc *sc; 2126ad6c618bSBill Paul 2127ad6c618bSBill Paul sc = ifp->if_softc; 2128ad6c618bSBill Paul NGE_LOCK(sc); 2129ad6c618bSBill Paul nge_start_locked(ifp); 2130ad6c618bSBill Paul NGE_UNLOCK(sc); 2131ad6c618bSBill Paul } 2132ad6c618bSBill Paul 2133ad6c618bSBill Paul static void 2134284c81cbSPyun YongHyeon nge_start_locked(struct ifnet *ifp) 2135ad6c618bSBill Paul { 2136ad6c618bSBill Paul struct nge_softc *sc; 2137f6bc9430SPyun YongHyeon struct mbuf *m_head; 2138f6bc9430SPyun YongHyeon int enq; 2139ce4946daSBill Paul 2140ce4946daSBill Paul sc = ifp->if_softc; 2141ce4946daSBill Paul 2142f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2143f6bc9430SPyun YongHyeon 2144f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 2145f6bc9430SPyun YongHyeon IFF_DRV_RUNNING || (sc->nge_flags & NGE_FLAG_LINK) == 0) 2146ce4946daSBill Paul return; 2147ce4946daSBill Paul 2148f6bc9430SPyun YongHyeon for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 2149f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_cnt < NGE_TX_RING_CNT - 2; ) { 2150f6bc9430SPyun YongHyeon IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2151ce4946daSBill Paul if (m_head == NULL) 2152ce4946daSBill Paul break; 2153f6bc9430SPyun YongHyeon /* 2154f6bc9430SPyun YongHyeon * Pack the data into the transmit ring. If we 2155f6bc9430SPyun YongHyeon * don't have room, set the OACTIVE flag and wait 2156f6bc9430SPyun YongHyeon * for the NIC to drain the ring. 2157f6bc9430SPyun YongHyeon */ 2158f6bc9430SPyun YongHyeon if (nge_encap(sc, &m_head)) { 2159f6bc9430SPyun YongHyeon if (m_head == NULL) 2160f6bc9430SPyun YongHyeon break; 2161f6bc9430SPyun YongHyeon IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 216213f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2163ce4946daSBill Paul break; 2164ce4946daSBill Paul } 2165ce4946daSBill Paul 2166f6bc9430SPyun YongHyeon enq++; 2167ce4946daSBill Paul /* 2168ce4946daSBill Paul * If there's a BPF listener, bounce a copy of this frame 2169ce4946daSBill Paul * to him. 2170ce4946daSBill Paul */ 217159a0d28bSChristian S.J. Peron ETHER_BPF_MTAP(ifp, m_head); 2172ce4946daSBill Paul } 2173ce4946daSBill Paul 2174f6bc9430SPyun YongHyeon if (enq > 0) { 2175f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag, 2176f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map, 2177f6bc9430SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2178ce4946daSBill Paul /* Transmit */ 2179ce4946daSBill Paul NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE); 2180ce4946daSBill Paul 2181f6bc9430SPyun YongHyeon /* Set a timeout in case the chip goes out to lunch. */ 2182f6bc9430SPyun YongHyeon sc->nge_watchdog_timer = 5; 2183f6bc9430SPyun YongHyeon } 2184ce4946daSBill Paul } 2185ce4946daSBill Paul 2186eaabec55SAlfred Perlstein static void 2187284c81cbSPyun YongHyeon nge_init(void *xsc) 2188ce4946daSBill Paul { 2189ce4946daSBill Paul struct nge_softc *sc = xsc; 2190ad6c618bSBill Paul 2191ad6c618bSBill Paul NGE_LOCK(sc); 2192ad6c618bSBill Paul nge_init_locked(sc); 2193ad6c618bSBill Paul NGE_UNLOCK(sc); 2194ad6c618bSBill Paul } 2195ad6c618bSBill Paul 2196ad6c618bSBill Paul static void 2197284c81cbSPyun YongHyeon nge_init_locked(struct nge_softc *sc) 2198ad6c618bSBill Paul { 2199fc74a9f9SBrooks Davis struct ifnet *ifp = sc->nge_ifp; 2200ce4946daSBill Paul struct mii_data *mii; 2201f6bc9430SPyun YongHyeon uint8_t *eaddr; 2202f6bc9430SPyun YongHyeon uint32_t reg; 2203ad6c618bSBill Paul 2204ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 2205ce4946daSBill Paul 2206f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2207ce4946daSBill Paul return; 2208ce4946daSBill Paul 2209ce4946daSBill Paul /* 2210ce4946daSBill Paul * Cancel pending I/O and free all RX/TX buffers. 2211ce4946daSBill Paul */ 2212ce4946daSBill Paul nge_stop(sc); 2213ce4946daSBill Paul 2214f6bc9430SPyun YongHyeon /* Reset the adapter. */ 2215f6bc9430SPyun YongHyeon nge_reset(sc); 2216ce4946daSBill Paul 2217f6bc9430SPyun YongHyeon /* Disable Rx filter prior to programming Rx filter. */ 2218f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_CTL, 0); 2219f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_RXFILT_CTL); 2220f6bc9430SPyun YongHyeon 2221f6bc9430SPyun YongHyeon mii = device_get_softc(sc->nge_miibus); 2222f6bc9430SPyun YongHyeon 2223f6bc9430SPyun YongHyeon /* Set MAC address. */ 2224f6bc9430SPyun YongHyeon eaddr = IF_LLADDR(sc->nge_ifp); 2225ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0); 2226f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[1] << 8) | eaddr[0]); 2227ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1); 2228f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[3] << 8) | eaddr[2]); 2229ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2); 2230f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[5] << 8) | eaddr[4]); 2231ce4946daSBill Paul 2232ce4946daSBill Paul /* Init circular RX list. */ 2233ce4946daSBill Paul if (nge_list_rx_init(sc) == ENOBUFS) { 22346b9f5c94SGleb Smirnoff device_printf(sc->nge_dev, "initialization failed: no " 2235646abee6SJohn Baldwin "memory for rx buffers\n"); 2236ce4946daSBill Paul nge_stop(sc); 2237ce4946daSBill Paul return; 2238ce4946daSBill Paul } 2239ce4946daSBill Paul 2240ce4946daSBill Paul /* 2241ce4946daSBill Paul * Init tx descriptors. 2242ce4946daSBill Paul */ 2243ce4946daSBill Paul nge_list_tx_init(sc); 2244ce4946daSBill Paul 2245ce4946daSBill Paul /* 2246ce4946daSBill Paul * For the NatSemi chip, we have to explicitly enable the 2247ce4946daSBill Paul * reception of ARP frames, as well as turn on the 'perfect 2248ce4946daSBill Paul * match' filter where we store the station address, otherwise 2249ce4946daSBill Paul * we won't receive unicasts meant for this host. 2250ce4946daSBill Paul */ 2251ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP); 2252ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT); 2253ce4946daSBill Paul 2254ce4946daSBill Paul /* 2255ce4946daSBill Paul * Set the capture broadcast bit to capture broadcast frames. 2256ce4946daSBill Paul */ 2257ce4946daSBill Paul if (ifp->if_flags & IFF_BROADCAST) { 2258ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD); 2259ce4946daSBill Paul } else { 2260ce4946daSBill Paul NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD); 2261ce4946daSBill Paul } 2262ce4946daSBill Paul 2263f6bc9430SPyun YongHyeon /* Turn the receive filter on. */ 2264ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE); 2265ce4946daSBill Paul 2266f6bc9430SPyun YongHyeon /* Set Rx filter. */ 2267f6bc9430SPyun YongHyeon nge_rxfilter(sc); 2268f6bc9430SPyun YongHyeon 2269f6bc9430SPyun YongHyeon /* Disable PRIQ ctl. */ 2270f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_PRIOQCTL, 0); 2271f6bc9430SPyun YongHyeon 2272f6bc9430SPyun YongHyeon /* 2273f6bc9430SPyun YongHyeon * Set pause frames paramters. 2274f6bc9430SPyun YongHyeon * Rx stat FIFO hi-threshold : 2 or more packets 2275f6bc9430SPyun YongHyeon * Rx stat FIFO lo-threshold : less than 2 packets 2276f6bc9430SPyun YongHyeon * Rx data FIFO hi-threshold : 2K or more bytes 2277f6bc9430SPyun YongHyeon * Rx data FIFO lo-threshold : less than 2K bytes 2278f6bc9430SPyun YongHyeon * pause time : (512ns * 0xffff) -> 33.55ms 2279f6bc9430SPyun YongHyeon */ 2280f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_PAUSECSR, 2281f6bc9430SPyun YongHyeon NGE_PAUSECSR_PAUSE_ON_MCAST | 2282f6bc9430SPyun YongHyeon NGE_PAUSECSR_PAUSE_ON_DA | 2283f6bc9430SPyun YongHyeon ((1 << 24) & NGE_PAUSECSR_RX_STATFIFO_THR_HI) | 2284f6bc9430SPyun YongHyeon ((1 << 22) & NGE_PAUSECSR_RX_STATFIFO_THR_LO) | 2285f6bc9430SPyun YongHyeon ((1 << 20) & NGE_PAUSECSR_RX_DATAFIFO_THR_HI) | 2286f6bc9430SPyun YongHyeon ((1 << 18) & NGE_PAUSECSR_RX_DATAFIFO_THR_LO) | 2287f6bc9430SPyun YongHyeon NGE_PAUSECSR_CNT); 2288f6bc9430SPyun YongHyeon 2289ce4946daSBill Paul /* 2290ce4946daSBill Paul * Load the address of the RX and TX lists. 2291ce4946daSBill Paul */ 2292f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 2293f6bc9430SPyun YongHyeon NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr)); 2294f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 2295f6bc9430SPyun YongHyeon NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr)); 2296f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI, 2297f6bc9430SPyun YongHyeon NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr)); 2298f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO, 2299f6bc9430SPyun YongHyeon NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr)); 2300ce4946daSBill Paul 2301f6bc9430SPyun YongHyeon /* Set RX configuration. */ 2302ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG); 2303f6bc9430SPyun YongHyeon 2304f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, 0); 2305ce4946daSBill Paul /* 2306ce4946daSBill Paul * Enable hardware checksum validation for all IPv4 2307ce4946daSBill Paul * packets, do not reject packets with bad checksums. 2308ce4946daSBill Paul */ 2309f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 2310f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB); 2311ce4946daSBill Paul 2312ce4946daSBill Paul /* 23139d4fe4b2SBrooks Davis * Tell the chip to detect and strip VLAN tag info from 23149d4fe4b2SBrooks Davis * received frames. The tag will be provided in the extsts 23159d4fe4b2SBrooks Davis * field in the RX descriptors. 2316ce4946daSBill Paul */ 2317f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_DETECT_ENB); 2318f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) 2319f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_STRIP_ENB); 2320ce4946daSBill Paul 2321f6bc9430SPyun YongHyeon /* Set TX configuration. */ 2322ce4946daSBill Paul CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG); 2323ce4946daSBill Paul 2324ce4946daSBill Paul /* 2325ce4946daSBill Paul * Enable TX IPv4 checksumming on a per-packet basis. 2326ce4946daSBill Paul */ 2327ce4946daSBill Paul CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT); 2328ce4946daSBill Paul 2329ce4946daSBill Paul /* 23309d4fe4b2SBrooks Davis * Tell the chip to insert VLAN tags on a per-packet basis as 23319d4fe4b2SBrooks Davis * dictated by the code in the frame encapsulation routine. 2332ce4946daSBill Paul */ 2333ce4946daSBill Paul NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT); 2334ce4946daSBill Paul 2335ce4946daSBill Paul /* 2336ce4946daSBill Paul * Enable the delivery of PHY interrupts based on 233723d3a203SBill Paul * link/speed/duplex status changes. Also enable the 233823d3a203SBill Paul * extsts field in the DMA descriptors (needed for 233923d3a203SBill Paul * TCP/IP checksum offload on transmit). 2340ce4946daSBill Paul */ 23412ce0498bSBill Paul NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD | 234223d3a203SBill Paul NGE_CFG_PHYINTR_LNK | NGE_CFG_PHYINTR_DUP | NGE_CFG_EXTSTS_ENB); 2343ce4946daSBill Paul 2344ce4946daSBill Paul /* 2345962315f6SBill Paul * Configure interrupt holdoff (moderation). We can 2346962315f6SBill Paul * have the chip delay interrupt delivery for a certain 2347962315f6SBill Paul * period. Units are in 100us, and the max setting 2348962315f6SBill Paul * is 25500us (0xFF x 100us). Default is a 100us holdoff. 2349962315f6SBill Paul */ 2350f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_IHR, sc->nge_int_holdoff); 2351f6bc9430SPyun YongHyeon 2352f6bc9430SPyun YongHyeon /* 2353f6bc9430SPyun YongHyeon * Enable MAC statistics counters and clear. 2354f6bc9430SPyun YongHyeon */ 2355f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_MIBCTL); 2356f6bc9430SPyun YongHyeon reg &= ~NGE_MIBCTL_FREEZE_CNT; 2357f6bc9430SPyun YongHyeon reg |= NGE_MIBCTL_CLEAR_CNT; 2358f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_MIBCTL, reg); 2359962315f6SBill Paul 2360962315f6SBill Paul /* 2361ce4946daSBill Paul * Enable interrupts. 2362ce4946daSBill Paul */ 2363ce4946daSBill Paul CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS); 2364196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING 2365196c0df6SHidetoshi Shimokawa /* 2366196c0df6SHidetoshi Shimokawa * ... only enable interrupts if we are not polling, make sure 2367196c0df6SHidetoshi Shimokawa * they are off otherwise. 2368196c0df6SHidetoshi Shimokawa */ 2369f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_POLLING) != 0) 2370196c0df6SHidetoshi Shimokawa CSR_WRITE_4(sc, NGE_IER, 0); 2371196c0df6SHidetoshi Shimokawa else 237240929967SGleb Smirnoff #endif 2373ce4946daSBill Paul CSR_WRITE_4(sc, NGE_IER, 1); 2374ce4946daSBill Paul 2375f6bc9430SPyun YongHyeon sc->nge_flags &= ~NGE_FLAG_LINK; 2376f6bc9430SPyun YongHyeon mii_mediachg(mii); 2377ce4946daSBill Paul 2378f6bc9430SPyun YongHyeon sc->nge_watchdog_timer = 0; 2379f6bc9430SPyun YongHyeon callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc); 2380ce4946daSBill Paul 238113f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 238213f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2383ce4946daSBill Paul } 2384ce4946daSBill Paul 2385ce4946daSBill Paul /* 2386ce4946daSBill Paul * Set media options. 2387ce4946daSBill Paul */ 2388eaabec55SAlfred Perlstein static int 2389f6bc9430SPyun YongHyeon nge_mediachange(struct ifnet *ifp) 2390646abee6SJohn Baldwin { 2391646abee6SJohn Baldwin struct nge_softc *sc; 2392ce4946daSBill Paul struct mii_data *mii; 2393f6bc9430SPyun YongHyeon struct mii_softc *miisc; 2394f6bc9430SPyun YongHyeon int error; 2395ce4946daSBill Paul 2396ce4946daSBill Paul sc = ifp->if_softc; 2397f6bc9430SPyun YongHyeon NGE_LOCK(sc); 2398ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 2399646abee6SJohn Baldwin LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 2400*3fcb7a53SMarius Strobl PHY_RESET(miisc); 2401f6bc9430SPyun YongHyeon error = mii_mediachg(mii); 2402f6bc9430SPyun YongHyeon NGE_UNLOCK(sc); 2403f6bc9430SPyun YongHyeon 2404f6bc9430SPyun YongHyeon return (error); 2405ce4946daSBill Paul } 2406ce4946daSBill Paul 2407ce4946daSBill Paul /* 2408ce4946daSBill Paul * Report current media status. 2409ce4946daSBill Paul */ 2410eaabec55SAlfred Perlstein static void 2411f6bc9430SPyun YongHyeon nge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 2412ce4946daSBill Paul { 2413ce4946daSBill Paul struct nge_softc *sc; 2414ce4946daSBill Paul struct mii_data *mii; 2415ce4946daSBill Paul 2416ce4946daSBill Paul sc = ifp->if_softc; 2417646abee6SJohn Baldwin NGE_LOCK(sc); 2418ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 2419ce4946daSBill Paul mii_pollstat(mii); 2420f6bc9430SPyun YongHyeon NGE_UNLOCK(sc); 2421ce4946daSBill Paul ifmr->ifm_active = mii->mii_media_active; 2422ce4946daSBill Paul ifmr->ifm_status = mii->mii_media_status; 24231f548804SDoug Ambrisko } 2424ce4946daSBill Paul 2425eaabec55SAlfred Perlstein static int 2426284c81cbSPyun YongHyeon nge_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 2427ce4946daSBill Paul { 2428ce4946daSBill Paul struct nge_softc *sc = ifp->if_softc; 2429ce4946daSBill Paul struct ifreq *ifr = (struct ifreq *) data; 2430ce4946daSBill Paul struct mii_data *mii; 2431f6bc9430SPyun YongHyeon int error = 0, mask; 2432ce4946daSBill Paul 2433ce4946daSBill Paul switch (command) { 2434ce4946daSBill Paul case SIOCSIFMTU: 2435f6bc9430SPyun YongHyeon if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > NGE_JUMBO_MTU) 2436ce4946daSBill Paul error = EINVAL; 2437cb2f755cSBill Paul else { 2438646abee6SJohn Baldwin NGE_LOCK(sc); 2439ce4946daSBill Paul ifp->if_mtu = ifr->ifr_mtu; 2440cb2f755cSBill Paul /* 2441cb2f755cSBill Paul * Workaround: if the MTU is larger than 2442cb2f755cSBill Paul * 8152 (TX FIFO size minus 64 minus 18), turn off 2443cb2f755cSBill Paul * TX checksum offloading. 2444cb2f755cSBill Paul */ 2445a5820ecbSYaroslav Tykhiy if (ifr->ifr_mtu >= 8152) { 2446a5820ecbSYaroslav Tykhiy ifp->if_capenable &= ~IFCAP_TXCSUM; 2447f6bc9430SPyun YongHyeon ifp->if_hwassist &= ~NGE_CSUM_FEATURES; 2448a5820ecbSYaroslav Tykhiy } else { 2449a5820ecbSYaroslav Tykhiy ifp->if_capenable |= IFCAP_TXCSUM; 2450f6bc9430SPyun YongHyeon ifp->if_hwassist |= NGE_CSUM_FEATURES; 2451cb2f755cSBill Paul } 2452646abee6SJohn Baldwin NGE_UNLOCK(sc); 2453f6bc9430SPyun YongHyeon VLAN_CAPABILITIES(ifp); 2454a5820ecbSYaroslav Tykhiy } 2455ce4946daSBill Paul break; 2456ce4946daSBill Paul case SIOCSIFFLAGS: 2457ad6c618bSBill Paul NGE_LOCK(sc); 2458f6bc9430SPyun YongHyeon if ((ifp->if_flags & IFF_UP) != 0) { 2459f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2460f6bc9430SPyun YongHyeon if ((ifp->if_flags ^ sc->nge_if_flags) & 2461f6bc9430SPyun YongHyeon (IFF_PROMISC | IFF_ALLMULTI)) 2462f6bc9430SPyun YongHyeon nge_rxfilter(sc); 2463ce4946daSBill Paul } else { 2464f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_DETACH) == 0) 2465ad6c618bSBill Paul nge_init_locked(sc); 2466ce4946daSBill Paul } 2467ce4946daSBill Paul } else { 2468f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2469ce4946daSBill Paul nge_stop(sc); 2470ce4946daSBill Paul } 2471ce4946daSBill Paul sc->nge_if_flags = ifp->if_flags; 2472ad6c618bSBill Paul NGE_UNLOCK(sc); 2473ce4946daSBill Paul error = 0; 2474ce4946daSBill Paul break; 2475ce4946daSBill Paul case SIOCADDMULTI: 2476ce4946daSBill Paul case SIOCDELMULTI: 2477ad6c618bSBill Paul NGE_LOCK(sc); 2478f6bc9430SPyun YongHyeon nge_rxfilter(sc); 2479ad6c618bSBill Paul NGE_UNLOCK(sc); 2480ce4946daSBill Paul error = 0; 2481ce4946daSBill Paul break; 2482ce4946daSBill Paul case SIOCGIFMEDIA: 2483ce4946daSBill Paul case SIOCSIFMEDIA: 2484ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 2485f6bc9430SPyun YongHyeon error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 2486ce4946daSBill Paul break; 248737f5f239SRuslan Ermilov case SIOCSIFCAP: 248840929967SGleb Smirnoff NGE_LOCK(sc); 2489f6bc9430SPyun YongHyeon mask = ifr->ifr_reqcap ^ ifp->if_capenable; 2490f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING 2491f6bc9430SPyun YongHyeon if ((mask & IFCAP_POLLING) != 0 && 2492f6bc9430SPyun YongHyeon (IFCAP_POLLING & ifp->if_capabilities) != 0) { 2493f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_POLLING; 2494f6bc9430SPyun YongHyeon if ((IFCAP_POLLING & ifp->if_capenable) != 0) { 2495f6bc9430SPyun YongHyeon error = ether_poll_register(nge_poll, ifp); 2496f6bc9430SPyun YongHyeon if (error != 0) { 249740929967SGleb Smirnoff NGE_UNLOCK(sc); 2498f6bc9430SPyun YongHyeon break; 249940929967SGleb Smirnoff } 2500f6bc9430SPyun YongHyeon /* Disable interrupts. */ 2501f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_IER, 0); 2502f6bc9430SPyun YongHyeon } else { 250340929967SGleb Smirnoff error = ether_poll_deregister(ifp); 250440929967SGleb Smirnoff /* Enable interrupts. */ 250540929967SGleb Smirnoff CSR_WRITE_4(sc, NGE_IER, 1); 2506f6bc9430SPyun YongHyeon } 250740929967SGleb Smirnoff } 250840929967SGleb Smirnoff #endif /* DEVICE_POLLING */ 2509f6bc9430SPyun YongHyeon if ((mask & IFCAP_TXCSUM) != 0 && 2510f6bc9430SPyun YongHyeon (IFCAP_TXCSUM & ifp->if_capabilities) != 0) { 2511f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_TXCSUM; 2512f6bc9430SPyun YongHyeon if ((IFCAP_TXCSUM & ifp->if_capenable) != 0) 2513f6bc9430SPyun YongHyeon ifp->if_hwassist |= NGE_CSUM_FEATURES; 2514f6bc9430SPyun YongHyeon else 2515f6bc9430SPyun YongHyeon ifp->if_hwassist &= ~NGE_CSUM_FEATURES; 2516f6bc9430SPyun YongHyeon } 2517f6bc9430SPyun YongHyeon if ((mask & IFCAP_RXCSUM) != 0 && 2518f6bc9430SPyun YongHyeon (IFCAP_RXCSUM & ifp->if_capabilities) != 0) 2519f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_RXCSUM; 2520f6bc9430SPyun YongHyeon 2521f6bc9430SPyun YongHyeon if ((mask & IFCAP_WOL) != 0 && 2522f6bc9430SPyun YongHyeon (ifp->if_capabilities & IFCAP_WOL) != 0) { 2523f6bc9430SPyun YongHyeon if ((mask & IFCAP_WOL_UCAST) != 0) 2524f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_UCAST; 2525f6bc9430SPyun YongHyeon if ((mask & IFCAP_WOL_MCAST) != 0) 2526f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MCAST; 2527f6bc9430SPyun YongHyeon if ((mask & IFCAP_WOL_MAGIC) != 0) 2528f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MAGIC; 2529f6bc9430SPyun YongHyeon } 2530f6bc9430SPyun YongHyeon 2531f6bc9430SPyun YongHyeon if ((mask & IFCAP_VLAN_HWCSUM) != 0 && 2532f6bc9430SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0) 2533f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWCSUM; 2534f6bc9430SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTAGGING) != 0 && 2535f6bc9430SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) { 2536f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 2537f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2538f6bc9430SPyun YongHyeon if ((ifp->if_capenable & 2539f6bc9430SPyun YongHyeon IFCAP_VLAN_HWTAGGING) != 0) 2540f6bc9430SPyun YongHyeon NGE_SETBIT(sc, 2541f6bc9430SPyun YongHyeon NGE_VLAN_IP_RXCTL, 2542f6bc9430SPyun YongHyeon NGE_VIPRXCTL_TAG_STRIP_ENB); 2543f6bc9430SPyun YongHyeon else 2544f6bc9430SPyun YongHyeon NGE_CLRBIT(sc, 2545f6bc9430SPyun YongHyeon NGE_VLAN_IP_RXCTL, 2546f6bc9430SPyun YongHyeon NGE_VIPRXCTL_TAG_STRIP_ENB); 2547f6bc9430SPyun YongHyeon } 2548f6bc9430SPyun YongHyeon } 2549f6bc9430SPyun YongHyeon /* 2550f6bc9430SPyun YongHyeon * Both VLAN hardware tagging and checksum offload is 2551f6bc9430SPyun YongHyeon * required to do checksum offload on VLAN interface. 2552f6bc9430SPyun YongHyeon */ 2553f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) == 0) 2554f6bc9430SPyun YongHyeon ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM; 2555f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 2556f6bc9430SPyun YongHyeon ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM; 2557f6bc9430SPyun YongHyeon NGE_UNLOCK(sc); 2558f6bc9430SPyun YongHyeon VLAN_CAPABILITIES(ifp); 255937f5f239SRuslan Ermilov break; 2560ce4946daSBill Paul default: 2561673d9191SSam Leffler error = ether_ioctl(ifp, command, data); 2562ce4946daSBill Paul break; 2563ce4946daSBill Paul } 2564ce4946daSBill Paul 2565ce4946daSBill Paul return (error); 2566ce4946daSBill Paul } 2567ce4946daSBill Paul 2568eaabec55SAlfred Perlstein static void 2569f6bc9430SPyun YongHyeon nge_watchdog(struct nge_softc *sc) 2570ce4946daSBill Paul { 2571f6bc9430SPyun YongHyeon struct ifnet *ifp; 2572ce4946daSBill Paul 2573f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2574ce4946daSBill Paul 2575f6bc9430SPyun YongHyeon if (sc->nge_watchdog_timer == 0 || --sc->nge_watchdog_timer) 2576f6bc9430SPyun YongHyeon return; 2577f6bc9430SPyun YongHyeon 2578f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 2579ce4946daSBill Paul ifp->if_oerrors++; 25806b9f5c94SGleb Smirnoff if_printf(ifp, "watchdog timeout\n"); 2581ce4946daSBill Paul 258213f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2583ad6c618bSBill Paul nge_init_locked(sc); 2584ce4946daSBill Paul 2585f6bc9430SPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2586ad6c618bSBill Paul nge_start_locked(ifp); 2587f6bc9430SPyun YongHyeon } 2588ad6c618bSBill Paul 2589f6bc9430SPyun YongHyeon static int 2590f6bc9430SPyun YongHyeon nge_stop_mac(struct nge_softc *sc) 2591f6bc9430SPyun YongHyeon { 2592f6bc9430SPyun YongHyeon uint32_t reg; 2593f6bc9430SPyun YongHyeon int i; 2594f6bc9430SPyun YongHyeon 2595f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2596f6bc9430SPyun YongHyeon 2597f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_CSR); 2598f6bc9430SPyun YongHyeon if ((reg & (NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE)) != 0) { 2599f6bc9430SPyun YongHyeon reg &= ~(NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE); 2600f6bc9430SPyun YongHyeon reg |= NGE_CSR_TX_DISABLE | NGE_CSR_RX_DISABLE; 2601f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CSR, reg); 2602f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TIMEOUT; i++) { 2603f6bc9430SPyun YongHyeon DELAY(1); 2604f6bc9430SPyun YongHyeon if ((CSR_READ_4(sc, NGE_CSR) & 2605f6bc9430SPyun YongHyeon (NGE_CSR_RX_ENABLE | NGE_CSR_TX_ENABLE)) == 0) 2606f6bc9430SPyun YongHyeon break; 2607f6bc9430SPyun YongHyeon } 2608f6bc9430SPyun YongHyeon if (i == NGE_TIMEOUT) 2609f6bc9430SPyun YongHyeon return (ETIMEDOUT); 2610f6bc9430SPyun YongHyeon } 2611f6bc9430SPyun YongHyeon 2612f6bc9430SPyun YongHyeon return (0); 2613ce4946daSBill Paul } 2614ce4946daSBill Paul 2615ce4946daSBill Paul /* 2616ce4946daSBill Paul * Stop the adapter and free any mbufs allocated to the 2617ce4946daSBill Paul * RX and TX lists. 2618ce4946daSBill Paul */ 2619eaabec55SAlfred Perlstein static void 2620284c81cbSPyun YongHyeon nge_stop(struct nge_softc *sc) 2621ce4946daSBill Paul { 2622f6bc9430SPyun YongHyeon struct nge_txdesc *txd; 2623f6bc9430SPyun YongHyeon struct nge_rxdesc *rxd; 26242cf2d799SPyun YongHyeon int i; 2625ce4946daSBill Paul struct ifnet *ifp; 2626ce4946daSBill Paul 2627ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 2628fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 2629ce4946daSBill Paul 263013f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2631f6bc9430SPyun YongHyeon sc->nge_flags &= ~NGE_FLAG_LINK; 2632f6bc9430SPyun YongHyeon callout_stop(&sc->nge_stat_ch); 2633f6bc9430SPyun YongHyeon sc->nge_watchdog_timer = 0; 2634f6bc9430SPyun YongHyeon 2635f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_IER, 0); 2636f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_IMR, 0); 2637f6bc9430SPyun YongHyeon if (nge_stop_mac(sc) == ETIMEDOUT) 2638f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 2639f6bc9430SPyun YongHyeon "%s: unable to stop Tx/Rx MAC\n", __func__); 2640f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI, 0); 2641f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO, 0); 2642f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0); 2643f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0); 2644f6bc9430SPyun YongHyeon nge_stats_update(sc); 2645f6bc9430SPyun YongHyeon if (sc->nge_head != NULL) { 2646f6bc9430SPyun YongHyeon m_freem(sc->nge_head); 2647f6bc9430SPyun YongHyeon sc->nge_head = sc->nge_tail = NULL; 2648f6bc9430SPyun YongHyeon } 2649f6bc9430SPyun YongHyeon 2650f6bc9430SPyun YongHyeon /* 2651f6bc9430SPyun YongHyeon * Free RX and TX mbufs still in the queues. 2652f6bc9430SPyun YongHyeon */ 2653f6bc9430SPyun YongHyeon for (i = 0; i < NGE_RX_RING_CNT; i++) { 2654f6bc9430SPyun YongHyeon rxd = &sc->nge_cdata.nge_rxdesc[i]; 2655f6bc9430SPyun YongHyeon if (rxd->rx_m != NULL) { 2656f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, 2657f6bc9430SPyun YongHyeon rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 2658f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_rx_tag, 2659f6bc9430SPyun YongHyeon rxd->rx_dmamap); 2660f6bc9430SPyun YongHyeon m_freem(rxd->rx_m); 2661f6bc9430SPyun YongHyeon rxd->rx_m = NULL; 2662f6bc9430SPyun YongHyeon } 2663f6bc9430SPyun YongHyeon } 2664f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TX_RING_CNT; i++) { 2665f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[i]; 2666f6bc9430SPyun YongHyeon if (txd->tx_m != NULL) { 2667f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, 2668f6bc9430SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 2669f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, 2670f6bc9430SPyun YongHyeon txd->tx_dmamap); 2671f6bc9430SPyun YongHyeon m_freem(txd->tx_m); 2672f6bc9430SPyun YongHyeon txd->tx_m = NULL; 2673f6bc9430SPyun YongHyeon } 2674f6bc9430SPyun YongHyeon } 2675f6bc9430SPyun YongHyeon } 2676f6bc9430SPyun YongHyeon 2677f6bc9430SPyun YongHyeon /* 2678f6bc9430SPyun YongHyeon * Before setting WOL bits, caller should have stopped Receiver. 2679f6bc9430SPyun YongHyeon */ 2680f6bc9430SPyun YongHyeon static void 2681f6bc9430SPyun YongHyeon nge_wol(struct nge_softc *sc) 2682f6bc9430SPyun YongHyeon { 2683f6bc9430SPyun YongHyeon struct ifnet *ifp; 2684f6bc9430SPyun YongHyeon uint32_t reg; 2685f6bc9430SPyun YongHyeon uint16_t pmstat; 2686f6bc9430SPyun YongHyeon int pmc; 2687f6bc9430SPyun YongHyeon 2688f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2689f6bc9430SPyun YongHyeon 26903b0a4aefSJohn Baldwin if (pci_find_cap(sc->nge_dev, PCIY_PMG, &pmc) != 0) 2691f6bc9430SPyun YongHyeon return; 2692f6bc9430SPyun YongHyeon 2693f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 2694f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) == 0) { 2695f6bc9430SPyun YongHyeon /* Disable WOL & disconnect CLKRUN to save power. */ 2696f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_WOLCSR, 0); 2697f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CLKRUN, 0); 2698f6bc9430SPyun YongHyeon } else { 2699f6bc9430SPyun YongHyeon if (nge_stop_mac(sc) == ETIMEDOUT) 2700f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 2701f6bc9430SPyun YongHyeon "%s: unable to stop Tx/Rx MAC\n", __func__); 2702f6bc9430SPyun YongHyeon /* 2703f6bc9430SPyun YongHyeon * Make sure wake frames will be buffered in the Rx FIFO. 2704f6bc9430SPyun YongHyeon * (i.e. Silent Rx mode.) 2705f6bc9430SPyun YongHyeon */ 2706f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0); 2707f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_RX_LISTPTR_HI); 2708f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0); 2709f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_RX_LISTPTR_LO); 2710f6bc9430SPyun YongHyeon /* Enable Rx again. */ 2711f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 2712f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_CSR); 2713f6bc9430SPyun YongHyeon 2714f6bc9430SPyun YongHyeon /* Configure WOL events. */ 2715f6bc9430SPyun YongHyeon reg = 0; 2716f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0) 2717f6bc9430SPyun YongHyeon reg |= NGE_WOLCSR_WAKE_ON_UNICAST; 2718f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0) 2719f6bc9430SPyun YongHyeon reg |= NGE_WOLCSR_WAKE_ON_MULTICAST; 2720f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) 2721f6bc9430SPyun YongHyeon reg |= NGE_WOLCSR_WAKE_ON_MAGICPKT; 2722f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_WOLCSR, reg); 2723f6bc9430SPyun YongHyeon 2724f6bc9430SPyun YongHyeon /* Activate CLKRUN. */ 2725f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_CLKRUN); 2726f6bc9430SPyun YongHyeon reg |= NGE_CLKRUN_PMEENB | NGE_CLNRUN_CLKRUN_ENB; 2727f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CLKRUN, reg); 2728f6bc9430SPyun YongHyeon } 2729f6bc9430SPyun YongHyeon 2730f6bc9430SPyun YongHyeon /* Request PME. */ 2731f6bc9430SPyun YongHyeon pmstat = pci_read_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, 2); 2732f6bc9430SPyun YongHyeon pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE); 2733f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 2734f6bc9430SPyun YongHyeon pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE; 2735f6bc9430SPyun YongHyeon pci_write_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, pmstat, 2); 2736ce4946daSBill Paul } 2737ce4946daSBill Paul 2738ce4946daSBill Paul /* 2739ce4946daSBill Paul * Stop all chip I/O so that the kernel's probe routines don't 2740ce4946daSBill Paul * get confused by errant DMAs when rebooting. 2741ce4946daSBill Paul */ 27426a087a87SPyun YongHyeon static int 2743284c81cbSPyun YongHyeon nge_shutdown(device_t dev) 2744ce4946daSBill Paul { 2745f6bc9430SPyun YongHyeon 2746f6bc9430SPyun YongHyeon return (nge_suspend(dev)); 2747f6bc9430SPyun YongHyeon } 2748f6bc9430SPyun YongHyeon 2749f6bc9430SPyun YongHyeon static int 2750f6bc9430SPyun YongHyeon nge_suspend(device_t dev) 2751f6bc9430SPyun YongHyeon { 2752ce4946daSBill Paul struct nge_softc *sc; 2753ce4946daSBill Paul 2754ce4946daSBill Paul sc = device_get_softc(dev); 2755ce4946daSBill Paul 2756ad6c618bSBill Paul NGE_LOCK(sc); 2757ce4946daSBill Paul nge_stop(sc); 2758f6bc9430SPyun YongHyeon nge_wol(sc); 2759f6bc9430SPyun YongHyeon sc->nge_flags |= NGE_FLAG_SUSPENDED; 2760ad6c618bSBill Paul NGE_UNLOCK(sc); 2761ce4946daSBill Paul 27626a087a87SPyun YongHyeon return (0); 2763ce4946daSBill Paul } 2764f6bc9430SPyun YongHyeon 2765f6bc9430SPyun YongHyeon static int 2766f6bc9430SPyun YongHyeon nge_resume(device_t dev) 2767f6bc9430SPyun YongHyeon { 2768f6bc9430SPyun YongHyeon struct nge_softc *sc; 2769f6bc9430SPyun YongHyeon struct ifnet *ifp; 2770f6bc9430SPyun YongHyeon uint16_t pmstat; 2771f6bc9430SPyun YongHyeon int pmc; 2772f6bc9430SPyun YongHyeon 2773f6bc9430SPyun YongHyeon sc = device_get_softc(dev); 2774f6bc9430SPyun YongHyeon 2775f6bc9430SPyun YongHyeon NGE_LOCK(sc); 2776f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 27773b0a4aefSJohn Baldwin if (pci_find_cap(sc->nge_dev, PCIY_PMG, &pmc) == 0) { 2778f6bc9430SPyun YongHyeon /* Disable PME and clear PME status. */ 2779f6bc9430SPyun YongHyeon pmstat = pci_read_config(sc->nge_dev, 2780f6bc9430SPyun YongHyeon pmc + PCIR_POWER_STATUS, 2); 2781f6bc9430SPyun YongHyeon if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) { 2782f6bc9430SPyun YongHyeon pmstat &= ~PCIM_PSTAT_PMEENABLE; 2783f6bc9430SPyun YongHyeon pci_write_config(sc->nge_dev, 2784f6bc9430SPyun YongHyeon pmc + PCIR_POWER_STATUS, pmstat, 2); 2785f6bc9430SPyun YongHyeon } 2786f6bc9430SPyun YongHyeon } 2787f6bc9430SPyun YongHyeon if (ifp->if_flags & IFF_UP) { 2788f6bc9430SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2789f6bc9430SPyun YongHyeon nge_init_locked(sc); 2790f6bc9430SPyun YongHyeon } 2791f6bc9430SPyun YongHyeon 2792f6bc9430SPyun YongHyeon sc->nge_flags &= ~NGE_FLAG_SUSPENDED; 2793f6bc9430SPyun YongHyeon NGE_UNLOCK(sc); 2794f6bc9430SPyun YongHyeon 2795f6bc9430SPyun YongHyeon return (0); 2796f6bc9430SPyun YongHyeon } 2797f6bc9430SPyun YongHyeon 2798f6bc9430SPyun YongHyeon #define NGE_SYSCTL_STAT_ADD32(c, h, n, p, d) \ 2799f6bc9430SPyun YongHyeon SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d) 2800f6bc9430SPyun YongHyeon 2801f6bc9430SPyun YongHyeon static void 2802f6bc9430SPyun YongHyeon nge_sysctl_node(struct nge_softc *sc) 2803f6bc9430SPyun YongHyeon { 2804f6bc9430SPyun YongHyeon struct sysctl_ctx_list *ctx; 2805f6bc9430SPyun YongHyeon struct sysctl_oid_list *child, *parent; 2806f6bc9430SPyun YongHyeon struct sysctl_oid *tree; 2807f6bc9430SPyun YongHyeon struct nge_stats *stats; 2808f6bc9430SPyun YongHyeon int error; 2809f6bc9430SPyun YongHyeon 2810f6bc9430SPyun YongHyeon ctx = device_get_sysctl_ctx(sc->nge_dev); 2811f6bc9430SPyun YongHyeon child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->nge_dev)); 2812f6bc9430SPyun YongHyeon SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_holdoff", 2813f6bc9430SPyun YongHyeon CTLTYPE_INT | CTLFLAG_RW, &sc->nge_int_holdoff, 0, 2814f6bc9430SPyun YongHyeon sysctl_hw_nge_int_holdoff, "I", "NGE interrupt moderation"); 2815f6bc9430SPyun YongHyeon /* Pull in device tunables. */ 2816f6bc9430SPyun YongHyeon sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT; 2817f6bc9430SPyun YongHyeon error = resource_int_value(device_get_name(sc->nge_dev), 2818f6bc9430SPyun YongHyeon device_get_unit(sc->nge_dev), "int_holdoff", &sc->nge_int_holdoff); 2819f6bc9430SPyun YongHyeon if (error == 0) { 2820f6bc9430SPyun YongHyeon if (sc->nge_int_holdoff < NGE_INT_HOLDOFF_MIN || 2821f6bc9430SPyun YongHyeon sc->nge_int_holdoff > NGE_INT_HOLDOFF_MAX ) { 2822f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 2823f6bc9430SPyun YongHyeon "int_holdoff value out of range; " 2824f6bc9430SPyun YongHyeon "using default: %d(%d us)\n", 2825f6bc9430SPyun YongHyeon NGE_INT_HOLDOFF_DEFAULT, 2826f6bc9430SPyun YongHyeon NGE_INT_HOLDOFF_DEFAULT * 100); 2827f6bc9430SPyun YongHyeon sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT; 2828f6bc9430SPyun YongHyeon } 2829f6bc9430SPyun YongHyeon } 2830f6bc9430SPyun YongHyeon 2831f6bc9430SPyun YongHyeon stats = &sc->nge_stats; 2832f6bc9430SPyun YongHyeon tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD, 2833f6bc9430SPyun YongHyeon NULL, "NGE statistics"); 2834f6bc9430SPyun YongHyeon parent = SYSCTL_CHILDREN(tree); 2835f6bc9430SPyun YongHyeon 2836f6bc9430SPyun YongHyeon /* Rx statistics. */ 2837f6bc9430SPyun YongHyeon tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD, 2838f6bc9430SPyun YongHyeon NULL, "Rx MAC statistics"); 2839f6bc9430SPyun YongHyeon child = SYSCTL_CHILDREN(tree); 2840f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_errs", 2841f6bc9430SPyun YongHyeon &stats->rx_pkts_errs, 2842f6bc9430SPyun YongHyeon "Packet errors including both wire errors and FIFO overruns"); 2843f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "crc_errs", 2844f6bc9430SPyun YongHyeon &stats->rx_crc_errs, "CRC errors"); 2845f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows", 2846f6bc9430SPyun YongHyeon &stats->rx_fifo_oflows, "FIFO overflows"); 2847f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "align_errs", 2848f6bc9430SPyun YongHyeon &stats->rx_align_errs, "Frame alignment errors"); 2849f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "sym_errs", 2850f6bc9430SPyun YongHyeon &stats->rx_sym_errs, "One or more symbol errors"); 2851f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_jumbos", 2852f6bc9430SPyun YongHyeon &stats->rx_pkts_jumbos, 2853f6bc9430SPyun YongHyeon "Packets received with length greater than 1518 bytes"); 2854f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "len_errs", 2855f6bc9430SPyun YongHyeon &stats->rx_len_errs, "In Range Length errors"); 2856f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "unctl_frames", 2857f6bc9430SPyun YongHyeon &stats->rx_unctl_frames, "Control frames with unsupported opcode"); 2858f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "pause", 2859f6bc9430SPyun YongHyeon &stats->rx_pause, "Pause frames"); 2860f6bc9430SPyun YongHyeon 2861f6bc9430SPyun YongHyeon /* Tx statistics. */ 2862f6bc9430SPyun YongHyeon tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD, 2863f6bc9430SPyun YongHyeon NULL, "Tx MAC statistics"); 2864f6bc9430SPyun YongHyeon child = SYSCTL_CHILDREN(tree); 2865f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "pause", 2866f6bc9430SPyun YongHyeon &stats->tx_pause, "Pause frames"); 2867f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "seq_errs", 2868f6bc9430SPyun YongHyeon &stats->tx_seq_errs, 2869f6bc9430SPyun YongHyeon "Loss of collision heartbeat during transmission"); 2870f6bc9430SPyun YongHyeon } 2871f6bc9430SPyun YongHyeon 2872f6bc9430SPyun YongHyeon #undef NGE_SYSCTL_STAT_ADD32 2873f6bc9430SPyun YongHyeon 2874f6bc9430SPyun YongHyeon static int 2875f6bc9430SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 2876f6bc9430SPyun YongHyeon { 2877f6bc9430SPyun YongHyeon int error, value; 2878f6bc9430SPyun YongHyeon 2879f6bc9430SPyun YongHyeon if (arg1 == NULL) 2880f6bc9430SPyun YongHyeon return (EINVAL); 2881f6bc9430SPyun YongHyeon value = *(int *)arg1; 2882f6bc9430SPyun YongHyeon error = sysctl_handle_int(oidp, &value, 0, req); 2883f6bc9430SPyun YongHyeon if (error != 0 || req->newptr == NULL) 2884f6bc9430SPyun YongHyeon return (error); 2885f6bc9430SPyun YongHyeon if (value < low || value > high) 2886f6bc9430SPyun YongHyeon return (EINVAL); 2887f6bc9430SPyun YongHyeon *(int *)arg1 = value; 2888f6bc9430SPyun YongHyeon 2889f6bc9430SPyun YongHyeon return (0); 2890f6bc9430SPyun YongHyeon } 2891f6bc9430SPyun YongHyeon 2892f6bc9430SPyun YongHyeon static int 2893f6bc9430SPyun YongHyeon sysctl_hw_nge_int_holdoff(SYSCTL_HANDLER_ARGS) 2894f6bc9430SPyun YongHyeon { 2895f6bc9430SPyun YongHyeon 2896f6bc9430SPyun YongHyeon return (sysctl_int_range(oidp, arg1, arg2, req, NGE_INT_HOLDOFF_MIN, 2897f6bc9430SPyun YongHyeon NGE_INT_HOLDOFF_MAX)); 2898f6bc9430SPyun YongHyeon } 2899