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 160e51a25f8SAlfred Perlstein static void 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 */ 86613b203d0SRobert Watson IF_ADDR_LOCK(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 } 87813b203d0SRobert Watson IF_ADDR_UNLOCK(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 */ 1064f6bc9430SPyun YongHyeon if (pci_find_extcap(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 */ 1082f6bc9430SPyun YongHyeon error = mii_phy_probe(dev, &sc->nge_miibus, nge_mediachange, 1083f6bc9430SPyun YongHyeon nge_mediastatus); 1084f6bc9430SPyun YongHyeon if (error != 0) { 1085f6bc9430SPyun YongHyeon device_printf(dev, "no PHY found!\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 */ 1578eaabec55SAlfred Perlstein static void 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; 1585f6bc9430SPyun YongHyeon int cons, prog, 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; 1592ce4946daSBill Paul 1593f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag, 1594f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map, 1595f6bc9430SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1596ce4946daSBill Paul 1597f6bc9430SPyun YongHyeon for (prog = 0; prog < NGE_RX_RING_CNT && 1598f6bc9430SPyun YongHyeon (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0; 1599f6bc9430SPyun YongHyeon NGE_INC(cons, NGE_RX_RING_CNT)) { 1600196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING 160140929967SGleb Smirnoff if (ifp->if_capenable & IFCAP_POLLING) { 1602196c0df6SHidetoshi Shimokawa if (sc->rxcycles <= 0) 1603196c0df6SHidetoshi Shimokawa break; 1604196c0df6SHidetoshi Shimokawa sc->rxcycles--; 1605196c0df6SHidetoshi Shimokawa } 160640929967SGleb Smirnoff #endif 1607f6bc9430SPyun YongHyeon cur_rx = &sc->nge_rdata.nge_rx_ring[cons]; 1608f6bc9430SPyun YongHyeon cmdsts = le32toh(cur_rx->nge_cmdsts); 1609f6bc9430SPyun YongHyeon extsts = le32toh(cur_rx->nge_extsts); 1610f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_OWN) == 0) 1611f6bc9430SPyun YongHyeon break; 1612f6bc9430SPyun YongHyeon prog++; 1613f6bc9430SPyun YongHyeon rxd = &sc->nge_cdata.nge_rxdesc[cons]; 1614f6bc9430SPyun YongHyeon m = rxd->rx_m; 1615f6bc9430SPyun YongHyeon total_len = cmdsts & NGE_CMDSTS_BUFLEN; 1616196c0df6SHidetoshi Shimokawa 1617f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_MORE) != 0) { 1618f6bc9430SPyun YongHyeon if (nge_newbuf(sc, cons) != 0) { 1619f6bc9430SPyun YongHyeon ifp->if_iqdrops++; 1620f6bc9430SPyun YongHyeon if (sc->nge_head != NULL) { 1621f6bc9430SPyun YongHyeon m_freem(sc->nge_head); 1622f6bc9430SPyun YongHyeon sc->nge_head = sc->nge_tail = NULL; 1623f6bc9430SPyun YongHyeon } 1624f6bc9430SPyun YongHyeon nge_discard_rxbuf(sc, cons); 1625f6bc9430SPyun YongHyeon continue; 1626f6bc9430SPyun YongHyeon } 1627ad6c618bSBill Paul m->m_len = total_len; 1628ad6c618bSBill Paul if (sc->nge_head == NULL) { 1629ad6c618bSBill Paul m->m_pkthdr.len = total_len; 1630ad6c618bSBill Paul sc->nge_head = sc->nge_tail = m; 1631ad6c618bSBill Paul } else { 1632ad6c618bSBill Paul m->m_flags &= ~M_PKTHDR; 1633ad6c618bSBill Paul sc->nge_head->m_pkthdr.len += total_len; 1634ad6c618bSBill Paul sc->nge_tail->m_next = m; 1635ad6c618bSBill Paul sc->nge_tail = m; 1636ad6c618bSBill Paul } 1637ad6c618bSBill Paul continue; 1638ad6c618bSBill Paul } 1639ad6c618bSBill Paul 1640ce4946daSBill Paul /* 1641ce4946daSBill Paul * If an error occurs, update stats, clear the 1642ce4946daSBill Paul * status word and leave the mbuf cluster in place: 1643ce4946daSBill Paul * it should simply get re-used next time this descriptor 1644ce4946daSBill Paul * comes up in the ring. 1645ce4946daSBill Paul */ 1646f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) { 1647f6bc9430SPyun YongHyeon if ((cmdsts & NGE_RXSTAT_RUNT) && 1648f6bc9430SPyun YongHyeon total_len >= (ETHER_MIN_LEN - ETHER_CRC_LEN - 4)) { 1649f6bc9430SPyun YongHyeon /* 1650f6bc9430SPyun YongHyeon * Work-around hardware bug, accept runt frames 1651f6bc9430SPyun YongHyeon * if its length is larger than or equal to 56. 1652f6bc9430SPyun YongHyeon */ 1653f6bc9430SPyun YongHyeon } else { 1654f6bc9430SPyun YongHyeon /* 1655f6bc9430SPyun YongHyeon * Input error counters are updated by hardware. 1656f6bc9430SPyun YongHyeon */ 1657ad6c618bSBill Paul if (sc->nge_head != NULL) { 1658ad6c618bSBill Paul m_freem(sc->nge_head); 1659ad6c618bSBill Paul sc->nge_head = sc->nge_tail = NULL; 1660ad6c618bSBill Paul } 1661f6bc9430SPyun YongHyeon nge_discard_rxbuf(sc, cons); 1662ce4946daSBill Paul continue; 1663ce4946daSBill Paul } 1664f6bc9430SPyun YongHyeon } 1665ce4946daSBill Paul 1666ad6c618bSBill Paul /* Try conjure up a replacement mbuf. */ 1667ad6c618bSBill Paul 1668f6bc9430SPyun YongHyeon if (nge_newbuf(sc, cons) != 0) { 1669f6bc9430SPyun YongHyeon ifp->if_iqdrops++; 1670ad6c618bSBill Paul if (sc->nge_head != NULL) { 1671ad6c618bSBill Paul m_freem(sc->nge_head); 1672ad6c618bSBill Paul sc->nge_head = sc->nge_tail = NULL; 1673ad6c618bSBill Paul } 1674f6bc9430SPyun YongHyeon nge_discard_rxbuf(sc, cons); 1675ad6c618bSBill Paul continue; 1676ad6c618bSBill Paul } 1677ad6c618bSBill Paul 1678f6bc9430SPyun YongHyeon /* Chain received mbufs. */ 1679ad6c618bSBill Paul if (sc->nge_head != NULL) { 1680ad6c618bSBill Paul m->m_len = total_len; 1681ad6c618bSBill Paul m->m_flags &= ~M_PKTHDR; 1682ad6c618bSBill Paul sc->nge_tail->m_next = m; 1683ad6c618bSBill Paul m = sc->nge_head; 1684ad6c618bSBill Paul m->m_pkthdr.len += total_len; 1685ad6c618bSBill Paul sc->nge_head = sc->nge_tail = NULL; 1686ad6c618bSBill Paul } else 1687ad6c618bSBill Paul m->m_pkthdr.len = m->m_len = total_len; 1688ad6c618bSBill Paul 1689ce4946daSBill Paul /* 1690ce4946daSBill Paul * Ok. NatSemi really screwed up here. This is the 1691ce4946daSBill Paul * only gigE chip I know of with alignment constraints 1692ce4946daSBill Paul * on receive buffers. RX buffers must be 64-bit aligned. 1693ce4946daSBill Paul */ 1694962315f6SBill Paul /* 1695962315f6SBill Paul * By popular demand, ignore the alignment problems 1696f6bc9430SPyun YongHyeon * on the non-strict alignment platform. The performance hit 1697962315f6SBill Paul * incurred due to unaligned accesses is much smaller 1698962315f6SBill Paul * than the hit produced by forcing buffer copies all 1699962315f6SBill Paul * the time, especially with jumbo frames. We still 1700962315f6SBill Paul * need to fix up the alignment everywhere else though. 1701962315f6SBill Paul */ 1702f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT 1703ad6c618bSBill Paul nge_fixup_rx(m); 1704962315f6SBill Paul #endif 1705ad6c618bSBill Paul m->m_pkthdr.rcvif = ifp; 1706f6bc9430SPyun YongHyeon ifp->if_ipackets++; 1707ce4946daSBill Paul 1708f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) { 1709ce4946daSBill Paul /* Do IP checksum checking. */ 1710f6bc9430SPyun YongHyeon if ((extsts & NGE_RXEXTSTS_IPPKT) != 0) 1711ce4946daSBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED; 1712f6bc9430SPyun YongHyeon if ((extsts & NGE_RXEXTSTS_IPCSUMERR) == 0) 171301702579SBill Paul m->m_pkthdr.csum_flags |= CSUM_IP_VALID; 17142195de46SBill Paul if ((extsts & NGE_RXEXTSTS_TCPPKT && 17152195de46SBill Paul !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) || 17162195de46SBill Paul (extsts & NGE_RXEXTSTS_UDPPKT && 17172195de46SBill Paul !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) { 17182195de46SBill Paul m->m_pkthdr.csum_flags |= 17192195de46SBill Paul CSUM_DATA_VALID | CSUM_PSEUDO_HDR; 1720c9215605SBill Paul m->m_pkthdr.csum_data = 0xffff; 17212195de46SBill Paul } 1722f6bc9430SPyun YongHyeon } 1723ce4946daSBill Paul 1724ce4946daSBill Paul /* 1725ce4946daSBill Paul * If we received a packet with a vlan tag, pass it 1726ce4946daSBill Paul * to vlan_input() instead of ether_input(). 1727ce4946daSBill Paul */ 1728f6bc9430SPyun YongHyeon if ((extsts & NGE_RXEXTSTS_VLANPKT) != 0 && 1729f6bc9430SPyun YongHyeon (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) { 173078ba57b9SAndre Oppermann m->m_pkthdr.ether_vtag = 1731f6bc9430SPyun YongHyeon bswap16(extsts & NGE_RXEXTSTS_VTCI); 173278ba57b9SAndre Oppermann m->m_flags |= M_VLANTAG; 1733ce4946daSBill Paul } 1734ad6c618bSBill Paul NGE_UNLOCK(sc); 1735673d9191SSam Leffler (*ifp->if_input)(ifp, m); 1736ad6c618bSBill Paul NGE_LOCK(sc); 1737ce4946daSBill Paul } 1738ce4946daSBill Paul 1739f6bc9430SPyun YongHyeon if (prog > 0) { 1740f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_cons = cons; 1741f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag, 1742f6bc9430SPyun YongHyeon sc->nge_cdata.nge_rx_ring_map, 1743f6bc9430SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 1744f6bc9430SPyun YongHyeon } 1745ce4946daSBill Paul } 1746ce4946daSBill Paul 1747ce4946daSBill Paul /* 1748ce4946daSBill Paul * A frame was downloaded to the chip. It's safe for us to clean up 1749ce4946daSBill Paul * the list buffers. 1750ce4946daSBill Paul */ 1751eaabec55SAlfred Perlstein static void 1752284c81cbSPyun YongHyeon nge_txeof(struct nge_softc *sc) 1753ce4946daSBill Paul { 17541e73ec7dSRuslan Ermilov struct nge_desc *cur_tx; 1755f6bc9430SPyun YongHyeon struct nge_txdesc *txd; 1756ce4946daSBill Paul struct ifnet *ifp; 1757f6bc9430SPyun YongHyeon uint32_t cmdsts; 1758f6bc9430SPyun YongHyeon int cons, prod; 1759ce4946daSBill Paul 1760ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 1761fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 1762ce4946daSBill Paul 1763f6bc9430SPyun YongHyeon cons = sc->nge_cdata.nge_tx_cons; 1764f6bc9430SPyun YongHyeon prod = sc->nge_cdata.nge_tx_prod; 1765f6bc9430SPyun YongHyeon if (cons == prod) 1766f6bc9430SPyun YongHyeon return; 1767f6bc9430SPyun YongHyeon 1768f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag, 1769f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map, 1770f6bc9430SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 1771f6bc9430SPyun YongHyeon 1772ce4946daSBill Paul /* 1773ce4946daSBill Paul * Go through our tx list and free mbufs for those 1774ce4946daSBill Paul * frames that have been transmitted. 1775ce4946daSBill Paul */ 1776f6bc9430SPyun YongHyeon for (; cons != prod; NGE_INC(cons, NGE_TX_RING_CNT)) { 1777f6bc9430SPyun YongHyeon cur_tx = &sc->nge_rdata.nge_tx_ring[cons]; 1778f6bc9430SPyun YongHyeon cmdsts = le32toh(cur_tx->nge_cmdsts); 1779f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_OWN) != 0) 1780ce4946daSBill Paul break; 1781ce4946daSBill Paul sc->nge_cdata.nge_tx_cnt--; 178213f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1783f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_MORE) != 0) 1784f6bc9430SPyun YongHyeon continue; 1785f6bc9430SPyun YongHyeon 1786f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[cons]; 1787f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap, 1788f6bc9430SPyun YongHyeon BUS_DMASYNC_POSTWRITE); 1789f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap); 1790f6bc9430SPyun YongHyeon if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) { 1791f6bc9430SPyun YongHyeon ifp->if_oerrors++; 1792f6bc9430SPyun YongHyeon if ((cmdsts & NGE_TXSTAT_EXCESSCOLLS) != 0) 1793f6bc9430SPyun YongHyeon ifp->if_collisions++; 1794f6bc9430SPyun YongHyeon if ((cmdsts & NGE_TXSTAT_OUTOFWINCOLL) != 0) 1795f6bc9430SPyun YongHyeon ifp->if_collisions++; 1796f6bc9430SPyun YongHyeon } else 1797f6bc9430SPyun YongHyeon ifp->if_opackets++; 1798f6bc9430SPyun YongHyeon 1799f6bc9430SPyun YongHyeon ifp->if_collisions += (cmdsts & NGE_TXSTAT_COLLCNT) >> 16; 1800f6bc9430SPyun YongHyeon KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!\n", 1801f6bc9430SPyun YongHyeon __func__)); 1802f6bc9430SPyun YongHyeon m_freem(txd->tx_m); 1803f6bc9430SPyun YongHyeon txd->tx_m = NULL; 1804ce4946daSBill Paul } 1805ce4946daSBill Paul 1806f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_cons = cons; 1807f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_tx_cnt == 0) 1808f6bc9430SPyun YongHyeon sc->nge_watchdog_timer = 0; 1809ce4946daSBill Paul } 1810ce4946daSBill Paul 1811eaabec55SAlfred Perlstein static void 1812284c81cbSPyun YongHyeon nge_tick(void *xsc) 1813ce4946daSBill Paul { 1814ce4946daSBill Paul struct nge_softc *sc; 1815ad6c618bSBill Paul struct mii_data *mii; 1816ad6c618bSBill Paul 1817646abee6SJohn Baldwin sc = xsc; 1818ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 1819ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 1820ce4946daSBill Paul mii_tick(mii); 1821f6bc9430SPyun YongHyeon /* 1822f6bc9430SPyun YongHyeon * For PHYs that does not reset established link, it is 1823f6bc9430SPyun YongHyeon * necessary to check whether driver still have a valid 1824f6bc9430SPyun YongHyeon * link(e.g link state change callback is not called). 1825f6bc9430SPyun YongHyeon * Otherwise, driver think it lost link because driver 1826f6bc9430SPyun YongHyeon * initialization routine clears link state flag. 1827f6bc9430SPyun YongHyeon */ 1828f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_LINK) == 0) 1829f6bc9430SPyun YongHyeon nge_miibus_statchg(sc->nge_dev); 1830f6bc9430SPyun YongHyeon nge_stats_update(sc); 1831f6bc9430SPyun YongHyeon nge_watchdog(sc); 1832ad6c618bSBill Paul callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc); 1833ce4946daSBill Paul } 1834ce4946daSBill Paul 1835f6bc9430SPyun YongHyeon static void 1836f6bc9430SPyun YongHyeon nge_stats_update(struct nge_softc *sc) 1837f6bc9430SPyun YongHyeon { 1838f6bc9430SPyun YongHyeon struct ifnet *ifp; 1839f6bc9430SPyun YongHyeon struct nge_stats now, *stats, *nstats; 1840f6bc9430SPyun YongHyeon 1841f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 1842f6bc9430SPyun YongHyeon 1843f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 1844f6bc9430SPyun YongHyeon stats = &now; 1845f6bc9430SPyun YongHyeon stats->rx_pkts_errs = 1846f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRPKT) & 0xFFFF; 1847f6bc9430SPyun YongHyeon stats->rx_crc_errs = 1848f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRFCS) & 0xFFFF; 1849f6bc9430SPyun YongHyeon stats->rx_fifo_oflows = 1850f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRMISSEDPKT) & 0xFFFF; 1851f6bc9430SPyun YongHyeon stats->rx_align_errs = 1852f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRALIGN) & 0xFFFF; 1853f6bc9430SPyun YongHyeon stats->rx_sym_errs = 1854f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRSYM) & 0xFFFF; 1855f6bc9430SPyun YongHyeon stats->rx_pkts_jumbos = 1856f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRGIANT) & 0xFFFF; 1857f6bc9430SPyun YongHyeon stats->rx_len_errs = 1858f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXERRRANGLEN) & 0xFFFF; 1859f6bc9430SPyun YongHyeon stats->rx_unctl_frames = 1860f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXBADOPCODE) & 0xFFFF; 1861f6bc9430SPyun YongHyeon stats->rx_pause = 1862f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_RXPAUSEPKTS) & 0xFFFF; 1863f6bc9430SPyun YongHyeon stats->tx_pause = 1864f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_TXPAUSEPKTS) & 0xFFFF; 1865f6bc9430SPyun YongHyeon stats->tx_seq_errs = 1866f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_MIB_TXERRSQE) & 0xFF; 1867f6bc9430SPyun YongHyeon 1868f6bc9430SPyun YongHyeon /* 1869f6bc9430SPyun YongHyeon * Since we've accept errored frames exclude Rx length errors. 1870f6bc9430SPyun YongHyeon */ 1871f6bc9430SPyun YongHyeon ifp->if_ierrors += stats->rx_pkts_errs + stats->rx_crc_errs + 1872f6bc9430SPyun YongHyeon stats->rx_fifo_oflows + stats->rx_sym_errs; 1873f6bc9430SPyun YongHyeon 1874f6bc9430SPyun YongHyeon nstats = &sc->nge_stats; 1875f6bc9430SPyun YongHyeon nstats->rx_pkts_errs += stats->rx_pkts_errs; 1876f6bc9430SPyun YongHyeon nstats->rx_crc_errs += stats->rx_crc_errs; 1877f6bc9430SPyun YongHyeon nstats->rx_fifo_oflows += stats->rx_fifo_oflows; 1878f6bc9430SPyun YongHyeon nstats->rx_align_errs += stats->rx_align_errs; 1879f6bc9430SPyun YongHyeon nstats->rx_sym_errs += stats->rx_sym_errs; 1880f6bc9430SPyun YongHyeon nstats->rx_pkts_jumbos += stats->rx_pkts_jumbos; 1881f6bc9430SPyun YongHyeon nstats->rx_len_errs += stats->rx_len_errs; 1882f6bc9430SPyun YongHyeon nstats->rx_unctl_frames += stats->rx_unctl_frames; 1883f6bc9430SPyun YongHyeon nstats->rx_pause += stats->rx_pause; 1884f6bc9430SPyun YongHyeon nstats->tx_pause += stats->tx_pause; 1885f6bc9430SPyun YongHyeon nstats->tx_seq_errs += stats->tx_seq_errs; 1886f6bc9430SPyun YongHyeon } 1887f6bc9430SPyun YongHyeon 1888196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING 1889196c0df6SHidetoshi Shimokawa static poll_handler_t nge_poll; 1890196c0df6SHidetoshi Shimokawa 1891196c0df6SHidetoshi Shimokawa static void 1892196c0df6SHidetoshi Shimokawa nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count) 1893196c0df6SHidetoshi Shimokawa { 1894f6bc9430SPyun YongHyeon struct nge_softc *sc; 1895f6bc9430SPyun YongHyeon 1896f6bc9430SPyun YongHyeon sc = ifp->if_softc; 1897196c0df6SHidetoshi Shimokawa 1898ad6c618bSBill Paul NGE_LOCK(sc); 1899f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 1900ad6c618bSBill Paul NGE_UNLOCK(sc); 1901196c0df6SHidetoshi Shimokawa return; 1902196c0df6SHidetoshi Shimokawa } 1903196c0df6SHidetoshi Shimokawa 1904196c0df6SHidetoshi Shimokawa /* 1905196c0df6SHidetoshi Shimokawa * On the nge, reading the status register also clears it. 1906196c0df6SHidetoshi Shimokawa * So before returning to intr mode we must make sure that all 1907196c0df6SHidetoshi Shimokawa * possible pending sources of interrupts have been served. 1908196c0df6SHidetoshi Shimokawa * In practice this means run to completion the *eof routines, 1909f6bc9430SPyun YongHyeon * and then call the interrupt routine. 1910196c0df6SHidetoshi Shimokawa */ 1911196c0df6SHidetoshi Shimokawa sc->rxcycles = count; 1912196c0df6SHidetoshi Shimokawa nge_rxeof(sc); 1913196c0df6SHidetoshi Shimokawa nge_txeof(sc); 1914f6bc9430SPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1915ad6c618bSBill Paul nge_start_locked(ifp); 1916196c0df6SHidetoshi Shimokawa 1917196c0df6SHidetoshi Shimokawa if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) { 19183929ff51SPyun YongHyeon uint32_t status; 1919196c0df6SHidetoshi Shimokawa 1920196c0df6SHidetoshi Shimokawa /* Reading the ISR register clears all interrupts. */ 1921196c0df6SHidetoshi Shimokawa status = CSR_READ_4(sc, NGE_ISR); 1922196c0df6SHidetoshi Shimokawa 1923f6bc9430SPyun YongHyeon if ((status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW)) != 0) 1924196c0df6SHidetoshi Shimokawa nge_rxeof(sc); 1925196c0df6SHidetoshi Shimokawa 1926f6bc9430SPyun YongHyeon if ((status & NGE_ISR_RX_IDLE) != 0) 1927196c0df6SHidetoshi Shimokawa NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 1928196c0df6SHidetoshi Shimokawa 1929f6bc9430SPyun YongHyeon if ((status & NGE_ISR_SYSERR) != 0) { 1930f6bc9430SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1931ad6c618bSBill Paul nge_init_locked(sc); 1932196c0df6SHidetoshi Shimokawa } 1933196c0df6SHidetoshi Shimokawa } 1934ad6c618bSBill Paul NGE_UNLOCK(sc); 1935196c0df6SHidetoshi Shimokawa } 1936196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */ 1937196c0df6SHidetoshi Shimokawa 1938eaabec55SAlfred Perlstein static void 1939284c81cbSPyun YongHyeon nge_intr(void *arg) 1940ce4946daSBill Paul { 1941ce4946daSBill Paul struct nge_softc *sc; 1942ce4946daSBill Paul struct ifnet *ifp; 19433929ff51SPyun YongHyeon uint32_t status; 1944ce4946daSBill Paul 1945f6bc9430SPyun YongHyeon sc = (struct nge_softc *)arg; 1946fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 1947ce4946daSBill Paul 1948ad6c618bSBill Paul NGE_LOCK(sc); 1949196c0df6SHidetoshi Shimokawa 1950f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_SUSPENDED) != 0) 1951f6bc9430SPyun YongHyeon goto done_locked; 1952f6bc9430SPyun YongHyeon 1953f6bc9430SPyun YongHyeon /* Reading the ISR register clears all interrupts. */ 1954f6bc9430SPyun YongHyeon status = CSR_READ_4(sc, NGE_ISR); 1955f6bc9430SPyun YongHyeon if (status == 0xffffffff || (status & NGE_INTRS) == 0) 1956f6bc9430SPyun YongHyeon goto done_locked; 1957f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING 1958f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_POLLING) != 0) 1959f6bc9430SPyun YongHyeon goto done_locked; 1960f6bc9430SPyun YongHyeon #endif 1961f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) 1962f6bc9430SPyun YongHyeon goto done_locked; 1963ce4946daSBill Paul 1964ce4946daSBill Paul /* Disable interrupts. */ 1965ce4946daSBill Paul CSR_WRITE_4(sc, NGE_IER, 0); 1966ce4946daSBill Paul 19671f548804SDoug Ambrisko /* Data LED on for TBI mode */ 1968f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_TBI) != 0) 1969f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_GPIO, 1970f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_GPIO) | NGE_GPIO_GP3_OUT); 19711f548804SDoug Ambrisko 1972f6bc9430SPyun YongHyeon for (; (status & NGE_INTRS) != 0;) { 1973f6bc9430SPyun YongHyeon if ((status & (NGE_ISR_TX_DESC_OK | NGE_ISR_TX_ERR | 1974f6bc9430SPyun YongHyeon NGE_ISR_TX_OK | NGE_ISR_TX_IDLE)) != 0) 1975ce4946daSBill Paul nge_txeof(sc); 1976ce4946daSBill Paul 1977f6bc9430SPyun YongHyeon if ((status & (NGE_ISR_RX_DESC_OK | NGE_ISR_RX_ERR | 1978f6bc9430SPyun YongHyeon NGE_ISR_RX_OFLOW | NGE_ISR_RX_FIFO_OFLOW | 1979f6bc9430SPyun YongHyeon NGE_ISR_RX_IDLE | NGE_ISR_RX_OK)) != 0) 1980ce4946daSBill Paul nge_rxeof(sc); 1981ff7ed9f7SPoul-Henning Kamp 1982f6bc9430SPyun YongHyeon if ((status & NGE_ISR_RX_IDLE) != 0) 1983ff7ed9f7SPoul-Henning Kamp NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 1984ff7ed9f7SPoul-Henning Kamp 1985f6bc9430SPyun YongHyeon if ((status & NGE_ISR_SYSERR) != 0) { 198613f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 1987ad6c618bSBill Paul nge_init_locked(sc); 1988ce4946daSBill Paul } 1989f6bc9430SPyun YongHyeon /* Reading the ISR register clears all interrupts. */ 1990f6bc9430SPyun YongHyeon status = CSR_READ_4(sc, NGE_ISR); 1991ce4946daSBill Paul } 1992ce4946daSBill Paul 1993ce4946daSBill Paul /* Re-enable interrupts. */ 1994ce4946daSBill Paul CSR_WRITE_4(sc, NGE_IER, 1); 1995ce4946daSBill Paul 1996f6bc9430SPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 1997ad6c618bSBill Paul nge_start_locked(ifp); 1998ce4946daSBill Paul 19991f548804SDoug Ambrisko /* Data LED off for TBI mode */ 2000f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_TBI) != 0) 2001f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_GPIO, 2002f6bc9430SPyun YongHyeon CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT); 20031f548804SDoug Ambrisko 2004f6bc9430SPyun YongHyeon done_locked: 2005ad6c618bSBill Paul NGE_UNLOCK(sc); 2006ce4946daSBill Paul } 2007ce4946daSBill Paul 2008ce4946daSBill Paul /* 2009ce4946daSBill Paul * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data 2010ce4946daSBill Paul * pointers to the fragment pointers. 2011ce4946daSBill Paul */ 2012eaabec55SAlfred Perlstein static int 2013f6bc9430SPyun YongHyeon nge_encap(struct nge_softc *sc, struct mbuf **m_head) 2014ce4946daSBill Paul { 2015f6bc9430SPyun YongHyeon struct nge_txdesc *txd, *txd_last; 2016f6bc9430SPyun YongHyeon struct nge_desc *desc; 2017ce4946daSBill Paul struct mbuf *m; 2018f6bc9430SPyun YongHyeon bus_dmamap_t map; 2019f6bc9430SPyun YongHyeon bus_dma_segment_t txsegs[NGE_MAXTXSEGS]; 2020f6bc9430SPyun YongHyeon int error, i, nsegs, prod, si; 2021ce4946daSBill Paul 2022f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2023ce4946daSBill Paul 2024f6bc9430SPyun YongHyeon m = *m_head; 2025f6bc9430SPyun YongHyeon prod = sc->nge_cdata.nge_tx_prod; 2026f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[prod]; 2027f6bc9430SPyun YongHyeon txd_last = txd; 2028f6bc9430SPyun YongHyeon map = txd->tx_dmamap; 2029f6bc9430SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag, map, 2030f6bc9430SPyun YongHyeon *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT); 2031f6bc9430SPyun YongHyeon if (error == EFBIG) { 2032f6bc9430SPyun YongHyeon m = m_collapse(*m_head, M_DONTWAIT, NGE_MAXTXSEGS); 2033f6bc9430SPyun YongHyeon if (m == NULL) { 2034f6bc9430SPyun YongHyeon m_freem(*m_head); 2035f6bc9430SPyun YongHyeon *m_head = NULL; 2036ce4946daSBill Paul return (ENOBUFS); 2037ce4946daSBill Paul } 2038f6bc9430SPyun YongHyeon *m_head = m; 2039f6bc9430SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag, 2040f6bc9430SPyun YongHyeon map, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT); 2041f6bc9430SPyun YongHyeon if (error != 0) { 2042f6bc9430SPyun YongHyeon m_freem(*m_head); 2043f6bc9430SPyun YongHyeon *m_head = NULL; 2044f6bc9430SPyun YongHyeon return (error); 2045f6bc9430SPyun YongHyeon } 2046f6bc9430SPyun YongHyeon } else if (error != 0) 2047f6bc9430SPyun YongHyeon return (error); 2048f6bc9430SPyun YongHyeon if (nsegs == 0) { 2049f6bc9430SPyun YongHyeon m_freem(*m_head); 2050f6bc9430SPyun YongHyeon *m_head = NULL; 2051f6bc9430SPyun YongHyeon return (EIO); 2052ce4946daSBill Paul } 2053ce4946daSBill Paul 2054f6bc9430SPyun YongHyeon /* Check number of available descriptors. */ 2055f6bc9430SPyun YongHyeon if (sc->nge_cdata.nge_tx_cnt + nsegs >= (NGE_TX_RING_CNT - 1)) { 2056f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, map); 2057ce4946daSBill Paul return (ENOBUFS); 2058ce4946daSBill Paul } 2059ce4946daSBill Paul 2060f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, map, BUS_DMASYNC_PREWRITE); 2061ce4946daSBill Paul 2062f6bc9430SPyun YongHyeon si = prod; 2063f6bc9430SPyun YongHyeon for (i = 0; i < nsegs; i++) { 2064f6bc9430SPyun YongHyeon desc = &sc->nge_rdata.nge_tx_ring[prod]; 2065f6bc9430SPyun YongHyeon desc->nge_ptr = htole32(NGE_ADDR_LO(txsegs[i].ds_addr)); 2066f6bc9430SPyun YongHyeon if (i == 0) 2067f6bc9430SPyun YongHyeon desc->nge_cmdsts = htole32(txsegs[i].ds_len | 2068f6bc9430SPyun YongHyeon NGE_CMDSTS_MORE); 2069f6bc9430SPyun YongHyeon else 2070f6bc9430SPyun YongHyeon desc->nge_cmdsts = htole32(txsegs[i].ds_len | 2071f6bc9430SPyun YongHyeon NGE_CMDSTS_MORE | NGE_CMDSTS_OWN); 2072f6bc9430SPyun YongHyeon desc->nge_extsts = 0; 2073f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_cnt++; 2074f6bc9430SPyun YongHyeon NGE_INC(prod, NGE_TX_RING_CNT); 2075f6bc9430SPyun YongHyeon } 2076f6bc9430SPyun YongHyeon /* Update producer index. */ 2077f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_prod = prod; 2078f6bc9430SPyun YongHyeon 2079f6bc9430SPyun YongHyeon prod = (prod + NGE_TX_RING_CNT - 1) % NGE_TX_RING_CNT; 2080f6bc9430SPyun YongHyeon desc = &sc->nge_rdata.nge_tx_ring[prod]; 2081f6bc9430SPyun YongHyeon /* Check if we have a VLAN tag to insert. */ 2082f6bc9430SPyun YongHyeon if ((m->m_flags & M_VLANTAG) != 0) 2083f6bc9430SPyun YongHyeon desc->nge_extsts |= htole32(NGE_TXEXTSTS_VLANPKT | 2084f6bc9430SPyun YongHyeon bswap16(m->m_pkthdr.ether_vtag)); 2085f6bc9430SPyun YongHyeon /* Set EOP on the last desciptor. */ 2086f6bc9430SPyun YongHyeon desc->nge_cmdsts &= htole32(~NGE_CMDSTS_MORE); 2087f6bc9430SPyun YongHyeon 2088f6bc9430SPyun YongHyeon /* Set checksum offload in the first descriptor. */ 2089f6bc9430SPyun YongHyeon desc = &sc->nge_rdata.nge_tx_ring[si]; 2090f6bc9430SPyun YongHyeon if ((m->m_pkthdr.csum_flags & NGE_CSUM_FEATURES) != 0) { 2091f6bc9430SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0) 2092f6bc9430SPyun YongHyeon desc->nge_extsts |= htole32(NGE_TXEXTSTS_IPCSUM); 2093f6bc9430SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0) 2094f6bc9430SPyun YongHyeon desc->nge_extsts |= htole32(NGE_TXEXTSTS_TCPCSUM); 2095f6bc9430SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0) 2096f6bc9430SPyun YongHyeon desc->nge_extsts |= htole32(NGE_TXEXTSTS_UDPCSUM); 2097f6bc9430SPyun YongHyeon } 2098f6bc9430SPyun YongHyeon /* Lastly, turn the first descriptor ownership to hardware. */ 2099f6bc9430SPyun YongHyeon desc->nge_cmdsts |= htole32(NGE_CMDSTS_OWN); 2100f6bc9430SPyun YongHyeon 2101f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[prod]; 2102f6bc9430SPyun YongHyeon map = txd_last->tx_dmamap; 2103f6bc9430SPyun YongHyeon txd_last->tx_dmamap = txd->tx_dmamap; 2104f6bc9430SPyun YongHyeon txd->tx_dmamap = map; 2105f6bc9430SPyun YongHyeon txd->tx_m = m; 2106ce4946daSBill Paul 2107ce4946daSBill Paul return (0); 2108ce4946daSBill Paul } 2109ce4946daSBill Paul 2110ce4946daSBill Paul /* 2111ce4946daSBill Paul * Main transmit routine. To avoid having to do mbuf copies, we put pointers 2112ce4946daSBill Paul * to the mbuf data regions directly in the transmit lists. We also save a 2113ce4946daSBill Paul * copy of the pointers since the transmit list fragment pointers are 2114ce4946daSBill Paul * physical addresses. 2115ce4946daSBill Paul */ 2116ce4946daSBill Paul 2117eaabec55SAlfred Perlstein static void 2118284c81cbSPyun YongHyeon nge_start(struct ifnet *ifp) 2119ce4946daSBill Paul { 2120ce4946daSBill Paul struct nge_softc *sc; 2121ad6c618bSBill Paul 2122ad6c618bSBill Paul sc = ifp->if_softc; 2123ad6c618bSBill Paul NGE_LOCK(sc); 2124ad6c618bSBill Paul nge_start_locked(ifp); 2125ad6c618bSBill Paul NGE_UNLOCK(sc); 2126ad6c618bSBill Paul } 2127ad6c618bSBill Paul 2128ad6c618bSBill Paul static void 2129284c81cbSPyun YongHyeon nge_start_locked(struct ifnet *ifp) 2130ad6c618bSBill Paul { 2131ad6c618bSBill Paul struct nge_softc *sc; 2132f6bc9430SPyun YongHyeon struct mbuf *m_head; 2133f6bc9430SPyun YongHyeon int enq; 2134ce4946daSBill Paul 2135ce4946daSBill Paul sc = ifp->if_softc; 2136ce4946daSBill Paul 2137f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2138f6bc9430SPyun YongHyeon 2139f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) != 2140f6bc9430SPyun YongHyeon IFF_DRV_RUNNING || (sc->nge_flags & NGE_FLAG_LINK) == 0) 2141ce4946daSBill Paul return; 2142ce4946daSBill Paul 2143f6bc9430SPyun YongHyeon for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) && 2144f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_cnt < NGE_TX_RING_CNT - 2; ) { 2145f6bc9430SPyun YongHyeon IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head); 2146ce4946daSBill Paul if (m_head == NULL) 2147ce4946daSBill Paul break; 2148f6bc9430SPyun YongHyeon /* 2149f6bc9430SPyun YongHyeon * Pack the data into the transmit ring. If we 2150f6bc9430SPyun YongHyeon * don't have room, set the OACTIVE flag and wait 2151f6bc9430SPyun YongHyeon * for the NIC to drain the ring. 2152f6bc9430SPyun YongHyeon */ 2153f6bc9430SPyun YongHyeon if (nge_encap(sc, &m_head)) { 2154f6bc9430SPyun YongHyeon if (m_head == NULL) 2155f6bc9430SPyun YongHyeon break; 2156f6bc9430SPyun YongHyeon IFQ_DRV_PREPEND(&ifp->if_snd, m_head); 215713f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2158ce4946daSBill Paul break; 2159ce4946daSBill Paul } 2160ce4946daSBill Paul 2161f6bc9430SPyun YongHyeon enq++; 2162ce4946daSBill Paul /* 2163ce4946daSBill Paul * If there's a BPF listener, bounce a copy of this frame 2164ce4946daSBill Paul * to him. 2165ce4946daSBill Paul */ 216659a0d28bSChristian S.J. Peron ETHER_BPF_MTAP(ifp, m_head); 2167ce4946daSBill Paul } 2168ce4946daSBill Paul 2169f6bc9430SPyun YongHyeon if (enq > 0) { 2170f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag, 2171f6bc9430SPyun YongHyeon sc->nge_cdata.nge_tx_ring_map, 2172f6bc9430SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); 2173ce4946daSBill Paul /* Transmit */ 2174ce4946daSBill Paul NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE); 2175ce4946daSBill Paul 2176f6bc9430SPyun YongHyeon /* Set a timeout in case the chip goes out to lunch. */ 2177f6bc9430SPyun YongHyeon sc->nge_watchdog_timer = 5; 2178f6bc9430SPyun YongHyeon } 2179ce4946daSBill Paul } 2180ce4946daSBill Paul 2181eaabec55SAlfred Perlstein static void 2182284c81cbSPyun YongHyeon nge_init(void *xsc) 2183ce4946daSBill Paul { 2184ce4946daSBill Paul struct nge_softc *sc = xsc; 2185ad6c618bSBill Paul 2186ad6c618bSBill Paul NGE_LOCK(sc); 2187ad6c618bSBill Paul nge_init_locked(sc); 2188ad6c618bSBill Paul NGE_UNLOCK(sc); 2189ad6c618bSBill Paul } 2190ad6c618bSBill Paul 2191ad6c618bSBill Paul static void 2192284c81cbSPyun YongHyeon nge_init_locked(struct nge_softc *sc) 2193ad6c618bSBill Paul { 2194fc74a9f9SBrooks Davis struct ifnet *ifp = sc->nge_ifp; 2195ce4946daSBill Paul struct mii_data *mii; 2196f6bc9430SPyun YongHyeon uint8_t *eaddr; 2197f6bc9430SPyun YongHyeon uint32_t reg; 2198ad6c618bSBill Paul 2199ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 2200ce4946daSBill Paul 2201f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2202ce4946daSBill Paul return; 2203ce4946daSBill Paul 2204ce4946daSBill Paul /* 2205ce4946daSBill Paul * Cancel pending I/O and free all RX/TX buffers. 2206ce4946daSBill Paul */ 2207ce4946daSBill Paul nge_stop(sc); 2208ce4946daSBill Paul 2209f6bc9430SPyun YongHyeon /* Reset the adapter. */ 2210f6bc9430SPyun YongHyeon nge_reset(sc); 2211ce4946daSBill Paul 2212f6bc9430SPyun YongHyeon /* Disable Rx filter prior to programming Rx filter. */ 2213f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_CTL, 0); 2214f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_RXFILT_CTL); 2215f6bc9430SPyun YongHyeon 2216f6bc9430SPyun YongHyeon mii = device_get_softc(sc->nge_miibus); 2217f6bc9430SPyun YongHyeon 2218f6bc9430SPyun YongHyeon /* Set MAC address. */ 2219f6bc9430SPyun YongHyeon eaddr = IF_LLADDR(sc->nge_ifp); 2220ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0); 2221f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[1] << 8) | eaddr[0]); 2222ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1); 2223f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[3] << 8) | eaddr[2]); 2224ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2); 2225f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[5] << 8) | eaddr[4]); 2226ce4946daSBill Paul 2227ce4946daSBill Paul /* Init circular RX list. */ 2228ce4946daSBill Paul if (nge_list_rx_init(sc) == ENOBUFS) { 22296b9f5c94SGleb Smirnoff device_printf(sc->nge_dev, "initialization failed: no " 2230646abee6SJohn Baldwin "memory for rx buffers\n"); 2231ce4946daSBill Paul nge_stop(sc); 2232ce4946daSBill Paul return; 2233ce4946daSBill Paul } 2234ce4946daSBill Paul 2235ce4946daSBill Paul /* 2236ce4946daSBill Paul * Init tx descriptors. 2237ce4946daSBill Paul */ 2238ce4946daSBill Paul nge_list_tx_init(sc); 2239ce4946daSBill Paul 2240ce4946daSBill Paul /* 2241ce4946daSBill Paul * For the NatSemi chip, we have to explicitly enable the 2242ce4946daSBill Paul * reception of ARP frames, as well as turn on the 'perfect 2243ce4946daSBill Paul * match' filter where we store the station address, otherwise 2244ce4946daSBill Paul * we won't receive unicasts meant for this host. 2245ce4946daSBill Paul */ 2246ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP); 2247ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT); 2248ce4946daSBill Paul 2249ce4946daSBill Paul /* 2250ce4946daSBill Paul * Set the capture broadcast bit to capture broadcast frames. 2251ce4946daSBill Paul */ 2252ce4946daSBill Paul if (ifp->if_flags & IFF_BROADCAST) { 2253ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD); 2254ce4946daSBill Paul } else { 2255ce4946daSBill Paul NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD); 2256ce4946daSBill Paul } 2257ce4946daSBill Paul 2258f6bc9430SPyun YongHyeon /* Turn the receive filter on. */ 2259ce4946daSBill Paul NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE); 2260ce4946daSBill Paul 2261f6bc9430SPyun YongHyeon /* Set Rx filter. */ 2262f6bc9430SPyun YongHyeon nge_rxfilter(sc); 2263f6bc9430SPyun YongHyeon 2264f6bc9430SPyun YongHyeon /* Disable PRIQ ctl. */ 2265f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_PRIOQCTL, 0); 2266f6bc9430SPyun YongHyeon 2267f6bc9430SPyun YongHyeon /* 2268f6bc9430SPyun YongHyeon * Set pause frames paramters. 2269f6bc9430SPyun YongHyeon * Rx stat FIFO hi-threshold : 2 or more packets 2270f6bc9430SPyun YongHyeon * Rx stat FIFO lo-threshold : less than 2 packets 2271f6bc9430SPyun YongHyeon * Rx data FIFO hi-threshold : 2K or more bytes 2272f6bc9430SPyun YongHyeon * Rx data FIFO lo-threshold : less than 2K bytes 2273f6bc9430SPyun YongHyeon * pause time : (512ns * 0xffff) -> 33.55ms 2274f6bc9430SPyun YongHyeon */ 2275f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_PAUSECSR, 2276f6bc9430SPyun YongHyeon NGE_PAUSECSR_PAUSE_ON_MCAST | 2277f6bc9430SPyun YongHyeon NGE_PAUSECSR_PAUSE_ON_DA | 2278f6bc9430SPyun YongHyeon ((1 << 24) & NGE_PAUSECSR_RX_STATFIFO_THR_HI) | 2279f6bc9430SPyun YongHyeon ((1 << 22) & NGE_PAUSECSR_RX_STATFIFO_THR_LO) | 2280f6bc9430SPyun YongHyeon ((1 << 20) & NGE_PAUSECSR_RX_DATAFIFO_THR_HI) | 2281f6bc9430SPyun YongHyeon ((1 << 18) & NGE_PAUSECSR_RX_DATAFIFO_THR_LO) | 2282f6bc9430SPyun YongHyeon NGE_PAUSECSR_CNT); 2283f6bc9430SPyun YongHyeon 2284ce4946daSBill Paul /* 2285ce4946daSBill Paul * Load the address of the RX and TX lists. 2286ce4946daSBill Paul */ 2287f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 2288f6bc9430SPyun YongHyeon NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr)); 2289f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 2290f6bc9430SPyun YongHyeon NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr)); 2291f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI, 2292f6bc9430SPyun YongHyeon NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr)); 2293f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO, 2294f6bc9430SPyun YongHyeon NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr)); 2295ce4946daSBill Paul 2296f6bc9430SPyun YongHyeon /* Set RX configuration. */ 2297ce4946daSBill Paul CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG); 2298f6bc9430SPyun YongHyeon 2299f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, 0); 2300ce4946daSBill Paul /* 2301ce4946daSBill Paul * Enable hardware checksum validation for all IPv4 2302ce4946daSBill Paul * packets, do not reject packets with bad checksums. 2303ce4946daSBill Paul */ 2304f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) 2305f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB); 2306ce4946daSBill Paul 2307ce4946daSBill Paul /* 23089d4fe4b2SBrooks Davis * Tell the chip to detect and strip VLAN tag info from 23099d4fe4b2SBrooks Davis * received frames. The tag will be provided in the extsts 23109d4fe4b2SBrooks Davis * field in the RX descriptors. 2311ce4946daSBill Paul */ 2312f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_DETECT_ENB); 2313f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) 2314f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_STRIP_ENB); 2315ce4946daSBill Paul 2316f6bc9430SPyun YongHyeon /* Set TX configuration. */ 2317ce4946daSBill Paul CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG); 2318ce4946daSBill Paul 2319ce4946daSBill Paul /* 2320ce4946daSBill Paul * Enable TX IPv4 checksumming on a per-packet basis. 2321ce4946daSBill Paul */ 2322ce4946daSBill Paul CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT); 2323ce4946daSBill Paul 2324ce4946daSBill Paul /* 23259d4fe4b2SBrooks Davis * Tell the chip to insert VLAN tags on a per-packet basis as 23269d4fe4b2SBrooks Davis * dictated by the code in the frame encapsulation routine. 2327ce4946daSBill Paul */ 2328ce4946daSBill Paul NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT); 2329ce4946daSBill Paul 2330ce4946daSBill Paul /* 2331ce4946daSBill Paul * Enable the delivery of PHY interrupts based on 233223d3a203SBill Paul * link/speed/duplex status changes. Also enable the 233323d3a203SBill Paul * extsts field in the DMA descriptors (needed for 233423d3a203SBill Paul * TCP/IP checksum offload on transmit). 2335ce4946daSBill Paul */ 23362ce0498bSBill Paul NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD | 233723d3a203SBill Paul NGE_CFG_PHYINTR_LNK | NGE_CFG_PHYINTR_DUP | NGE_CFG_EXTSTS_ENB); 2338ce4946daSBill Paul 2339ce4946daSBill Paul /* 2340962315f6SBill Paul * Configure interrupt holdoff (moderation). We can 2341962315f6SBill Paul * have the chip delay interrupt delivery for a certain 2342962315f6SBill Paul * period. Units are in 100us, and the max setting 2343962315f6SBill Paul * is 25500us (0xFF x 100us). Default is a 100us holdoff. 2344962315f6SBill Paul */ 2345f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_IHR, sc->nge_int_holdoff); 2346f6bc9430SPyun YongHyeon 2347f6bc9430SPyun YongHyeon /* 2348f6bc9430SPyun YongHyeon * Enable MAC statistics counters and clear. 2349f6bc9430SPyun YongHyeon */ 2350f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_MIBCTL); 2351f6bc9430SPyun YongHyeon reg &= ~NGE_MIBCTL_FREEZE_CNT; 2352f6bc9430SPyun YongHyeon reg |= NGE_MIBCTL_CLEAR_CNT; 2353f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_MIBCTL, reg); 2354962315f6SBill Paul 2355962315f6SBill Paul /* 2356ce4946daSBill Paul * Enable interrupts. 2357ce4946daSBill Paul */ 2358ce4946daSBill Paul CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS); 2359196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING 2360196c0df6SHidetoshi Shimokawa /* 2361196c0df6SHidetoshi Shimokawa * ... only enable interrupts if we are not polling, make sure 2362196c0df6SHidetoshi Shimokawa * they are off otherwise. 2363196c0df6SHidetoshi Shimokawa */ 2364f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_POLLING) != 0) 2365196c0df6SHidetoshi Shimokawa CSR_WRITE_4(sc, NGE_IER, 0); 2366196c0df6SHidetoshi Shimokawa else 236740929967SGleb Smirnoff #endif 2368ce4946daSBill Paul CSR_WRITE_4(sc, NGE_IER, 1); 2369ce4946daSBill Paul 2370f6bc9430SPyun YongHyeon sc->nge_flags &= ~NGE_FLAG_LINK; 2371f6bc9430SPyun YongHyeon mii_mediachg(mii); 2372ce4946daSBill Paul 2373f6bc9430SPyun YongHyeon sc->nge_watchdog_timer = 0; 2374f6bc9430SPyun YongHyeon callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc); 2375ce4946daSBill Paul 237613f4c340SRobert Watson ifp->if_drv_flags |= IFF_DRV_RUNNING; 237713f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2378ce4946daSBill Paul } 2379ce4946daSBill Paul 2380ce4946daSBill Paul /* 2381ce4946daSBill Paul * Set media options. 2382ce4946daSBill Paul */ 2383eaabec55SAlfred Perlstein static int 2384f6bc9430SPyun YongHyeon nge_mediachange(struct ifnet *ifp) 2385646abee6SJohn Baldwin { 2386646abee6SJohn Baldwin struct nge_softc *sc; 2387ce4946daSBill Paul struct mii_data *mii; 2388f6bc9430SPyun YongHyeon struct mii_softc *miisc; 2389f6bc9430SPyun YongHyeon int error; 2390ce4946daSBill Paul 2391ce4946daSBill Paul sc = ifp->if_softc; 2392f6bc9430SPyun YongHyeon NGE_LOCK(sc); 2393ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 2394ce4946daSBill Paul if (mii->mii_instance) { 2395646abee6SJohn Baldwin LIST_FOREACH(miisc, &mii->mii_phys, mii_list) 2396ce4946daSBill Paul mii_phy_reset(miisc); 2397ce4946daSBill Paul } 2398f6bc9430SPyun YongHyeon error = mii_mediachg(mii); 2399f6bc9430SPyun YongHyeon NGE_UNLOCK(sc); 2400f6bc9430SPyun YongHyeon 2401f6bc9430SPyun YongHyeon return (error); 2402ce4946daSBill Paul } 2403ce4946daSBill Paul 2404ce4946daSBill Paul /* 2405ce4946daSBill Paul * Report current media status. 2406ce4946daSBill Paul */ 2407eaabec55SAlfred Perlstein static void 2408f6bc9430SPyun YongHyeon nge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr) 2409ce4946daSBill Paul { 2410ce4946daSBill Paul struct nge_softc *sc; 2411ce4946daSBill Paul struct mii_data *mii; 2412ce4946daSBill Paul 2413ce4946daSBill Paul sc = ifp->if_softc; 2414646abee6SJohn Baldwin NGE_LOCK(sc); 2415ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 2416ce4946daSBill Paul mii_pollstat(mii); 2417f6bc9430SPyun YongHyeon NGE_UNLOCK(sc); 2418ce4946daSBill Paul ifmr->ifm_active = mii->mii_media_active; 2419ce4946daSBill Paul ifmr->ifm_status = mii->mii_media_status; 24201f548804SDoug Ambrisko } 2421ce4946daSBill Paul 2422eaabec55SAlfred Perlstein static int 2423284c81cbSPyun YongHyeon nge_ioctl(struct ifnet *ifp, u_long command, caddr_t data) 2424ce4946daSBill Paul { 2425ce4946daSBill Paul struct nge_softc *sc = ifp->if_softc; 2426ce4946daSBill Paul struct ifreq *ifr = (struct ifreq *) data; 2427ce4946daSBill Paul struct mii_data *mii; 2428f6bc9430SPyun YongHyeon int error = 0, mask; 2429ce4946daSBill Paul 2430ce4946daSBill Paul switch (command) { 2431ce4946daSBill Paul case SIOCSIFMTU: 2432f6bc9430SPyun YongHyeon if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > NGE_JUMBO_MTU) 2433ce4946daSBill Paul error = EINVAL; 2434cb2f755cSBill Paul else { 2435646abee6SJohn Baldwin NGE_LOCK(sc); 2436ce4946daSBill Paul ifp->if_mtu = ifr->ifr_mtu; 2437cb2f755cSBill Paul /* 2438cb2f755cSBill Paul * Workaround: if the MTU is larger than 2439cb2f755cSBill Paul * 8152 (TX FIFO size minus 64 minus 18), turn off 2440cb2f755cSBill Paul * TX checksum offloading. 2441cb2f755cSBill Paul */ 2442a5820ecbSYaroslav Tykhiy if (ifr->ifr_mtu >= 8152) { 2443a5820ecbSYaroslav Tykhiy ifp->if_capenable &= ~IFCAP_TXCSUM; 2444f6bc9430SPyun YongHyeon ifp->if_hwassist &= ~NGE_CSUM_FEATURES; 2445a5820ecbSYaroslav Tykhiy } else { 2446a5820ecbSYaroslav Tykhiy ifp->if_capenable |= IFCAP_TXCSUM; 2447f6bc9430SPyun YongHyeon ifp->if_hwassist |= NGE_CSUM_FEATURES; 2448cb2f755cSBill Paul } 2449646abee6SJohn Baldwin NGE_UNLOCK(sc); 2450f6bc9430SPyun YongHyeon VLAN_CAPABILITIES(ifp); 2451a5820ecbSYaroslav Tykhiy } 2452ce4946daSBill Paul break; 2453ce4946daSBill Paul case SIOCSIFFLAGS: 2454ad6c618bSBill Paul NGE_LOCK(sc); 2455f6bc9430SPyun YongHyeon if ((ifp->if_flags & IFF_UP) != 0) { 2456f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2457f6bc9430SPyun YongHyeon if ((ifp->if_flags ^ sc->nge_if_flags) & 2458f6bc9430SPyun YongHyeon (IFF_PROMISC | IFF_ALLMULTI)) 2459f6bc9430SPyun YongHyeon nge_rxfilter(sc); 2460ce4946daSBill Paul } else { 2461f6bc9430SPyun YongHyeon if ((sc->nge_flags & NGE_FLAG_DETACH) == 0) 2462ad6c618bSBill Paul nge_init_locked(sc); 2463ce4946daSBill Paul } 2464ce4946daSBill Paul } else { 2465f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) 2466ce4946daSBill Paul nge_stop(sc); 2467ce4946daSBill Paul } 2468ce4946daSBill Paul sc->nge_if_flags = ifp->if_flags; 2469ad6c618bSBill Paul NGE_UNLOCK(sc); 2470ce4946daSBill Paul error = 0; 2471ce4946daSBill Paul break; 2472ce4946daSBill Paul case SIOCADDMULTI: 2473ce4946daSBill Paul case SIOCDELMULTI: 2474ad6c618bSBill Paul NGE_LOCK(sc); 2475f6bc9430SPyun YongHyeon nge_rxfilter(sc); 2476ad6c618bSBill Paul NGE_UNLOCK(sc); 2477ce4946daSBill Paul error = 0; 2478ce4946daSBill Paul break; 2479ce4946daSBill Paul case SIOCGIFMEDIA: 2480ce4946daSBill Paul case SIOCSIFMEDIA: 2481ce4946daSBill Paul mii = device_get_softc(sc->nge_miibus); 2482f6bc9430SPyun YongHyeon error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command); 2483ce4946daSBill Paul break; 248437f5f239SRuslan Ermilov case SIOCSIFCAP: 248540929967SGleb Smirnoff NGE_LOCK(sc); 2486f6bc9430SPyun YongHyeon mask = ifr->ifr_reqcap ^ ifp->if_capenable; 2487f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING 2488f6bc9430SPyun YongHyeon if ((mask & IFCAP_POLLING) != 0 && 2489f6bc9430SPyun YongHyeon (IFCAP_POLLING & ifp->if_capabilities) != 0) { 2490f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_POLLING; 2491f6bc9430SPyun YongHyeon if ((IFCAP_POLLING & ifp->if_capenable) != 0) { 2492f6bc9430SPyun YongHyeon error = ether_poll_register(nge_poll, ifp); 2493f6bc9430SPyun YongHyeon if (error != 0) { 249440929967SGleb Smirnoff NGE_UNLOCK(sc); 2495f6bc9430SPyun YongHyeon break; 249640929967SGleb Smirnoff } 2497f6bc9430SPyun YongHyeon /* Disable interrupts. */ 2498f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_IER, 0); 2499f6bc9430SPyun YongHyeon } else { 250040929967SGleb Smirnoff error = ether_poll_deregister(ifp); 250140929967SGleb Smirnoff /* Enable interrupts. */ 250240929967SGleb Smirnoff CSR_WRITE_4(sc, NGE_IER, 1); 2503f6bc9430SPyun YongHyeon } 250440929967SGleb Smirnoff } 250540929967SGleb Smirnoff #endif /* DEVICE_POLLING */ 2506f6bc9430SPyun YongHyeon if ((mask & IFCAP_TXCSUM) != 0 && 2507f6bc9430SPyun YongHyeon (IFCAP_TXCSUM & ifp->if_capabilities) != 0) { 2508f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_TXCSUM; 2509f6bc9430SPyun YongHyeon if ((IFCAP_TXCSUM & ifp->if_capenable) != 0) 2510f6bc9430SPyun YongHyeon ifp->if_hwassist |= NGE_CSUM_FEATURES; 2511f6bc9430SPyun YongHyeon else 2512f6bc9430SPyun YongHyeon ifp->if_hwassist &= ~NGE_CSUM_FEATURES; 2513f6bc9430SPyun YongHyeon } 2514f6bc9430SPyun YongHyeon if ((mask & IFCAP_RXCSUM) != 0 && 2515f6bc9430SPyun YongHyeon (IFCAP_RXCSUM & ifp->if_capabilities) != 0) 2516f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_RXCSUM; 2517f6bc9430SPyun YongHyeon 2518f6bc9430SPyun YongHyeon if ((mask & IFCAP_WOL) != 0 && 2519f6bc9430SPyun YongHyeon (ifp->if_capabilities & IFCAP_WOL) != 0) { 2520f6bc9430SPyun YongHyeon if ((mask & IFCAP_WOL_UCAST) != 0) 2521f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_UCAST; 2522f6bc9430SPyun YongHyeon if ((mask & IFCAP_WOL_MCAST) != 0) 2523f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MCAST; 2524f6bc9430SPyun YongHyeon if ((mask & IFCAP_WOL_MAGIC) != 0) 2525f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_WOL_MAGIC; 2526f6bc9430SPyun YongHyeon } 2527f6bc9430SPyun YongHyeon 2528f6bc9430SPyun YongHyeon if ((mask & IFCAP_VLAN_HWCSUM) != 0 && 2529f6bc9430SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0) 2530f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWCSUM; 2531f6bc9430SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTAGGING) != 0 && 2532f6bc9430SPyun YongHyeon (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) { 2533f6bc9430SPyun YongHyeon ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING; 2534f6bc9430SPyun YongHyeon if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) { 2535f6bc9430SPyun YongHyeon if ((ifp->if_capenable & 2536f6bc9430SPyun YongHyeon IFCAP_VLAN_HWTAGGING) != 0) 2537f6bc9430SPyun YongHyeon NGE_SETBIT(sc, 2538f6bc9430SPyun YongHyeon NGE_VLAN_IP_RXCTL, 2539f6bc9430SPyun YongHyeon NGE_VIPRXCTL_TAG_STRIP_ENB); 2540f6bc9430SPyun YongHyeon else 2541f6bc9430SPyun YongHyeon NGE_CLRBIT(sc, 2542f6bc9430SPyun YongHyeon NGE_VLAN_IP_RXCTL, 2543f6bc9430SPyun YongHyeon NGE_VIPRXCTL_TAG_STRIP_ENB); 2544f6bc9430SPyun YongHyeon } 2545f6bc9430SPyun YongHyeon } 2546f6bc9430SPyun YongHyeon /* 2547f6bc9430SPyun YongHyeon * Both VLAN hardware tagging and checksum offload is 2548f6bc9430SPyun YongHyeon * required to do checksum offload on VLAN interface. 2549f6bc9430SPyun YongHyeon */ 2550f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_TXCSUM) == 0) 2551f6bc9430SPyun YongHyeon ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM; 2552f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0) 2553f6bc9430SPyun YongHyeon ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM; 2554f6bc9430SPyun YongHyeon NGE_UNLOCK(sc); 2555f6bc9430SPyun YongHyeon VLAN_CAPABILITIES(ifp); 255637f5f239SRuslan Ermilov break; 2557ce4946daSBill Paul default: 2558673d9191SSam Leffler error = ether_ioctl(ifp, command, data); 2559ce4946daSBill Paul break; 2560ce4946daSBill Paul } 2561ce4946daSBill Paul 2562ce4946daSBill Paul return (error); 2563ce4946daSBill Paul } 2564ce4946daSBill Paul 2565eaabec55SAlfred Perlstein static void 2566f6bc9430SPyun YongHyeon nge_watchdog(struct nge_softc *sc) 2567ce4946daSBill Paul { 2568f6bc9430SPyun YongHyeon struct ifnet *ifp; 2569ce4946daSBill Paul 2570f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2571ce4946daSBill Paul 2572f6bc9430SPyun YongHyeon if (sc->nge_watchdog_timer == 0 || --sc->nge_watchdog_timer) 2573f6bc9430SPyun YongHyeon return; 2574f6bc9430SPyun YongHyeon 2575f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 2576ce4946daSBill Paul ifp->if_oerrors++; 25776b9f5c94SGleb Smirnoff if_printf(ifp, "watchdog timeout\n"); 2578ce4946daSBill Paul 257913f4c340SRobert Watson ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2580ad6c618bSBill Paul nge_init_locked(sc); 2581ce4946daSBill Paul 2582f6bc9430SPyun YongHyeon if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) 2583ad6c618bSBill Paul nge_start_locked(ifp); 2584f6bc9430SPyun YongHyeon } 2585ad6c618bSBill Paul 2586f6bc9430SPyun YongHyeon static int 2587f6bc9430SPyun YongHyeon nge_stop_mac(struct nge_softc *sc) 2588f6bc9430SPyun YongHyeon { 2589f6bc9430SPyun YongHyeon uint32_t reg; 2590f6bc9430SPyun YongHyeon int i; 2591f6bc9430SPyun YongHyeon 2592f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2593f6bc9430SPyun YongHyeon 2594f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_CSR); 2595f6bc9430SPyun YongHyeon if ((reg & (NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE)) != 0) { 2596f6bc9430SPyun YongHyeon reg &= ~(NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE); 2597f6bc9430SPyun YongHyeon reg |= NGE_CSR_TX_DISABLE | NGE_CSR_RX_DISABLE; 2598f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CSR, reg); 2599f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TIMEOUT; i++) { 2600f6bc9430SPyun YongHyeon DELAY(1); 2601f6bc9430SPyun YongHyeon if ((CSR_READ_4(sc, NGE_CSR) & 2602f6bc9430SPyun YongHyeon (NGE_CSR_RX_ENABLE | NGE_CSR_TX_ENABLE)) == 0) 2603f6bc9430SPyun YongHyeon break; 2604f6bc9430SPyun YongHyeon } 2605f6bc9430SPyun YongHyeon if (i == NGE_TIMEOUT) 2606f6bc9430SPyun YongHyeon return (ETIMEDOUT); 2607f6bc9430SPyun YongHyeon } 2608f6bc9430SPyun YongHyeon 2609f6bc9430SPyun YongHyeon return (0); 2610ce4946daSBill Paul } 2611ce4946daSBill Paul 2612ce4946daSBill Paul /* 2613ce4946daSBill Paul * Stop the adapter and free any mbufs allocated to the 2614ce4946daSBill Paul * RX and TX lists. 2615ce4946daSBill Paul */ 2616eaabec55SAlfred Perlstein static void 2617284c81cbSPyun YongHyeon nge_stop(struct nge_softc *sc) 2618ce4946daSBill Paul { 2619f6bc9430SPyun YongHyeon struct nge_txdesc *txd; 2620f6bc9430SPyun YongHyeon struct nge_rxdesc *rxd; 26212cf2d799SPyun YongHyeon int i; 2622ce4946daSBill Paul struct ifnet *ifp; 2623ce4946daSBill Paul 2624ad6c618bSBill Paul NGE_LOCK_ASSERT(sc); 2625fc74a9f9SBrooks Davis ifp = sc->nge_ifp; 2626ce4946daSBill Paul 262713f4c340SRobert Watson ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2628f6bc9430SPyun YongHyeon sc->nge_flags &= ~NGE_FLAG_LINK; 2629f6bc9430SPyun YongHyeon callout_stop(&sc->nge_stat_ch); 2630f6bc9430SPyun YongHyeon sc->nge_watchdog_timer = 0; 2631f6bc9430SPyun YongHyeon 2632f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_IER, 0); 2633f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_IMR, 0); 2634f6bc9430SPyun YongHyeon if (nge_stop_mac(sc) == ETIMEDOUT) 2635f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 2636f6bc9430SPyun YongHyeon "%s: unable to stop Tx/Rx MAC\n", __func__); 2637f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI, 0); 2638f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO, 0); 2639f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0); 2640f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0); 2641f6bc9430SPyun YongHyeon nge_stats_update(sc); 2642f6bc9430SPyun YongHyeon if (sc->nge_head != NULL) { 2643f6bc9430SPyun YongHyeon m_freem(sc->nge_head); 2644f6bc9430SPyun YongHyeon sc->nge_head = sc->nge_tail = NULL; 2645f6bc9430SPyun YongHyeon } 2646f6bc9430SPyun YongHyeon 2647f6bc9430SPyun YongHyeon /* 2648f6bc9430SPyun YongHyeon * Free RX and TX mbufs still in the queues. 2649f6bc9430SPyun YongHyeon */ 2650f6bc9430SPyun YongHyeon for (i = 0; i < NGE_RX_RING_CNT; i++) { 2651f6bc9430SPyun YongHyeon rxd = &sc->nge_cdata.nge_rxdesc[i]; 2652f6bc9430SPyun YongHyeon if (rxd->rx_m != NULL) { 2653f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, 2654f6bc9430SPyun YongHyeon rxd->rx_dmamap, BUS_DMASYNC_POSTREAD); 2655f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_rx_tag, 2656f6bc9430SPyun YongHyeon rxd->rx_dmamap); 2657f6bc9430SPyun YongHyeon m_freem(rxd->rx_m); 2658f6bc9430SPyun YongHyeon rxd->rx_m = NULL; 2659f6bc9430SPyun YongHyeon } 2660f6bc9430SPyun YongHyeon } 2661f6bc9430SPyun YongHyeon for (i = 0; i < NGE_TX_RING_CNT; i++) { 2662f6bc9430SPyun YongHyeon txd = &sc->nge_cdata.nge_txdesc[i]; 2663f6bc9430SPyun YongHyeon if (txd->tx_m != NULL) { 2664f6bc9430SPyun YongHyeon bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, 2665f6bc9430SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE); 2666f6bc9430SPyun YongHyeon bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, 2667f6bc9430SPyun YongHyeon txd->tx_dmamap); 2668f6bc9430SPyun YongHyeon m_freem(txd->tx_m); 2669f6bc9430SPyun YongHyeon txd->tx_m = NULL; 2670f6bc9430SPyun YongHyeon } 2671f6bc9430SPyun YongHyeon } 2672f6bc9430SPyun YongHyeon } 2673f6bc9430SPyun YongHyeon 2674f6bc9430SPyun YongHyeon /* 2675f6bc9430SPyun YongHyeon * Before setting WOL bits, caller should have stopped Receiver. 2676f6bc9430SPyun YongHyeon */ 2677f6bc9430SPyun YongHyeon static void 2678f6bc9430SPyun YongHyeon nge_wol(struct nge_softc *sc) 2679f6bc9430SPyun YongHyeon { 2680f6bc9430SPyun YongHyeon struct ifnet *ifp; 2681f6bc9430SPyun YongHyeon uint32_t reg; 2682f6bc9430SPyun YongHyeon uint16_t pmstat; 2683f6bc9430SPyun YongHyeon int pmc; 2684f6bc9430SPyun YongHyeon 2685f6bc9430SPyun YongHyeon NGE_LOCK_ASSERT(sc); 2686f6bc9430SPyun YongHyeon 2687f6bc9430SPyun YongHyeon if (pci_find_extcap(sc->nge_dev, PCIY_PMG, &pmc) != 0) 2688f6bc9430SPyun YongHyeon return; 2689f6bc9430SPyun YongHyeon 2690f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 2691f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) == 0) { 2692f6bc9430SPyun YongHyeon /* Disable WOL & disconnect CLKRUN to save power. */ 2693f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_WOLCSR, 0); 2694f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CLKRUN, 0); 2695f6bc9430SPyun YongHyeon } else { 2696f6bc9430SPyun YongHyeon if (nge_stop_mac(sc) == ETIMEDOUT) 2697f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 2698f6bc9430SPyun YongHyeon "%s: unable to stop Tx/Rx MAC\n", __func__); 2699f6bc9430SPyun YongHyeon /* 2700f6bc9430SPyun YongHyeon * Make sure wake frames will be buffered in the Rx FIFO. 2701f6bc9430SPyun YongHyeon * (i.e. Silent Rx mode.) 2702f6bc9430SPyun YongHyeon */ 2703f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0); 2704f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_RX_LISTPTR_HI); 2705f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0); 2706f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_RX_LISTPTR_LO); 2707f6bc9430SPyun YongHyeon /* Enable Rx again. */ 2708f6bc9430SPyun YongHyeon NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE); 2709f6bc9430SPyun YongHyeon CSR_BARRIER_WRITE_4(sc, NGE_CSR); 2710f6bc9430SPyun YongHyeon 2711f6bc9430SPyun YongHyeon /* Configure WOL events. */ 2712f6bc9430SPyun YongHyeon reg = 0; 2713f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0) 2714f6bc9430SPyun YongHyeon reg |= NGE_WOLCSR_WAKE_ON_UNICAST; 2715f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0) 2716f6bc9430SPyun YongHyeon reg |= NGE_WOLCSR_WAKE_ON_MULTICAST; 2717f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0) 2718f6bc9430SPyun YongHyeon reg |= NGE_WOLCSR_WAKE_ON_MAGICPKT; 2719f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_WOLCSR, reg); 2720f6bc9430SPyun YongHyeon 2721f6bc9430SPyun YongHyeon /* Activate CLKRUN. */ 2722f6bc9430SPyun YongHyeon reg = CSR_READ_4(sc, NGE_CLKRUN); 2723f6bc9430SPyun YongHyeon reg |= NGE_CLKRUN_PMEENB | NGE_CLNRUN_CLKRUN_ENB; 2724f6bc9430SPyun YongHyeon CSR_WRITE_4(sc, NGE_CLKRUN, reg); 2725f6bc9430SPyun YongHyeon } 2726f6bc9430SPyun YongHyeon 2727f6bc9430SPyun YongHyeon /* Request PME. */ 2728f6bc9430SPyun YongHyeon pmstat = pci_read_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, 2); 2729f6bc9430SPyun YongHyeon pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE); 2730f6bc9430SPyun YongHyeon if ((ifp->if_capenable & IFCAP_WOL) != 0) 2731f6bc9430SPyun YongHyeon pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE; 2732f6bc9430SPyun YongHyeon pci_write_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, pmstat, 2); 2733ce4946daSBill Paul } 2734ce4946daSBill Paul 2735ce4946daSBill Paul /* 2736ce4946daSBill Paul * Stop all chip I/O so that the kernel's probe routines don't 2737ce4946daSBill Paul * get confused by errant DMAs when rebooting. 2738ce4946daSBill Paul */ 27396a087a87SPyun YongHyeon static int 2740284c81cbSPyun YongHyeon nge_shutdown(device_t dev) 2741ce4946daSBill Paul { 2742f6bc9430SPyun YongHyeon 2743f6bc9430SPyun YongHyeon return (nge_suspend(dev)); 2744f6bc9430SPyun YongHyeon } 2745f6bc9430SPyun YongHyeon 2746f6bc9430SPyun YongHyeon static int 2747f6bc9430SPyun YongHyeon nge_suspend(device_t dev) 2748f6bc9430SPyun YongHyeon { 2749ce4946daSBill Paul struct nge_softc *sc; 2750ce4946daSBill Paul 2751ce4946daSBill Paul sc = device_get_softc(dev); 2752ce4946daSBill Paul 2753ad6c618bSBill Paul NGE_LOCK(sc); 2754ce4946daSBill Paul nge_stop(sc); 2755f6bc9430SPyun YongHyeon nge_wol(sc); 2756f6bc9430SPyun YongHyeon sc->nge_flags |= NGE_FLAG_SUSPENDED; 2757ad6c618bSBill Paul NGE_UNLOCK(sc); 2758ce4946daSBill Paul 27596a087a87SPyun YongHyeon return (0); 2760ce4946daSBill Paul } 2761f6bc9430SPyun YongHyeon 2762f6bc9430SPyun YongHyeon static int 2763f6bc9430SPyun YongHyeon nge_resume(device_t dev) 2764f6bc9430SPyun YongHyeon { 2765f6bc9430SPyun YongHyeon struct nge_softc *sc; 2766f6bc9430SPyun YongHyeon struct ifnet *ifp; 2767f6bc9430SPyun YongHyeon uint16_t pmstat; 2768f6bc9430SPyun YongHyeon int pmc; 2769f6bc9430SPyun YongHyeon 2770f6bc9430SPyun YongHyeon sc = device_get_softc(dev); 2771f6bc9430SPyun YongHyeon 2772f6bc9430SPyun YongHyeon NGE_LOCK(sc); 2773f6bc9430SPyun YongHyeon ifp = sc->nge_ifp; 2774f6bc9430SPyun YongHyeon if (pci_find_extcap(sc->nge_dev, PCIY_PMG, &pmc) == 0) { 2775f6bc9430SPyun YongHyeon /* Disable PME and clear PME status. */ 2776f6bc9430SPyun YongHyeon pmstat = pci_read_config(sc->nge_dev, 2777f6bc9430SPyun YongHyeon pmc + PCIR_POWER_STATUS, 2); 2778f6bc9430SPyun YongHyeon if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) { 2779f6bc9430SPyun YongHyeon pmstat &= ~PCIM_PSTAT_PMEENABLE; 2780f6bc9430SPyun YongHyeon pci_write_config(sc->nge_dev, 2781f6bc9430SPyun YongHyeon pmc + PCIR_POWER_STATUS, pmstat, 2); 2782f6bc9430SPyun YongHyeon } 2783f6bc9430SPyun YongHyeon } 2784f6bc9430SPyun YongHyeon if (ifp->if_flags & IFF_UP) { 2785f6bc9430SPyun YongHyeon ifp->if_drv_flags &= ~IFF_DRV_RUNNING; 2786f6bc9430SPyun YongHyeon nge_init_locked(sc); 2787f6bc9430SPyun YongHyeon } 2788f6bc9430SPyun YongHyeon 2789f6bc9430SPyun YongHyeon sc->nge_flags &= ~NGE_FLAG_SUSPENDED; 2790f6bc9430SPyun YongHyeon NGE_UNLOCK(sc); 2791f6bc9430SPyun YongHyeon 2792f6bc9430SPyun YongHyeon return (0); 2793f6bc9430SPyun YongHyeon } 2794f6bc9430SPyun YongHyeon 2795f6bc9430SPyun YongHyeon #define NGE_SYSCTL_STAT_ADD32(c, h, n, p, d) \ 2796f6bc9430SPyun YongHyeon SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d) 2797f6bc9430SPyun YongHyeon 2798f6bc9430SPyun YongHyeon static void 2799f6bc9430SPyun YongHyeon nge_sysctl_node(struct nge_softc *sc) 2800f6bc9430SPyun YongHyeon { 2801f6bc9430SPyun YongHyeon struct sysctl_ctx_list *ctx; 2802f6bc9430SPyun YongHyeon struct sysctl_oid_list *child, *parent; 2803f6bc9430SPyun YongHyeon struct sysctl_oid *tree; 2804f6bc9430SPyun YongHyeon struct nge_stats *stats; 2805f6bc9430SPyun YongHyeon int error; 2806f6bc9430SPyun YongHyeon 2807f6bc9430SPyun YongHyeon ctx = device_get_sysctl_ctx(sc->nge_dev); 2808f6bc9430SPyun YongHyeon child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->nge_dev)); 2809f6bc9430SPyun YongHyeon SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_holdoff", 2810f6bc9430SPyun YongHyeon CTLTYPE_INT | CTLFLAG_RW, &sc->nge_int_holdoff, 0, 2811f6bc9430SPyun YongHyeon sysctl_hw_nge_int_holdoff, "I", "NGE interrupt moderation"); 2812f6bc9430SPyun YongHyeon /* Pull in device tunables. */ 2813f6bc9430SPyun YongHyeon sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT; 2814f6bc9430SPyun YongHyeon error = resource_int_value(device_get_name(sc->nge_dev), 2815f6bc9430SPyun YongHyeon device_get_unit(sc->nge_dev), "int_holdoff", &sc->nge_int_holdoff); 2816f6bc9430SPyun YongHyeon if (error == 0) { 2817f6bc9430SPyun YongHyeon if (sc->nge_int_holdoff < NGE_INT_HOLDOFF_MIN || 2818f6bc9430SPyun YongHyeon sc->nge_int_holdoff > NGE_INT_HOLDOFF_MAX ) { 2819f6bc9430SPyun YongHyeon device_printf(sc->nge_dev, 2820f6bc9430SPyun YongHyeon "int_holdoff value out of range; " 2821f6bc9430SPyun YongHyeon "using default: %d(%d us)\n", 2822f6bc9430SPyun YongHyeon NGE_INT_HOLDOFF_DEFAULT, 2823f6bc9430SPyun YongHyeon NGE_INT_HOLDOFF_DEFAULT * 100); 2824f6bc9430SPyun YongHyeon sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT; 2825f6bc9430SPyun YongHyeon } 2826f6bc9430SPyun YongHyeon } 2827f6bc9430SPyun YongHyeon 2828f6bc9430SPyun YongHyeon stats = &sc->nge_stats; 2829f6bc9430SPyun YongHyeon tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD, 2830f6bc9430SPyun YongHyeon NULL, "NGE statistics"); 2831f6bc9430SPyun YongHyeon parent = SYSCTL_CHILDREN(tree); 2832f6bc9430SPyun YongHyeon 2833f6bc9430SPyun YongHyeon /* Rx statistics. */ 2834f6bc9430SPyun YongHyeon tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD, 2835f6bc9430SPyun YongHyeon NULL, "Rx MAC statistics"); 2836f6bc9430SPyun YongHyeon child = SYSCTL_CHILDREN(tree); 2837f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_errs", 2838f6bc9430SPyun YongHyeon &stats->rx_pkts_errs, 2839f6bc9430SPyun YongHyeon "Packet errors including both wire errors and FIFO overruns"); 2840f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "crc_errs", 2841f6bc9430SPyun YongHyeon &stats->rx_crc_errs, "CRC errors"); 2842f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows", 2843f6bc9430SPyun YongHyeon &stats->rx_fifo_oflows, "FIFO overflows"); 2844f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "align_errs", 2845f6bc9430SPyun YongHyeon &stats->rx_align_errs, "Frame alignment errors"); 2846f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "sym_errs", 2847f6bc9430SPyun YongHyeon &stats->rx_sym_errs, "One or more symbol errors"); 2848f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_jumbos", 2849f6bc9430SPyun YongHyeon &stats->rx_pkts_jumbos, 2850f6bc9430SPyun YongHyeon "Packets received with length greater than 1518 bytes"); 2851f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "len_errs", 2852f6bc9430SPyun YongHyeon &stats->rx_len_errs, "In Range Length errors"); 2853f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "unctl_frames", 2854f6bc9430SPyun YongHyeon &stats->rx_unctl_frames, "Control frames with unsupported opcode"); 2855f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "pause", 2856f6bc9430SPyun YongHyeon &stats->rx_pause, "Pause frames"); 2857f6bc9430SPyun YongHyeon 2858f6bc9430SPyun YongHyeon /* Tx statistics. */ 2859f6bc9430SPyun YongHyeon tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD, 2860f6bc9430SPyun YongHyeon NULL, "Tx MAC statistics"); 2861f6bc9430SPyun YongHyeon child = SYSCTL_CHILDREN(tree); 2862f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "pause", 2863f6bc9430SPyun YongHyeon &stats->tx_pause, "Pause frames"); 2864f6bc9430SPyun YongHyeon NGE_SYSCTL_STAT_ADD32(ctx, child, "seq_errs", 2865f6bc9430SPyun YongHyeon &stats->tx_seq_errs, 2866f6bc9430SPyun YongHyeon "Loss of collision heartbeat during transmission"); 2867f6bc9430SPyun YongHyeon } 2868f6bc9430SPyun YongHyeon 2869f6bc9430SPyun YongHyeon #undef NGE_SYSCTL_STAT_ADD32 2870f6bc9430SPyun YongHyeon 2871f6bc9430SPyun YongHyeon static int 2872f6bc9430SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high) 2873f6bc9430SPyun YongHyeon { 2874f6bc9430SPyun YongHyeon int error, value; 2875f6bc9430SPyun YongHyeon 2876f6bc9430SPyun YongHyeon if (arg1 == NULL) 2877f6bc9430SPyun YongHyeon return (EINVAL); 2878f6bc9430SPyun YongHyeon value = *(int *)arg1; 2879f6bc9430SPyun YongHyeon error = sysctl_handle_int(oidp, &value, 0, req); 2880f6bc9430SPyun YongHyeon if (error != 0 || req->newptr == NULL) 2881f6bc9430SPyun YongHyeon return (error); 2882f6bc9430SPyun YongHyeon if (value < low || value > high) 2883f6bc9430SPyun YongHyeon return (EINVAL); 2884f6bc9430SPyun YongHyeon *(int *)arg1 = value; 2885f6bc9430SPyun YongHyeon 2886f6bc9430SPyun YongHyeon return (0); 2887f6bc9430SPyun YongHyeon } 2888f6bc9430SPyun YongHyeon 2889f6bc9430SPyun YongHyeon static int 2890f6bc9430SPyun YongHyeon sysctl_hw_nge_int_holdoff(SYSCTL_HANDLER_ARGS) 2891f6bc9430SPyun YongHyeon { 2892f6bc9430SPyun YongHyeon 2893f6bc9430SPyun YongHyeon return (sysctl_int_range(oidp, arg1, arg2, req, NGE_INT_HOLDOFF_MIN, 2894f6bc9430SPyun YongHyeon NGE_INT_HOLDOFF_MAX)); 2895f6bc9430SPyun YongHyeon } 2896