116199571SPyun YongHyeon /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
416199571SPyun YongHyeon * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
516199571SPyun YongHyeon * All rights reserved.
616199571SPyun YongHyeon *
716199571SPyun YongHyeon * Redistribution and use in source and binary forms, with or without
816199571SPyun YongHyeon * modification, are permitted provided that the following conditions
916199571SPyun YongHyeon * are met:
1016199571SPyun YongHyeon * 1. Redistributions of source code must retain the above copyright
1116199571SPyun YongHyeon * notice unmodified, this list of conditions, and the following
1216199571SPyun YongHyeon * disclaimer.
1316199571SPyun YongHyeon * 2. Redistributions in binary form must reproduce the above copyright
1416199571SPyun YongHyeon * notice, this list of conditions and the following disclaimer in the
1516199571SPyun YongHyeon * documentation and/or other materials provided with the distribution.
1616199571SPyun YongHyeon *
1716199571SPyun YongHyeon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1816199571SPyun YongHyeon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1916199571SPyun YongHyeon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2016199571SPyun YongHyeon * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2116199571SPyun YongHyeon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2216199571SPyun YongHyeon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2316199571SPyun YongHyeon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2416199571SPyun YongHyeon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2516199571SPyun YongHyeon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2616199571SPyun YongHyeon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2716199571SPyun YongHyeon * SUCH DAMAGE.
2816199571SPyun YongHyeon */
2916199571SPyun YongHyeon
3016199571SPyun YongHyeon /* Driver for Attansic Technology Corp. L1 Gigabit Ethernet. */
3116199571SPyun YongHyeon
3216199571SPyun YongHyeon #include <sys/param.h>
3316199571SPyun YongHyeon #include <sys/systm.h>
3416199571SPyun YongHyeon #include <sys/bus.h>
3516199571SPyun YongHyeon #include <sys/endian.h>
3616199571SPyun YongHyeon #include <sys/kernel.h>
3716199571SPyun YongHyeon #include <sys/malloc.h>
3816199571SPyun YongHyeon #include <sys/mbuf.h>
3916199571SPyun YongHyeon #include <sys/rman.h>
4016199571SPyun YongHyeon #include <sys/module.h>
4116199571SPyun YongHyeon #include <sys/queue.h>
4216199571SPyun YongHyeon #include <sys/socket.h>
4316199571SPyun YongHyeon #include <sys/sockio.h>
4416199571SPyun YongHyeon #include <sys/sysctl.h>
4516199571SPyun YongHyeon #include <sys/taskqueue.h>
4616199571SPyun YongHyeon
4716199571SPyun YongHyeon #include <net/bpf.h>
4816199571SPyun YongHyeon #include <net/if.h>
4976039bc8SGleb Smirnoff #include <net/if_var.h>
5016199571SPyun YongHyeon #include <net/if_arp.h>
5116199571SPyun YongHyeon #include <net/ethernet.h>
5216199571SPyun YongHyeon #include <net/if_dl.h>
5316199571SPyun YongHyeon #include <net/if_media.h>
5416199571SPyun YongHyeon #include <net/if_types.h>
5516199571SPyun YongHyeon #include <net/if_vlan_var.h>
5616199571SPyun YongHyeon
5716199571SPyun YongHyeon #include <netinet/in.h>
5816199571SPyun YongHyeon #include <netinet/in_systm.h>
5916199571SPyun YongHyeon #include <netinet/ip.h>
6016199571SPyun YongHyeon #include <netinet/tcp.h>
6116199571SPyun YongHyeon
6216199571SPyun YongHyeon #include <dev/mii/mii.h>
6316199571SPyun YongHyeon #include <dev/mii/miivar.h>
6416199571SPyun YongHyeon
6516199571SPyun YongHyeon #include <dev/pci/pcireg.h>
6616199571SPyun YongHyeon #include <dev/pci/pcivar.h>
6716199571SPyun YongHyeon
6816199571SPyun YongHyeon #include <machine/bus.h>
6916199571SPyun YongHyeon #include <machine/in_cksum.h>
7016199571SPyun YongHyeon
7116199571SPyun YongHyeon #include <dev/age/if_agereg.h>
7216199571SPyun YongHyeon #include <dev/age/if_agevar.h>
7316199571SPyun YongHyeon
7416199571SPyun YongHyeon /* "device miibus" required. See GENERIC if you get errors here. */
7516199571SPyun YongHyeon #include "miibus_if.h"
7616199571SPyun YongHyeon
7716199571SPyun YongHyeon #define AGE_CSUM_FEATURES (CSUM_TCP | CSUM_UDP)
7816199571SPyun YongHyeon
7916199571SPyun YongHyeon MODULE_DEPEND(age, pci, 1, 1, 1);
8016199571SPyun YongHyeon MODULE_DEPEND(age, ether, 1, 1, 1);
8116199571SPyun YongHyeon MODULE_DEPEND(age, miibus, 1, 1, 1);
8216199571SPyun YongHyeon
8316199571SPyun YongHyeon /* Tunables. */
8416199571SPyun YongHyeon static int msi_disable = 0;
8516199571SPyun YongHyeon static int msix_disable = 0;
8616199571SPyun YongHyeon TUNABLE_INT("hw.age.msi_disable", &msi_disable);
8716199571SPyun YongHyeon TUNABLE_INT("hw.age.msix_disable", &msix_disable);
8816199571SPyun YongHyeon
8916199571SPyun YongHyeon /*
9016199571SPyun YongHyeon * Devices supported by this driver.
9116199571SPyun YongHyeon */
9216199571SPyun YongHyeon static struct age_dev {
9316199571SPyun YongHyeon uint16_t age_vendorid;
9416199571SPyun YongHyeon uint16_t age_deviceid;
9516199571SPyun YongHyeon const char *age_name;
9616199571SPyun YongHyeon } age_devs[] = {
9716199571SPyun YongHyeon { VENDORID_ATTANSIC, DEVICEID_ATTANSIC_L1,
9816199571SPyun YongHyeon "Attansic Technology Corp, L1 Gigabit Ethernet" },
9916199571SPyun YongHyeon };
10016199571SPyun YongHyeon
10116199571SPyun YongHyeon static int age_miibus_readreg(device_t, int, int);
10216199571SPyun YongHyeon static int age_miibus_writereg(device_t, int, int, int);
10316199571SPyun YongHyeon static void age_miibus_statchg(device_t);
10452436412SJustin Hibbits static void age_mediastatus(if_t, struct ifmediareq *);
10552436412SJustin Hibbits static int age_mediachange(if_t);
10616199571SPyun YongHyeon static int age_probe(device_t);
10716199571SPyun YongHyeon static void age_get_macaddr(struct age_softc *);
10816199571SPyun YongHyeon static void age_phy_reset(struct age_softc *);
10916199571SPyun YongHyeon static int age_attach(device_t);
11016199571SPyun YongHyeon static int age_detach(device_t);
11116199571SPyun YongHyeon static void age_sysctl_node(struct age_softc *);
11216199571SPyun YongHyeon static void age_dmamap_cb(void *, bus_dma_segment_t *, int, int);
11316199571SPyun YongHyeon static int age_check_boundary(struct age_softc *);
11416199571SPyun YongHyeon static int age_dma_alloc(struct age_softc *);
11516199571SPyun YongHyeon static void age_dma_free(struct age_softc *);
11616199571SPyun YongHyeon static int age_shutdown(device_t);
11716199571SPyun YongHyeon static void age_setwol(struct age_softc *);
11816199571SPyun YongHyeon static int age_suspend(device_t);
11916199571SPyun YongHyeon static int age_resume(device_t);
12016199571SPyun YongHyeon static int age_encap(struct age_softc *, struct mbuf **);
12152436412SJustin Hibbits static void age_start(if_t);
12252436412SJustin Hibbits static void age_start_locked(if_t);
12316199571SPyun YongHyeon static void age_watchdog(struct age_softc *);
12452436412SJustin Hibbits static int age_ioctl(if_t, u_long, caddr_t);
12516199571SPyun YongHyeon static void age_mac_config(struct age_softc *);
12616199571SPyun YongHyeon static void age_link_task(void *, int);
12716199571SPyun YongHyeon static void age_stats_update(struct age_softc *);
12816199571SPyun YongHyeon static int age_intr(void *);
12916199571SPyun YongHyeon static void age_int_task(void *, int);
13016199571SPyun YongHyeon static void age_txintr(struct age_softc *, int);
13116199571SPyun YongHyeon static void age_rxeof(struct age_softc *sc, struct rx_rdesc *);
13216199571SPyun YongHyeon static int age_rxintr(struct age_softc *, int, int);
13316199571SPyun YongHyeon static void age_tick(void *);
13416199571SPyun YongHyeon static void age_reset(struct age_softc *);
13516199571SPyun YongHyeon static void age_init(void *);
13616199571SPyun YongHyeon static void age_init_locked(struct age_softc *);
13716199571SPyun YongHyeon static void age_stop(struct age_softc *);
13816199571SPyun YongHyeon static void age_stop_txmac(struct age_softc *);
13916199571SPyun YongHyeon static void age_stop_rxmac(struct age_softc *);
14016199571SPyun YongHyeon static void age_init_tx_ring(struct age_softc *);
14116199571SPyun YongHyeon static int age_init_rx_ring(struct age_softc *);
14216199571SPyun YongHyeon static void age_init_rr_ring(struct age_softc *);
14316199571SPyun YongHyeon static void age_init_cmb_block(struct age_softc *);
14416199571SPyun YongHyeon static void age_init_smb_block(struct age_softc *);
145088dd4b7SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
14652436412SJustin Hibbits static struct mbuf *age_fixup_rx(if_t, struct mbuf *);
147088dd4b7SPyun YongHyeon #endif
14816199571SPyun YongHyeon static int age_newbuf(struct age_softc *, struct age_rxdesc *);
14916199571SPyun YongHyeon static void age_rxvlan(struct age_softc *);
15016199571SPyun YongHyeon static void age_rxfilter(struct age_softc *);
15116199571SPyun YongHyeon static int sysctl_age_stats(SYSCTL_HANDLER_ARGS);
15216199571SPyun YongHyeon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
15316199571SPyun YongHyeon static int sysctl_hw_age_proc_limit(SYSCTL_HANDLER_ARGS);
15416199571SPyun YongHyeon static int sysctl_hw_age_int_mod(SYSCTL_HANDLER_ARGS);
15516199571SPyun YongHyeon
15616199571SPyun YongHyeon static device_method_t age_methods[] = {
15716199571SPyun YongHyeon /* Device interface. */
15816199571SPyun YongHyeon DEVMETHOD(device_probe, age_probe),
15916199571SPyun YongHyeon DEVMETHOD(device_attach, age_attach),
16016199571SPyun YongHyeon DEVMETHOD(device_detach, age_detach),
16116199571SPyun YongHyeon DEVMETHOD(device_shutdown, age_shutdown),
16216199571SPyun YongHyeon DEVMETHOD(device_suspend, age_suspend),
16316199571SPyun YongHyeon DEVMETHOD(device_resume, age_resume),
16416199571SPyun YongHyeon
16516199571SPyun YongHyeon /* MII interface. */
16616199571SPyun YongHyeon DEVMETHOD(miibus_readreg, age_miibus_readreg),
16716199571SPyun YongHyeon DEVMETHOD(miibus_writereg, age_miibus_writereg),
16816199571SPyun YongHyeon DEVMETHOD(miibus_statchg, age_miibus_statchg),
16916199571SPyun YongHyeon { NULL, NULL }
17016199571SPyun YongHyeon };
17116199571SPyun YongHyeon
17216199571SPyun YongHyeon static driver_t age_driver = {
17316199571SPyun YongHyeon "age",
17416199571SPyun YongHyeon age_methods,
17516199571SPyun YongHyeon sizeof(struct age_softc)
17616199571SPyun YongHyeon };
17716199571SPyun YongHyeon
17822a99a52SJohn Baldwin DRIVER_MODULE(age, pci, age_driver, 0, 0);
179ae8b178bSWarner Losh MODULE_PNP_INFO("U16:vendor;U16:device;D:#", pci, age, age_devs,
180329e817fSWarner Losh nitems(age_devs));
1813e38757dSJohn Baldwin DRIVER_MODULE(miibus, age, miibus_driver, 0, 0);
18216199571SPyun YongHyeon
18316199571SPyun YongHyeon static struct resource_spec age_res_spec_mem[] = {
18416199571SPyun YongHyeon { SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE },
18516199571SPyun YongHyeon { -1, 0, 0 }
18616199571SPyun YongHyeon };
18716199571SPyun YongHyeon
18816199571SPyun YongHyeon static struct resource_spec age_irq_spec_legacy[] = {
18916199571SPyun YongHyeon { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE },
19016199571SPyun YongHyeon { -1, 0, 0 }
19116199571SPyun YongHyeon };
19216199571SPyun YongHyeon
19316199571SPyun YongHyeon static struct resource_spec age_irq_spec_msi[] = {
19416199571SPyun YongHyeon { SYS_RES_IRQ, 1, RF_ACTIVE },
19516199571SPyun YongHyeon { -1, 0, 0 }
19616199571SPyun YongHyeon };
19716199571SPyun YongHyeon
19816199571SPyun YongHyeon static struct resource_spec age_irq_spec_msix[] = {
19916199571SPyun YongHyeon { SYS_RES_IRQ, 1, RF_ACTIVE },
20016199571SPyun YongHyeon { -1, 0, 0 }
20116199571SPyun YongHyeon };
20216199571SPyun YongHyeon
20316199571SPyun YongHyeon /*
20416199571SPyun YongHyeon * Read a PHY register on the MII of the L1.
20516199571SPyun YongHyeon */
20616199571SPyun YongHyeon static int
age_miibus_readreg(device_t dev,int phy,int reg)20716199571SPyun YongHyeon age_miibus_readreg(device_t dev, int phy, int reg)
20816199571SPyun YongHyeon {
20916199571SPyun YongHyeon struct age_softc *sc;
21016199571SPyun YongHyeon uint32_t v;
21116199571SPyun YongHyeon int i;
21216199571SPyun YongHyeon
21316199571SPyun YongHyeon sc = device_get_softc(dev);
21416199571SPyun YongHyeon
21516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
21616199571SPyun YongHyeon MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
21716199571SPyun YongHyeon for (i = AGE_PHY_TIMEOUT; i > 0; i--) {
21816199571SPyun YongHyeon DELAY(1);
21916199571SPyun YongHyeon v = CSR_READ_4(sc, AGE_MDIO);
22016199571SPyun YongHyeon if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
22116199571SPyun YongHyeon break;
22216199571SPyun YongHyeon }
22316199571SPyun YongHyeon
22416199571SPyun YongHyeon if (i == 0) {
22516199571SPyun YongHyeon device_printf(sc->age_dev, "phy read timeout : %d\n", reg);
22616199571SPyun YongHyeon return (0);
22716199571SPyun YongHyeon }
22816199571SPyun YongHyeon
22916199571SPyun YongHyeon return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
23016199571SPyun YongHyeon }
23116199571SPyun YongHyeon
23216199571SPyun YongHyeon /*
23316199571SPyun YongHyeon * Write a PHY register on the MII of the L1.
23416199571SPyun YongHyeon */
23516199571SPyun YongHyeon static int
age_miibus_writereg(device_t dev,int phy,int reg,int val)23616199571SPyun YongHyeon age_miibus_writereg(device_t dev, int phy, int reg, int val)
23716199571SPyun YongHyeon {
23816199571SPyun YongHyeon struct age_softc *sc;
23916199571SPyun YongHyeon uint32_t v;
24016199571SPyun YongHyeon int i;
24116199571SPyun YongHyeon
24216199571SPyun YongHyeon sc = device_get_softc(dev);
24316199571SPyun YongHyeon
24416199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
24516199571SPyun YongHyeon (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
24616199571SPyun YongHyeon MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
24716199571SPyun YongHyeon for (i = AGE_PHY_TIMEOUT; i > 0; i--) {
24816199571SPyun YongHyeon DELAY(1);
24916199571SPyun YongHyeon v = CSR_READ_4(sc, AGE_MDIO);
25016199571SPyun YongHyeon if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
25116199571SPyun YongHyeon break;
25216199571SPyun YongHyeon }
25316199571SPyun YongHyeon
25416199571SPyun YongHyeon if (i == 0)
25516199571SPyun YongHyeon device_printf(sc->age_dev, "phy write timeout : %d\n", reg);
25616199571SPyun YongHyeon
25716199571SPyun YongHyeon return (0);
25816199571SPyun YongHyeon }
25916199571SPyun YongHyeon
26016199571SPyun YongHyeon /*
26116199571SPyun YongHyeon * Callback from MII layer when media changes.
26216199571SPyun YongHyeon */
26316199571SPyun YongHyeon static void
age_miibus_statchg(device_t dev)26416199571SPyun YongHyeon age_miibus_statchg(device_t dev)
26516199571SPyun YongHyeon {
26616199571SPyun YongHyeon struct age_softc *sc;
26716199571SPyun YongHyeon
26816199571SPyun YongHyeon sc = device_get_softc(dev);
26916199571SPyun YongHyeon taskqueue_enqueue(taskqueue_swi, &sc->age_link_task);
27016199571SPyun YongHyeon }
27116199571SPyun YongHyeon
27216199571SPyun YongHyeon /*
27316199571SPyun YongHyeon * Get the current interface media status.
27416199571SPyun YongHyeon */
27516199571SPyun YongHyeon static void
age_mediastatus(if_t ifp,struct ifmediareq * ifmr)27652436412SJustin Hibbits age_mediastatus(if_t ifp, struct ifmediareq *ifmr)
27716199571SPyun YongHyeon {
27816199571SPyun YongHyeon struct age_softc *sc;
27916199571SPyun YongHyeon struct mii_data *mii;
28016199571SPyun YongHyeon
28152436412SJustin Hibbits sc = if_getsoftc(ifp);
28216199571SPyun YongHyeon AGE_LOCK(sc);
28316199571SPyun YongHyeon mii = device_get_softc(sc->age_miibus);
28416199571SPyun YongHyeon
28516199571SPyun YongHyeon mii_pollstat(mii);
28616199571SPyun YongHyeon ifmr->ifm_status = mii->mii_media_status;
28716199571SPyun YongHyeon ifmr->ifm_active = mii->mii_media_active;
28857c81d92SPyun YongHyeon AGE_UNLOCK(sc);
28916199571SPyun YongHyeon }
29016199571SPyun YongHyeon
29116199571SPyun YongHyeon /*
29216199571SPyun YongHyeon * Set hardware to newly-selected media.
29316199571SPyun YongHyeon */
29416199571SPyun YongHyeon static int
age_mediachange(if_t ifp)29552436412SJustin Hibbits age_mediachange(if_t ifp)
29616199571SPyun YongHyeon {
29716199571SPyun YongHyeon struct age_softc *sc;
29816199571SPyun YongHyeon struct mii_data *mii;
29916199571SPyun YongHyeon struct mii_softc *miisc;
30016199571SPyun YongHyeon int error;
30116199571SPyun YongHyeon
30252436412SJustin Hibbits sc = if_getsoftc(ifp);
30316199571SPyun YongHyeon AGE_LOCK(sc);
30416199571SPyun YongHyeon mii = device_get_softc(sc->age_miibus);
30516199571SPyun YongHyeon LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
3063fcb7a53SMarius Strobl PHY_RESET(miisc);
30716199571SPyun YongHyeon error = mii_mediachg(mii);
30816199571SPyun YongHyeon AGE_UNLOCK(sc);
30916199571SPyun YongHyeon
31016199571SPyun YongHyeon return (error);
31116199571SPyun YongHyeon }
31216199571SPyun YongHyeon
31316199571SPyun YongHyeon static int
age_probe(device_t dev)31416199571SPyun YongHyeon age_probe(device_t dev)
31516199571SPyun YongHyeon {
31616199571SPyun YongHyeon struct age_dev *sp;
31716199571SPyun YongHyeon int i;
31816199571SPyun YongHyeon uint16_t vendor, devid;
31916199571SPyun YongHyeon
32016199571SPyun YongHyeon vendor = pci_get_vendor(dev);
32116199571SPyun YongHyeon devid = pci_get_device(dev);
32216199571SPyun YongHyeon sp = age_devs;
32373a1170aSPedro F. Giffuni for (i = 0; i < nitems(age_devs); i++, sp++) {
32416199571SPyun YongHyeon if (vendor == sp->age_vendorid &&
32516199571SPyun YongHyeon devid == sp->age_deviceid) {
32616199571SPyun YongHyeon device_set_desc(dev, sp->age_name);
32716199571SPyun YongHyeon return (BUS_PROBE_DEFAULT);
32816199571SPyun YongHyeon }
32916199571SPyun YongHyeon }
33016199571SPyun YongHyeon
33116199571SPyun YongHyeon return (ENXIO);
33216199571SPyun YongHyeon }
33316199571SPyun YongHyeon
33416199571SPyun YongHyeon static void
age_get_macaddr(struct age_softc * sc)33516199571SPyun YongHyeon age_get_macaddr(struct age_softc *sc)
33616199571SPyun YongHyeon {
33706ca18c1SPyun YongHyeon uint32_t ea[2], reg;
33806ca18c1SPyun YongHyeon int i, vpdc;
33916199571SPyun YongHyeon
34016199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_SPI_CTRL);
34116199571SPyun YongHyeon if ((reg & SPI_VPD_ENB) != 0) {
34216199571SPyun YongHyeon /* Get VPD stored in TWSI EEPROM. */
34316199571SPyun YongHyeon reg &= ~SPI_VPD_ENB;
34416199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_SPI_CTRL, reg);
34516199571SPyun YongHyeon }
34616199571SPyun YongHyeon
3473b0a4aefSJohn Baldwin if (pci_find_cap(sc->age_dev, PCIY_VPD, &vpdc) == 0) {
34816199571SPyun YongHyeon /*
34906ca18c1SPyun YongHyeon * PCI VPD capability found, let TWSI reload EEPROM.
35006ca18c1SPyun YongHyeon * This will set ethernet address of controller.
35116199571SPyun YongHyeon */
35206ca18c1SPyun YongHyeon CSR_WRITE_4(sc, AGE_TWSI_CTRL, CSR_READ_4(sc, AGE_TWSI_CTRL) |
35306ca18c1SPyun YongHyeon TWSI_CTRL_SW_LD_START);
35406ca18c1SPyun YongHyeon for (i = 100; i > 0; i--) {
35506ca18c1SPyun YongHyeon DELAY(1000);
35606ca18c1SPyun YongHyeon reg = CSR_READ_4(sc, AGE_TWSI_CTRL);
35706ca18c1SPyun YongHyeon if ((reg & TWSI_CTRL_SW_LD_START) == 0)
35816199571SPyun YongHyeon break;
35916199571SPyun YongHyeon }
36006ca18c1SPyun YongHyeon if (i == 0)
36116199571SPyun YongHyeon device_printf(sc->age_dev,
36206ca18c1SPyun YongHyeon "reloading EEPROM timeout!\n");
36316199571SPyun YongHyeon } else {
364dca3a3a0SPyun YongHyeon if (bootverbose)
36516199571SPyun YongHyeon device_printf(sc->age_dev,
36616199571SPyun YongHyeon "PCI VPD capability not found!\n");
36716199571SPyun YongHyeon }
36816199571SPyun YongHyeon
36916199571SPyun YongHyeon ea[0] = CSR_READ_4(sc, AGE_PAR0);
37016199571SPyun YongHyeon ea[1] = CSR_READ_4(sc, AGE_PAR1);
37116199571SPyun YongHyeon sc->age_eaddr[0] = (ea[1] >> 8) & 0xFF;
37216199571SPyun YongHyeon sc->age_eaddr[1] = (ea[1] >> 0) & 0xFF;
37316199571SPyun YongHyeon sc->age_eaddr[2] = (ea[0] >> 24) & 0xFF;
37416199571SPyun YongHyeon sc->age_eaddr[3] = (ea[0] >> 16) & 0xFF;
37516199571SPyun YongHyeon sc->age_eaddr[4] = (ea[0] >> 8) & 0xFF;
37616199571SPyun YongHyeon sc->age_eaddr[5] = (ea[0] >> 0) & 0xFF;
37716199571SPyun YongHyeon }
37816199571SPyun YongHyeon
37916199571SPyun YongHyeon static void
age_phy_reset(struct age_softc * sc)38016199571SPyun YongHyeon age_phy_reset(struct age_softc *sc)
38116199571SPyun YongHyeon {
38206ca18c1SPyun YongHyeon uint16_t reg, pn;
38306ca18c1SPyun YongHyeon int i, linkup;
38416199571SPyun YongHyeon
38516199571SPyun YongHyeon /* Reset PHY. */
38616199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_GPHY_CTRL, GPHY_CTRL_RST);
38706ca18c1SPyun YongHyeon DELAY(2000);
38816199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_GPHY_CTRL, GPHY_CTRL_CLR);
38906ca18c1SPyun YongHyeon DELAY(2000);
39006ca18c1SPyun YongHyeon
39106ca18c1SPyun YongHyeon #define ATPHY_DBG_ADDR 0x1D
39206ca18c1SPyun YongHyeon #define ATPHY_DBG_DATA 0x1E
39306ca18c1SPyun YongHyeon #define ATPHY_CDTC 0x16
39406ca18c1SPyun YongHyeon #define PHY_CDTC_ENB 0x0001
39506ca18c1SPyun YongHyeon #define PHY_CDTC_POFF 8
39606ca18c1SPyun YongHyeon #define ATPHY_CDTS 0x1C
39706ca18c1SPyun YongHyeon #define PHY_CDTS_STAT_OK 0x0000
39806ca18c1SPyun YongHyeon #define PHY_CDTS_STAT_SHORT 0x0100
39906ca18c1SPyun YongHyeon #define PHY_CDTS_STAT_OPEN 0x0200
40006ca18c1SPyun YongHyeon #define PHY_CDTS_STAT_INVAL 0x0300
40106ca18c1SPyun YongHyeon #define PHY_CDTS_STAT_MASK 0x0300
40206ca18c1SPyun YongHyeon
40306ca18c1SPyun YongHyeon /* Check power saving mode. Magic from Linux. */
40406ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr, MII_BMCR, BMCR_RESET);
40506ca18c1SPyun YongHyeon for (linkup = 0, pn = 0; pn < 4; pn++) {
40606ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr, ATPHY_CDTC,
40706ca18c1SPyun YongHyeon (pn << PHY_CDTC_POFF) | PHY_CDTC_ENB);
40806ca18c1SPyun YongHyeon for (i = 200; i > 0; i--) {
40991216e1eSPyun YongHyeon DELAY(1000);
41006ca18c1SPyun YongHyeon reg = age_miibus_readreg(sc->age_dev, sc->age_phyaddr,
41106ca18c1SPyun YongHyeon ATPHY_CDTC);
41206ca18c1SPyun YongHyeon if ((reg & PHY_CDTC_ENB) == 0)
41306ca18c1SPyun YongHyeon break;
41406ca18c1SPyun YongHyeon }
41506ca18c1SPyun YongHyeon DELAY(1000);
41606ca18c1SPyun YongHyeon reg = age_miibus_readreg(sc->age_dev, sc->age_phyaddr,
41706ca18c1SPyun YongHyeon ATPHY_CDTS);
41806ca18c1SPyun YongHyeon if ((reg & PHY_CDTS_STAT_MASK) != PHY_CDTS_STAT_OPEN) {
41906ca18c1SPyun YongHyeon linkup++;
42006ca18c1SPyun YongHyeon break;
42106ca18c1SPyun YongHyeon }
42206ca18c1SPyun YongHyeon }
42306ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr, MII_BMCR,
42406ca18c1SPyun YongHyeon BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
42506ca18c1SPyun YongHyeon if (linkup == 0) {
42606ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
42706ca18c1SPyun YongHyeon ATPHY_DBG_ADDR, 0);
42806ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
42906ca18c1SPyun YongHyeon ATPHY_DBG_DATA, 0x124E);
43006ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
43106ca18c1SPyun YongHyeon ATPHY_DBG_ADDR, 1);
43206ca18c1SPyun YongHyeon reg = age_miibus_readreg(sc->age_dev, sc->age_phyaddr,
43306ca18c1SPyun YongHyeon ATPHY_DBG_DATA);
43406ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
43506ca18c1SPyun YongHyeon ATPHY_DBG_DATA, reg | 0x03);
43606ca18c1SPyun YongHyeon /* XXX */
43706ca18c1SPyun YongHyeon DELAY(1500 * 1000);
43806ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
43906ca18c1SPyun YongHyeon ATPHY_DBG_ADDR, 0);
44006ca18c1SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
44106ca18c1SPyun YongHyeon ATPHY_DBG_DATA, 0x024E);
44206ca18c1SPyun YongHyeon }
44306ca18c1SPyun YongHyeon
44406ca18c1SPyun YongHyeon #undef ATPHY_DBG_ADDR
44506ca18c1SPyun YongHyeon #undef ATPHY_DBG_DATA
44606ca18c1SPyun YongHyeon #undef ATPHY_CDTC
44706ca18c1SPyun YongHyeon #undef PHY_CDTC_ENB
44806ca18c1SPyun YongHyeon #undef PHY_CDTC_POFF
44906ca18c1SPyun YongHyeon #undef ATPHY_CDTS
45006ca18c1SPyun YongHyeon #undef PHY_CDTS_STAT_OK
45106ca18c1SPyun YongHyeon #undef PHY_CDTS_STAT_SHORT
45206ca18c1SPyun YongHyeon #undef PHY_CDTS_STAT_OPEN
45306ca18c1SPyun YongHyeon #undef PHY_CDTS_STAT_INVAL
45406ca18c1SPyun YongHyeon #undef PHY_CDTS_STAT_MASK
45516199571SPyun YongHyeon }
45616199571SPyun YongHyeon
45716199571SPyun YongHyeon static int
age_attach(device_t dev)45816199571SPyun YongHyeon age_attach(device_t dev)
45916199571SPyun YongHyeon {
46016199571SPyun YongHyeon struct age_softc *sc;
46152436412SJustin Hibbits if_t ifp;
46216199571SPyun YongHyeon uint16_t burst;
463*ddaf6524SJohn Baldwin int error, i, msic, msixc;
46416199571SPyun YongHyeon
46516199571SPyun YongHyeon error = 0;
46616199571SPyun YongHyeon sc = device_get_softc(dev);
46716199571SPyun YongHyeon sc->age_dev = dev;
46816199571SPyun YongHyeon
46916199571SPyun YongHyeon mtx_init(&sc->age_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
47016199571SPyun YongHyeon MTX_DEF);
47116199571SPyun YongHyeon callout_init_mtx(&sc->age_tick_ch, &sc->age_mtx, 0);
47216199571SPyun YongHyeon TASK_INIT(&sc->age_int_task, 0, age_int_task, sc);
47316199571SPyun YongHyeon TASK_INIT(&sc->age_link_task, 0, age_link_task, sc);
47416199571SPyun YongHyeon
47516199571SPyun YongHyeon /* Map the device. */
47616199571SPyun YongHyeon pci_enable_busmaster(dev);
47716199571SPyun YongHyeon sc->age_res_spec = age_res_spec_mem;
47816199571SPyun YongHyeon sc->age_irq_spec = age_irq_spec_legacy;
47916199571SPyun YongHyeon error = bus_alloc_resources(dev, sc->age_res_spec, sc->age_res);
48016199571SPyun YongHyeon if (error != 0) {
48116199571SPyun YongHyeon device_printf(dev, "cannot allocate memory resources.\n");
48216199571SPyun YongHyeon goto fail;
48316199571SPyun YongHyeon }
48416199571SPyun YongHyeon
48516199571SPyun YongHyeon /* Set PHY address. */
48616199571SPyun YongHyeon sc->age_phyaddr = AGE_PHY_ADDR;
48716199571SPyun YongHyeon
48816199571SPyun YongHyeon /* Reset PHY. */
48916199571SPyun YongHyeon age_phy_reset(sc);
49016199571SPyun YongHyeon
49116199571SPyun YongHyeon /* Reset the ethernet controller. */
49216199571SPyun YongHyeon age_reset(sc);
49316199571SPyun YongHyeon
49416199571SPyun YongHyeon /* Get PCI and chip id/revision. */
49516199571SPyun YongHyeon sc->age_rev = pci_get_revid(dev);
49616199571SPyun YongHyeon sc->age_chip_rev = CSR_READ_4(sc, AGE_MASTER_CFG) >>
49716199571SPyun YongHyeon MASTER_CHIP_REV_SHIFT;
498dca3a3a0SPyun YongHyeon if (bootverbose) {
49906ca18c1SPyun YongHyeon device_printf(dev, "PCI device revision : 0x%04x\n",
50006ca18c1SPyun YongHyeon sc->age_rev);
50116199571SPyun YongHyeon device_printf(dev, "Chip id/revision : 0x%04x\n",
50216199571SPyun YongHyeon sc->age_chip_rev);
50316199571SPyun YongHyeon }
50416199571SPyun YongHyeon
50516199571SPyun YongHyeon /*
50616199571SPyun YongHyeon * XXX
50716199571SPyun YongHyeon * Unintialized hardware returns an invalid chip id/revision
50816199571SPyun YongHyeon * as well as 0xFFFFFFFF for Tx/Rx fifo length. It seems that
50916199571SPyun YongHyeon * unplugged cable results in putting hardware into automatic
51016199571SPyun YongHyeon * power down mode which in turn returns invalld chip revision.
51116199571SPyun YongHyeon */
51216199571SPyun YongHyeon if (sc->age_chip_rev == 0xFFFF) {
51316199571SPyun YongHyeon device_printf(dev,"invalid chip revision : 0x%04x -- "
51416199571SPyun YongHyeon "not initialized?\n", sc->age_chip_rev);
51516199571SPyun YongHyeon error = ENXIO;
51616199571SPyun YongHyeon goto fail;
51716199571SPyun YongHyeon }
51816199571SPyun YongHyeon
51916199571SPyun YongHyeon device_printf(dev, "%d Tx FIFO, %d Rx FIFO\n",
52016199571SPyun YongHyeon CSR_READ_4(sc, AGE_SRAM_TX_FIFO_LEN),
52116199571SPyun YongHyeon CSR_READ_4(sc, AGE_SRAM_RX_FIFO_LEN));
52216199571SPyun YongHyeon
52316199571SPyun YongHyeon /* Allocate IRQ resources. */
52416199571SPyun YongHyeon msixc = pci_msix_count(dev);
52516199571SPyun YongHyeon msic = pci_msi_count(dev);
526dca3a3a0SPyun YongHyeon if (bootverbose) {
52716199571SPyun YongHyeon device_printf(dev, "MSIX count : %d\n", msixc);
52816199571SPyun YongHyeon device_printf(dev, "MSI count : %d\n", msic);
52916199571SPyun YongHyeon }
53016199571SPyun YongHyeon
53116199571SPyun YongHyeon /* Prefer MSIX over MSI. */
53216199571SPyun YongHyeon if (msix_disable == 0 || msi_disable == 0) {
53316199571SPyun YongHyeon if (msix_disable == 0 && msixc == AGE_MSIX_MESSAGES &&
53416199571SPyun YongHyeon pci_alloc_msix(dev, &msixc) == 0) {
53516199571SPyun YongHyeon if (msic == AGE_MSIX_MESSAGES) {
53616199571SPyun YongHyeon device_printf(dev, "Using %d MSIX messages.\n",
53716199571SPyun YongHyeon msixc);
53816199571SPyun YongHyeon sc->age_flags |= AGE_FLAG_MSIX;
53916199571SPyun YongHyeon sc->age_irq_spec = age_irq_spec_msix;
54016199571SPyun YongHyeon } else
54116199571SPyun YongHyeon pci_release_msi(dev);
54216199571SPyun YongHyeon }
54316199571SPyun YongHyeon if (msi_disable == 0 && (sc->age_flags & AGE_FLAG_MSIX) == 0 &&
54416199571SPyun YongHyeon msic == AGE_MSI_MESSAGES &&
54516199571SPyun YongHyeon pci_alloc_msi(dev, &msic) == 0) {
54616199571SPyun YongHyeon if (msic == AGE_MSI_MESSAGES) {
54716199571SPyun YongHyeon device_printf(dev, "Using %d MSI messages.\n",
54816199571SPyun YongHyeon msic);
54916199571SPyun YongHyeon sc->age_flags |= AGE_FLAG_MSI;
55016199571SPyun YongHyeon sc->age_irq_spec = age_irq_spec_msi;
55116199571SPyun YongHyeon } else
55216199571SPyun YongHyeon pci_release_msi(dev);
55316199571SPyun YongHyeon }
55416199571SPyun YongHyeon }
55516199571SPyun YongHyeon
55616199571SPyun YongHyeon error = bus_alloc_resources(dev, sc->age_irq_spec, sc->age_irq);
55716199571SPyun YongHyeon if (error != 0) {
55816199571SPyun YongHyeon device_printf(dev, "cannot allocate IRQ resources.\n");
55916199571SPyun YongHyeon goto fail;
56016199571SPyun YongHyeon }
56116199571SPyun YongHyeon
56216199571SPyun YongHyeon /* Get DMA parameters from PCIe device control register. */
5633b0a4aefSJohn Baldwin if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
56416199571SPyun YongHyeon sc->age_flags |= AGE_FLAG_PCIE;
56516199571SPyun YongHyeon burst = pci_read_config(dev, i + 0x08, 2);
56616199571SPyun YongHyeon /* Max read request size. */
56716199571SPyun YongHyeon sc->age_dma_rd_burst = ((burst >> 12) & 0x07) <<
56816199571SPyun YongHyeon DMA_CFG_RD_BURST_SHIFT;
56916199571SPyun YongHyeon /* Max payload size. */
57016199571SPyun YongHyeon sc->age_dma_wr_burst = ((burst >> 5) & 0x07) <<
57116199571SPyun YongHyeon DMA_CFG_WR_BURST_SHIFT;
572dca3a3a0SPyun YongHyeon if (bootverbose) {
57316199571SPyun YongHyeon device_printf(dev, "Read request size : %d bytes.\n",
57416199571SPyun YongHyeon 128 << ((burst >> 12) & 0x07));
57516199571SPyun YongHyeon device_printf(dev, "TLP payload size : %d bytes.\n",
57616199571SPyun YongHyeon 128 << ((burst >> 5) & 0x07));
57716199571SPyun YongHyeon }
57816199571SPyun YongHyeon } else {
57916199571SPyun YongHyeon sc->age_dma_rd_burst = DMA_CFG_RD_BURST_128;
58016199571SPyun YongHyeon sc->age_dma_wr_burst = DMA_CFG_WR_BURST_128;
58116199571SPyun YongHyeon }
58216199571SPyun YongHyeon
58316199571SPyun YongHyeon /* Create device sysctl node. */
58416199571SPyun YongHyeon age_sysctl_node(sc);
58516199571SPyun YongHyeon
5869dda5c8fSPyun YongHyeon if ((error = age_dma_alloc(sc)) != 0)
58716199571SPyun YongHyeon goto fail;
58816199571SPyun YongHyeon
58916199571SPyun YongHyeon /* Load station address. */
59016199571SPyun YongHyeon age_get_macaddr(sc);
59116199571SPyun YongHyeon
59216199571SPyun YongHyeon ifp = sc->age_ifp = if_alloc(IFT_ETHER);
59352436412SJustin Hibbits if_setsoftc(ifp, sc);
59416199571SPyun YongHyeon if_initname(ifp, device_get_name(dev), device_get_unit(dev));
59552436412SJustin Hibbits if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
59652436412SJustin Hibbits if_setioctlfn(ifp, age_ioctl);
59752436412SJustin Hibbits if_setstartfn(ifp, age_start);
59852436412SJustin Hibbits if_setinitfn(ifp, age_init);
59952436412SJustin Hibbits if_setsendqlen(ifp, AGE_TX_RING_CNT - 1);
60052436412SJustin Hibbits if_setsendqready(ifp);
60152436412SJustin Hibbits if_setcapabilities(ifp, IFCAP_HWCSUM | IFCAP_TSO4);
60252436412SJustin Hibbits if_sethwassist(ifp, AGE_CSUM_FEATURES | CSUM_TSO);
603*ddaf6524SJohn Baldwin if (pci_has_pm(dev)) {
60452436412SJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_WOL_MAGIC | IFCAP_WOL_MCAST, 0);
60516199571SPyun YongHyeon }
60652436412SJustin Hibbits if_setcapenable(ifp, if_getcapabilities(ifp));
60716199571SPyun YongHyeon
60816199571SPyun YongHyeon /* Set up MII bus. */
6098e5d93dbSMarius Strobl error = mii_attach(dev, &sc->age_miibus, ifp, age_mediachange,
6108e5d93dbSMarius Strobl age_mediastatus, BMSR_DEFCAPMASK, sc->age_phyaddr, MII_OFFSET_ANY,
6118e5d93dbSMarius Strobl 0);
6128e5d93dbSMarius Strobl if (error != 0) {
6138e5d93dbSMarius Strobl device_printf(dev, "attaching PHYs failed\n");
61416199571SPyun YongHyeon goto fail;
61516199571SPyun YongHyeon }
61616199571SPyun YongHyeon
61716199571SPyun YongHyeon ether_ifattach(ifp, sc->age_eaddr);
61816199571SPyun YongHyeon
61916199571SPyun YongHyeon /* VLAN capability setup. */
62052436412SJustin Hibbits if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
62152436412SJustin Hibbits IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO, 0);
62252436412SJustin Hibbits if_setcapenable(ifp, if_getcapabilities(ifp));
62316199571SPyun YongHyeon
62416199571SPyun YongHyeon /* Tell the upper layer(s) we support long frames. */
62552436412SJustin Hibbits if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
62616199571SPyun YongHyeon
62716199571SPyun YongHyeon /* Create local taskq. */
62816199571SPyun YongHyeon sc->age_tq = taskqueue_create_fast("age_taskq", M_WAITOK,
62916199571SPyun YongHyeon taskqueue_thread_enqueue, &sc->age_tq);
63016199571SPyun YongHyeon taskqueue_start_threads(&sc->age_tq, 1, PI_NET, "%s taskq",
63116199571SPyun YongHyeon device_get_nameunit(sc->age_dev));
63216199571SPyun YongHyeon
63316199571SPyun YongHyeon if ((sc->age_flags & AGE_FLAG_MSIX) != 0)
63416199571SPyun YongHyeon msic = AGE_MSIX_MESSAGES;
63516199571SPyun YongHyeon else if ((sc->age_flags & AGE_FLAG_MSI) != 0)
63616199571SPyun YongHyeon msic = AGE_MSI_MESSAGES;
63716199571SPyun YongHyeon else
63816199571SPyun YongHyeon msic = 1;
63916199571SPyun YongHyeon for (i = 0; i < msic; i++) {
64016199571SPyun YongHyeon error = bus_setup_intr(dev, sc->age_irq[i],
64116199571SPyun YongHyeon INTR_TYPE_NET | INTR_MPSAFE, age_intr, NULL, sc,
64216199571SPyun YongHyeon &sc->age_intrhand[i]);
64316199571SPyun YongHyeon if (error != 0)
64416199571SPyun YongHyeon break;
64516199571SPyun YongHyeon }
64616199571SPyun YongHyeon if (error != 0) {
64716199571SPyun YongHyeon device_printf(dev, "could not set up interrupt handler.\n");
64816199571SPyun YongHyeon taskqueue_free(sc->age_tq);
64916199571SPyun YongHyeon sc->age_tq = NULL;
65016199571SPyun YongHyeon ether_ifdetach(ifp);
65116199571SPyun YongHyeon goto fail;
65216199571SPyun YongHyeon }
65316199571SPyun YongHyeon
65416199571SPyun YongHyeon fail:
65516199571SPyun YongHyeon if (error != 0)
65616199571SPyun YongHyeon age_detach(dev);
65716199571SPyun YongHyeon
65816199571SPyun YongHyeon return (error);
65916199571SPyun YongHyeon }
66016199571SPyun YongHyeon
66116199571SPyun YongHyeon static int
age_detach(device_t dev)66216199571SPyun YongHyeon age_detach(device_t dev)
66316199571SPyun YongHyeon {
66416199571SPyun YongHyeon struct age_softc *sc;
66552436412SJustin Hibbits if_t ifp;
66616199571SPyun YongHyeon int i, msic;
66716199571SPyun YongHyeon
66816199571SPyun YongHyeon sc = device_get_softc(dev);
66916199571SPyun YongHyeon
67016199571SPyun YongHyeon ifp = sc->age_ifp;
67116199571SPyun YongHyeon if (device_is_attached(dev)) {
67216199571SPyun YongHyeon AGE_LOCK(sc);
67316199571SPyun YongHyeon sc->age_flags |= AGE_FLAG_DETACH;
67416199571SPyun YongHyeon age_stop(sc);
67516199571SPyun YongHyeon AGE_UNLOCK(sc);
67616199571SPyun YongHyeon callout_drain(&sc->age_tick_ch);
67716199571SPyun YongHyeon taskqueue_drain(sc->age_tq, &sc->age_int_task);
67816199571SPyun YongHyeon taskqueue_drain(taskqueue_swi, &sc->age_link_task);
67916199571SPyun YongHyeon ether_ifdetach(ifp);
68016199571SPyun YongHyeon }
68116199571SPyun YongHyeon
68216199571SPyun YongHyeon if (sc->age_tq != NULL) {
68316199571SPyun YongHyeon taskqueue_drain(sc->age_tq, &sc->age_int_task);
68416199571SPyun YongHyeon taskqueue_free(sc->age_tq);
68516199571SPyun YongHyeon sc->age_tq = NULL;
68616199571SPyun YongHyeon }
68716199571SPyun YongHyeon
68816199571SPyun YongHyeon bus_generic_detach(dev);
68916199571SPyun YongHyeon age_dma_free(sc);
69016199571SPyun YongHyeon
69116199571SPyun YongHyeon if (ifp != NULL) {
69216199571SPyun YongHyeon if_free(ifp);
69316199571SPyun YongHyeon sc->age_ifp = NULL;
69416199571SPyun YongHyeon }
69516199571SPyun YongHyeon
69616199571SPyun YongHyeon if ((sc->age_flags & AGE_FLAG_MSIX) != 0)
69716199571SPyun YongHyeon msic = AGE_MSIX_MESSAGES;
69816199571SPyun YongHyeon else if ((sc->age_flags & AGE_FLAG_MSI) != 0)
69916199571SPyun YongHyeon msic = AGE_MSI_MESSAGES;
70016199571SPyun YongHyeon else
70116199571SPyun YongHyeon msic = 1;
70216199571SPyun YongHyeon for (i = 0; i < msic; i++) {
70316199571SPyun YongHyeon if (sc->age_intrhand[i] != NULL) {
70416199571SPyun YongHyeon bus_teardown_intr(dev, sc->age_irq[i],
70516199571SPyun YongHyeon sc->age_intrhand[i]);
70616199571SPyun YongHyeon sc->age_intrhand[i] = NULL;
70716199571SPyun YongHyeon }
70816199571SPyun YongHyeon }
70916199571SPyun YongHyeon
71016199571SPyun YongHyeon bus_release_resources(dev, sc->age_irq_spec, sc->age_irq);
71116199571SPyun YongHyeon if ((sc->age_flags & (AGE_FLAG_MSI | AGE_FLAG_MSIX)) != 0)
71216199571SPyun YongHyeon pci_release_msi(dev);
71316199571SPyun YongHyeon bus_release_resources(dev, sc->age_res_spec, sc->age_res);
71416199571SPyun YongHyeon mtx_destroy(&sc->age_mtx);
71516199571SPyun YongHyeon
71616199571SPyun YongHyeon return (0);
71716199571SPyun YongHyeon }
71816199571SPyun YongHyeon
71916199571SPyun YongHyeon static void
age_sysctl_node(struct age_softc * sc)72016199571SPyun YongHyeon age_sysctl_node(struct age_softc *sc)
72116199571SPyun YongHyeon {
72216199571SPyun YongHyeon int error;
72316199571SPyun YongHyeon
72416199571SPyun YongHyeon SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->age_dev),
72516199571SPyun YongHyeon SYSCTL_CHILDREN(device_get_sysctl_tree(sc->age_dev)), OID_AUTO,
7267029da5cSPawel Biernacki "stats", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
7277029da5cSPawel Biernacki sc, 0, sysctl_age_stats, "I", "Statistics");
72816199571SPyun YongHyeon
72916199571SPyun YongHyeon SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->age_dev),
73016199571SPyun YongHyeon SYSCTL_CHILDREN(device_get_sysctl_tree(sc->age_dev)), OID_AUTO,
7317029da5cSPawel Biernacki "int_mod", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
7327029da5cSPawel Biernacki &sc->age_int_mod, 0, sysctl_hw_age_int_mod, "I",
7337029da5cSPawel Biernacki "age interrupt moderation");
73416199571SPyun YongHyeon
73516199571SPyun YongHyeon /* Pull in device tunables. */
73616199571SPyun YongHyeon sc->age_int_mod = AGE_IM_TIMER_DEFAULT;
73716199571SPyun YongHyeon error = resource_int_value(device_get_name(sc->age_dev),
73816199571SPyun YongHyeon device_get_unit(sc->age_dev), "int_mod", &sc->age_int_mod);
73916199571SPyun YongHyeon if (error == 0) {
74016199571SPyun YongHyeon if (sc->age_int_mod < AGE_IM_TIMER_MIN ||
74116199571SPyun YongHyeon sc->age_int_mod > AGE_IM_TIMER_MAX) {
74216199571SPyun YongHyeon device_printf(sc->age_dev,
74316199571SPyun YongHyeon "int_mod value out of range; using default: %d\n",
74416199571SPyun YongHyeon AGE_IM_TIMER_DEFAULT);
74516199571SPyun YongHyeon sc->age_int_mod = AGE_IM_TIMER_DEFAULT;
74616199571SPyun YongHyeon }
74716199571SPyun YongHyeon }
74816199571SPyun YongHyeon
74916199571SPyun YongHyeon SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->age_dev),
75016199571SPyun YongHyeon SYSCTL_CHILDREN(device_get_sysctl_tree(sc->age_dev)), OID_AUTO,
7517029da5cSPawel Biernacki "process_limit", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
7527029da5cSPawel Biernacki &sc->age_process_limit, 0, sysctl_hw_age_proc_limit, "I",
75316199571SPyun YongHyeon "max number of Rx events to process");
75416199571SPyun YongHyeon
75516199571SPyun YongHyeon /* Pull in device tunables. */
75616199571SPyun YongHyeon sc->age_process_limit = AGE_PROC_DEFAULT;
75716199571SPyun YongHyeon error = resource_int_value(device_get_name(sc->age_dev),
75816199571SPyun YongHyeon device_get_unit(sc->age_dev), "process_limit",
75916199571SPyun YongHyeon &sc->age_process_limit);
76016199571SPyun YongHyeon if (error == 0) {
76116199571SPyun YongHyeon if (sc->age_process_limit < AGE_PROC_MIN ||
76216199571SPyun YongHyeon sc->age_process_limit > AGE_PROC_MAX) {
76316199571SPyun YongHyeon device_printf(sc->age_dev,
76416199571SPyun YongHyeon "process_limit value out of range; "
76516199571SPyun YongHyeon "using default: %d\n", AGE_PROC_DEFAULT);
76616199571SPyun YongHyeon sc->age_process_limit = AGE_PROC_DEFAULT;
76716199571SPyun YongHyeon }
76816199571SPyun YongHyeon }
76916199571SPyun YongHyeon }
77016199571SPyun YongHyeon
77116199571SPyun YongHyeon struct age_dmamap_arg {
77216199571SPyun YongHyeon bus_addr_t age_busaddr;
77316199571SPyun YongHyeon };
77416199571SPyun YongHyeon
77516199571SPyun YongHyeon static void
age_dmamap_cb(void * arg,bus_dma_segment_t * segs,int nsegs,int error)77616199571SPyun YongHyeon age_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
77716199571SPyun YongHyeon {
77816199571SPyun YongHyeon struct age_dmamap_arg *ctx;
77916199571SPyun YongHyeon
78016199571SPyun YongHyeon if (error != 0)
78116199571SPyun YongHyeon return;
78216199571SPyun YongHyeon
78316199571SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
78416199571SPyun YongHyeon
78516199571SPyun YongHyeon ctx = (struct age_dmamap_arg *)arg;
78616199571SPyun YongHyeon ctx->age_busaddr = segs[0].ds_addr;
78716199571SPyun YongHyeon }
78816199571SPyun YongHyeon
78916199571SPyun YongHyeon /*
79016199571SPyun YongHyeon * Attansic L1 controller have single register to specify high
79116199571SPyun YongHyeon * address part of DMA blocks. So all descriptor structures and
79216199571SPyun YongHyeon * DMA memory blocks should have the same high address of given
79316199571SPyun YongHyeon * 4GB address space(i.e. crossing 4GB boundary is not allowed).
79416199571SPyun YongHyeon */
79516199571SPyun YongHyeon static int
age_check_boundary(struct age_softc * sc)79616199571SPyun YongHyeon age_check_boundary(struct age_softc *sc)
79716199571SPyun YongHyeon {
79816199571SPyun YongHyeon bus_addr_t rx_ring_end, rr_ring_end, tx_ring_end;
79916199571SPyun YongHyeon bus_addr_t cmb_block_end, smb_block_end;
80016199571SPyun YongHyeon
80116199571SPyun YongHyeon /* Tx/Rx descriptor queue should reside within 4GB boundary. */
80216199571SPyun YongHyeon tx_ring_end = sc->age_rdata.age_tx_ring_paddr + AGE_TX_RING_SZ;
80316199571SPyun YongHyeon rx_ring_end = sc->age_rdata.age_rx_ring_paddr + AGE_RX_RING_SZ;
80416199571SPyun YongHyeon rr_ring_end = sc->age_rdata.age_rr_ring_paddr + AGE_RR_RING_SZ;
80516199571SPyun YongHyeon cmb_block_end = sc->age_rdata.age_cmb_block_paddr + AGE_CMB_BLOCK_SZ;
80616199571SPyun YongHyeon smb_block_end = sc->age_rdata.age_smb_block_paddr + AGE_SMB_BLOCK_SZ;
80716199571SPyun YongHyeon
80816199571SPyun YongHyeon if ((AGE_ADDR_HI(tx_ring_end) !=
80916199571SPyun YongHyeon AGE_ADDR_HI(sc->age_rdata.age_tx_ring_paddr)) ||
81016199571SPyun YongHyeon (AGE_ADDR_HI(rx_ring_end) !=
81116199571SPyun YongHyeon AGE_ADDR_HI(sc->age_rdata.age_rx_ring_paddr)) ||
81216199571SPyun YongHyeon (AGE_ADDR_HI(rr_ring_end) !=
81316199571SPyun YongHyeon AGE_ADDR_HI(sc->age_rdata.age_rr_ring_paddr)) ||
81416199571SPyun YongHyeon (AGE_ADDR_HI(cmb_block_end) !=
81516199571SPyun YongHyeon AGE_ADDR_HI(sc->age_rdata.age_cmb_block_paddr)) ||
81616199571SPyun YongHyeon (AGE_ADDR_HI(smb_block_end) !=
81716199571SPyun YongHyeon AGE_ADDR_HI(sc->age_rdata.age_smb_block_paddr)))
81816199571SPyun YongHyeon return (EFBIG);
81916199571SPyun YongHyeon
82016199571SPyun YongHyeon if ((AGE_ADDR_HI(tx_ring_end) != AGE_ADDR_HI(rx_ring_end)) ||
82116199571SPyun YongHyeon (AGE_ADDR_HI(tx_ring_end) != AGE_ADDR_HI(rr_ring_end)) ||
82216199571SPyun YongHyeon (AGE_ADDR_HI(tx_ring_end) != AGE_ADDR_HI(cmb_block_end)) ||
82316199571SPyun YongHyeon (AGE_ADDR_HI(tx_ring_end) != AGE_ADDR_HI(smb_block_end)))
82416199571SPyun YongHyeon return (EFBIG);
82516199571SPyun YongHyeon
82616199571SPyun YongHyeon return (0);
82716199571SPyun YongHyeon }
82816199571SPyun YongHyeon
82916199571SPyun YongHyeon static int
age_dma_alloc(struct age_softc * sc)83016199571SPyun YongHyeon age_dma_alloc(struct age_softc *sc)
83116199571SPyun YongHyeon {
83216199571SPyun YongHyeon struct age_txdesc *txd;
83316199571SPyun YongHyeon struct age_rxdesc *rxd;
83416199571SPyun YongHyeon bus_addr_t lowaddr;
83516199571SPyun YongHyeon struct age_dmamap_arg ctx;
83616199571SPyun YongHyeon int error, i;
83716199571SPyun YongHyeon
83816199571SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR;
83916199571SPyun YongHyeon
84016199571SPyun YongHyeon again:
84116199571SPyun YongHyeon /* Create parent ring/DMA block tag. */
84216199571SPyun YongHyeon error = bus_dma_tag_create(
84316199571SPyun YongHyeon bus_get_dma_tag(sc->age_dev), /* parent */
84416199571SPyun YongHyeon 1, 0, /* alignment, boundary */
84516199571SPyun YongHyeon lowaddr, /* lowaddr */
84616199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
84716199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
84816199571SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
84916199571SPyun YongHyeon 0, /* nsegments */
85016199571SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
85116199571SPyun YongHyeon 0, /* flags */
85216199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
85316199571SPyun YongHyeon &sc->age_cdata.age_parent_tag);
85416199571SPyun YongHyeon if (error != 0) {
85516199571SPyun YongHyeon device_printf(sc->age_dev,
85616199571SPyun YongHyeon "could not create parent DMA tag.\n");
85716199571SPyun YongHyeon goto fail;
85816199571SPyun YongHyeon }
85916199571SPyun YongHyeon
86016199571SPyun YongHyeon /* Create tag for Tx ring. */
86116199571SPyun YongHyeon error = bus_dma_tag_create(
86216199571SPyun YongHyeon sc->age_cdata.age_parent_tag, /* parent */
86316199571SPyun YongHyeon AGE_TX_RING_ALIGN, 0, /* alignment, boundary */
86416199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
86516199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
86616199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
86716199571SPyun YongHyeon AGE_TX_RING_SZ, /* maxsize */
86816199571SPyun YongHyeon 1, /* nsegments */
86916199571SPyun YongHyeon AGE_TX_RING_SZ, /* maxsegsize */
87016199571SPyun YongHyeon 0, /* flags */
87116199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
87216199571SPyun YongHyeon &sc->age_cdata.age_tx_ring_tag);
87316199571SPyun YongHyeon if (error != 0) {
87416199571SPyun YongHyeon device_printf(sc->age_dev,
87516199571SPyun YongHyeon "could not create Tx ring DMA tag.\n");
87616199571SPyun YongHyeon goto fail;
87716199571SPyun YongHyeon }
87816199571SPyun YongHyeon
87916199571SPyun YongHyeon /* Create tag for Rx ring. */
88016199571SPyun YongHyeon error = bus_dma_tag_create(
88116199571SPyun YongHyeon sc->age_cdata.age_parent_tag, /* parent */
88216199571SPyun YongHyeon AGE_RX_RING_ALIGN, 0, /* alignment, boundary */
88316199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
88416199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
88516199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
88616199571SPyun YongHyeon AGE_RX_RING_SZ, /* maxsize */
88716199571SPyun YongHyeon 1, /* nsegments */
88816199571SPyun YongHyeon AGE_RX_RING_SZ, /* maxsegsize */
88916199571SPyun YongHyeon 0, /* flags */
89016199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
89116199571SPyun YongHyeon &sc->age_cdata.age_rx_ring_tag);
89216199571SPyun YongHyeon if (error != 0) {
89316199571SPyun YongHyeon device_printf(sc->age_dev,
89416199571SPyun YongHyeon "could not create Rx ring DMA tag.\n");
89516199571SPyun YongHyeon goto fail;
89616199571SPyun YongHyeon }
89716199571SPyun YongHyeon
89816199571SPyun YongHyeon /* Create tag for Rx return ring. */
89916199571SPyun YongHyeon error = bus_dma_tag_create(
90016199571SPyun YongHyeon sc->age_cdata.age_parent_tag, /* parent */
90116199571SPyun YongHyeon AGE_RR_RING_ALIGN, 0, /* alignment, boundary */
90216199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
90316199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
90416199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
90516199571SPyun YongHyeon AGE_RR_RING_SZ, /* maxsize */
90616199571SPyun YongHyeon 1, /* nsegments */
90716199571SPyun YongHyeon AGE_RR_RING_SZ, /* maxsegsize */
90816199571SPyun YongHyeon 0, /* flags */
90916199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
91016199571SPyun YongHyeon &sc->age_cdata.age_rr_ring_tag);
91116199571SPyun YongHyeon if (error != 0) {
91216199571SPyun YongHyeon device_printf(sc->age_dev,
91316199571SPyun YongHyeon "could not create Rx return ring DMA tag.\n");
91416199571SPyun YongHyeon goto fail;
91516199571SPyun YongHyeon }
91616199571SPyun YongHyeon
91716199571SPyun YongHyeon /* Create tag for coalesing message block. */
91816199571SPyun YongHyeon error = bus_dma_tag_create(
91916199571SPyun YongHyeon sc->age_cdata.age_parent_tag, /* parent */
92016199571SPyun YongHyeon AGE_CMB_ALIGN, 0, /* alignment, boundary */
92116199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
92216199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
92316199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
92416199571SPyun YongHyeon AGE_CMB_BLOCK_SZ, /* maxsize */
92516199571SPyun YongHyeon 1, /* nsegments */
92616199571SPyun YongHyeon AGE_CMB_BLOCK_SZ, /* maxsegsize */
92716199571SPyun YongHyeon 0, /* flags */
92816199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
92916199571SPyun YongHyeon &sc->age_cdata.age_cmb_block_tag);
93016199571SPyun YongHyeon if (error != 0) {
93116199571SPyun YongHyeon device_printf(sc->age_dev,
93216199571SPyun YongHyeon "could not create CMB DMA tag.\n");
93316199571SPyun YongHyeon goto fail;
93416199571SPyun YongHyeon }
93516199571SPyun YongHyeon
93616199571SPyun YongHyeon /* Create tag for statistics message block. */
93716199571SPyun YongHyeon error = bus_dma_tag_create(
93816199571SPyun YongHyeon sc->age_cdata.age_parent_tag, /* parent */
93916199571SPyun YongHyeon AGE_SMB_ALIGN, 0, /* alignment, boundary */
94016199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
94116199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
94216199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
94316199571SPyun YongHyeon AGE_SMB_BLOCK_SZ, /* maxsize */
94416199571SPyun YongHyeon 1, /* nsegments */
94516199571SPyun YongHyeon AGE_SMB_BLOCK_SZ, /* maxsegsize */
94616199571SPyun YongHyeon 0, /* flags */
94716199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
94816199571SPyun YongHyeon &sc->age_cdata.age_smb_block_tag);
94916199571SPyun YongHyeon if (error != 0) {
95016199571SPyun YongHyeon device_printf(sc->age_dev,
95116199571SPyun YongHyeon "could not create SMB DMA tag.\n");
95216199571SPyun YongHyeon goto fail;
95316199571SPyun YongHyeon }
95416199571SPyun YongHyeon
95516199571SPyun YongHyeon /* Allocate DMA'able memory and load the DMA map. */
95616199571SPyun YongHyeon error = bus_dmamem_alloc(sc->age_cdata.age_tx_ring_tag,
95716199571SPyun YongHyeon (void **)&sc->age_rdata.age_tx_ring,
95816199571SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
95916199571SPyun YongHyeon &sc->age_cdata.age_tx_ring_map);
96016199571SPyun YongHyeon if (error != 0) {
96116199571SPyun YongHyeon device_printf(sc->age_dev,
96216199571SPyun YongHyeon "could not allocate DMA'able memory for Tx ring.\n");
96316199571SPyun YongHyeon goto fail;
96416199571SPyun YongHyeon }
96516199571SPyun YongHyeon ctx.age_busaddr = 0;
96616199571SPyun YongHyeon error = bus_dmamap_load(sc->age_cdata.age_tx_ring_tag,
96716199571SPyun YongHyeon sc->age_cdata.age_tx_ring_map, sc->age_rdata.age_tx_ring,
96816199571SPyun YongHyeon AGE_TX_RING_SZ, age_dmamap_cb, &ctx, 0);
96916199571SPyun YongHyeon if (error != 0 || ctx.age_busaddr == 0) {
97016199571SPyun YongHyeon device_printf(sc->age_dev,
97116199571SPyun YongHyeon "could not load DMA'able memory for Tx ring.\n");
97216199571SPyun YongHyeon goto fail;
97316199571SPyun YongHyeon }
97416199571SPyun YongHyeon sc->age_rdata.age_tx_ring_paddr = ctx.age_busaddr;
97516199571SPyun YongHyeon /* Rx ring */
97616199571SPyun YongHyeon error = bus_dmamem_alloc(sc->age_cdata.age_rx_ring_tag,
97716199571SPyun YongHyeon (void **)&sc->age_rdata.age_rx_ring,
97816199571SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
97916199571SPyun YongHyeon &sc->age_cdata.age_rx_ring_map);
98016199571SPyun YongHyeon if (error != 0) {
98116199571SPyun YongHyeon device_printf(sc->age_dev,
98216199571SPyun YongHyeon "could not allocate DMA'able memory for Rx ring.\n");
98316199571SPyun YongHyeon goto fail;
98416199571SPyun YongHyeon }
98516199571SPyun YongHyeon ctx.age_busaddr = 0;
98616199571SPyun YongHyeon error = bus_dmamap_load(sc->age_cdata.age_rx_ring_tag,
98716199571SPyun YongHyeon sc->age_cdata.age_rx_ring_map, sc->age_rdata.age_rx_ring,
98816199571SPyun YongHyeon AGE_RX_RING_SZ, age_dmamap_cb, &ctx, 0);
98916199571SPyun YongHyeon if (error != 0 || ctx.age_busaddr == 0) {
99016199571SPyun YongHyeon device_printf(sc->age_dev,
99116199571SPyun YongHyeon "could not load DMA'able memory for Rx ring.\n");
99216199571SPyun YongHyeon goto fail;
99316199571SPyun YongHyeon }
99416199571SPyun YongHyeon sc->age_rdata.age_rx_ring_paddr = ctx.age_busaddr;
99516199571SPyun YongHyeon /* Rx return ring */
99616199571SPyun YongHyeon error = bus_dmamem_alloc(sc->age_cdata.age_rr_ring_tag,
99716199571SPyun YongHyeon (void **)&sc->age_rdata.age_rr_ring,
99816199571SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
99916199571SPyun YongHyeon &sc->age_cdata.age_rr_ring_map);
100016199571SPyun YongHyeon if (error != 0) {
100116199571SPyun YongHyeon device_printf(sc->age_dev,
100216199571SPyun YongHyeon "could not allocate DMA'able memory for Rx return ring.\n");
100316199571SPyun YongHyeon goto fail;
100416199571SPyun YongHyeon }
100516199571SPyun YongHyeon ctx.age_busaddr = 0;
100616199571SPyun YongHyeon error = bus_dmamap_load(sc->age_cdata.age_rr_ring_tag,
100716199571SPyun YongHyeon sc->age_cdata.age_rr_ring_map, sc->age_rdata.age_rr_ring,
100816199571SPyun YongHyeon AGE_RR_RING_SZ, age_dmamap_cb,
100916199571SPyun YongHyeon &ctx, 0);
101016199571SPyun YongHyeon if (error != 0 || ctx.age_busaddr == 0) {
101116199571SPyun YongHyeon device_printf(sc->age_dev,
101216199571SPyun YongHyeon "could not load DMA'able memory for Rx return ring.\n");
101316199571SPyun YongHyeon goto fail;
101416199571SPyun YongHyeon }
101516199571SPyun YongHyeon sc->age_rdata.age_rr_ring_paddr = ctx.age_busaddr;
101616199571SPyun YongHyeon /* CMB block */
101716199571SPyun YongHyeon error = bus_dmamem_alloc(sc->age_cdata.age_cmb_block_tag,
101816199571SPyun YongHyeon (void **)&sc->age_rdata.age_cmb_block,
101916199571SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
102016199571SPyun YongHyeon &sc->age_cdata.age_cmb_block_map);
102116199571SPyun YongHyeon if (error != 0) {
102216199571SPyun YongHyeon device_printf(sc->age_dev,
102316199571SPyun YongHyeon "could not allocate DMA'able memory for CMB block.\n");
102416199571SPyun YongHyeon goto fail;
102516199571SPyun YongHyeon }
102616199571SPyun YongHyeon ctx.age_busaddr = 0;
102716199571SPyun YongHyeon error = bus_dmamap_load(sc->age_cdata.age_cmb_block_tag,
102816199571SPyun YongHyeon sc->age_cdata.age_cmb_block_map, sc->age_rdata.age_cmb_block,
102916199571SPyun YongHyeon AGE_CMB_BLOCK_SZ, age_dmamap_cb, &ctx, 0);
103016199571SPyun YongHyeon if (error != 0 || ctx.age_busaddr == 0) {
103116199571SPyun YongHyeon device_printf(sc->age_dev,
103216199571SPyun YongHyeon "could not load DMA'able memory for CMB block.\n");
103316199571SPyun YongHyeon goto fail;
103416199571SPyun YongHyeon }
103516199571SPyun YongHyeon sc->age_rdata.age_cmb_block_paddr = ctx.age_busaddr;
103616199571SPyun YongHyeon /* SMB block */
103716199571SPyun YongHyeon error = bus_dmamem_alloc(sc->age_cdata.age_smb_block_tag,
103816199571SPyun YongHyeon (void **)&sc->age_rdata.age_smb_block,
103916199571SPyun YongHyeon BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
104016199571SPyun YongHyeon &sc->age_cdata.age_smb_block_map);
104116199571SPyun YongHyeon if (error != 0) {
104216199571SPyun YongHyeon device_printf(sc->age_dev,
104316199571SPyun YongHyeon "could not allocate DMA'able memory for SMB block.\n");
104416199571SPyun YongHyeon goto fail;
104516199571SPyun YongHyeon }
104616199571SPyun YongHyeon ctx.age_busaddr = 0;
104716199571SPyun YongHyeon error = bus_dmamap_load(sc->age_cdata.age_smb_block_tag,
104816199571SPyun YongHyeon sc->age_cdata.age_smb_block_map, sc->age_rdata.age_smb_block,
104916199571SPyun YongHyeon AGE_SMB_BLOCK_SZ, age_dmamap_cb, &ctx, 0);
105016199571SPyun YongHyeon if (error != 0 || ctx.age_busaddr == 0) {
105116199571SPyun YongHyeon device_printf(sc->age_dev,
105216199571SPyun YongHyeon "could not load DMA'able memory for SMB block.\n");
105316199571SPyun YongHyeon goto fail;
105416199571SPyun YongHyeon }
105516199571SPyun YongHyeon sc->age_rdata.age_smb_block_paddr = ctx.age_busaddr;
105616199571SPyun YongHyeon
105716199571SPyun YongHyeon /*
105816199571SPyun YongHyeon * All ring buffer and DMA blocks should have the same
105916199571SPyun YongHyeon * high address part of 64bit DMA address space.
106016199571SPyun YongHyeon */
106116199571SPyun YongHyeon if (lowaddr != BUS_SPACE_MAXADDR_32BIT &&
106216199571SPyun YongHyeon (error = age_check_boundary(sc)) != 0) {
106316199571SPyun YongHyeon device_printf(sc->age_dev, "4GB boundary crossed, "
106416199571SPyun YongHyeon "switching to 32bit DMA addressing mode.\n");
106516199571SPyun YongHyeon age_dma_free(sc);
106616199571SPyun YongHyeon /* Limit DMA address space to 32bit and try again. */
106716199571SPyun YongHyeon lowaddr = BUS_SPACE_MAXADDR_32BIT;
106816199571SPyun YongHyeon goto again;
106916199571SPyun YongHyeon }
107016199571SPyun YongHyeon
107116199571SPyun YongHyeon /*
107216199571SPyun YongHyeon * Create Tx/Rx buffer parent tag.
107316199571SPyun YongHyeon * L1 supports full 64bit DMA addressing in Tx/Rx buffers
107416199571SPyun YongHyeon * so it needs separate parent DMA tag.
1075525e4097SPyun YongHyeon * XXX
1076525e4097SPyun YongHyeon * It seems enabling 64bit DMA causes data corruption. Limit
1077525e4097SPyun YongHyeon * DMA address space to 32bit.
107816199571SPyun YongHyeon */
107916199571SPyun YongHyeon error = bus_dma_tag_create(
108016199571SPyun YongHyeon bus_get_dma_tag(sc->age_dev), /* parent */
108116199571SPyun YongHyeon 1, 0, /* alignment, boundary */
1082525e4097SPyun YongHyeon BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
108316199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
108416199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
108516199571SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
108616199571SPyun YongHyeon 0, /* nsegments */
108716199571SPyun YongHyeon BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
108816199571SPyun YongHyeon 0, /* flags */
108916199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
109016199571SPyun YongHyeon &sc->age_cdata.age_buffer_tag);
109116199571SPyun YongHyeon if (error != 0) {
109216199571SPyun YongHyeon device_printf(sc->age_dev,
109316199571SPyun YongHyeon "could not create parent buffer DMA tag.\n");
109416199571SPyun YongHyeon goto fail;
109516199571SPyun YongHyeon }
109616199571SPyun YongHyeon
109716199571SPyun YongHyeon /* Create tag for Tx buffers. */
109816199571SPyun YongHyeon error = bus_dma_tag_create(
109916199571SPyun YongHyeon sc->age_cdata.age_buffer_tag, /* parent */
110016199571SPyun YongHyeon 1, 0, /* alignment, boundary */
110116199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
110216199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
110316199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
110416199571SPyun YongHyeon AGE_TSO_MAXSIZE, /* maxsize */
110516199571SPyun YongHyeon AGE_MAXTXSEGS, /* nsegments */
110616199571SPyun YongHyeon AGE_TSO_MAXSEGSIZE, /* maxsegsize */
110716199571SPyun YongHyeon 0, /* flags */
110816199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
110916199571SPyun YongHyeon &sc->age_cdata.age_tx_tag);
111016199571SPyun YongHyeon if (error != 0) {
111116199571SPyun YongHyeon device_printf(sc->age_dev, "could not create Tx DMA tag.\n");
111216199571SPyun YongHyeon goto fail;
111316199571SPyun YongHyeon }
111416199571SPyun YongHyeon
111516199571SPyun YongHyeon /* Create tag for Rx buffers. */
111616199571SPyun YongHyeon error = bus_dma_tag_create(
111716199571SPyun YongHyeon sc->age_cdata.age_buffer_tag, /* parent */
1118088dd4b7SPyun YongHyeon AGE_RX_BUF_ALIGN, 0, /* alignment, boundary */
111916199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* lowaddr */
112016199571SPyun YongHyeon BUS_SPACE_MAXADDR, /* highaddr */
112116199571SPyun YongHyeon NULL, NULL, /* filter, filterarg */
112216199571SPyun YongHyeon MCLBYTES, /* maxsize */
112316199571SPyun YongHyeon 1, /* nsegments */
112416199571SPyun YongHyeon MCLBYTES, /* maxsegsize */
112516199571SPyun YongHyeon 0, /* flags */
112616199571SPyun YongHyeon NULL, NULL, /* lockfunc, lockarg */
112716199571SPyun YongHyeon &sc->age_cdata.age_rx_tag);
112816199571SPyun YongHyeon if (error != 0) {
112916199571SPyun YongHyeon device_printf(sc->age_dev, "could not create Rx DMA tag.\n");
113016199571SPyun YongHyeon goto fail;
113116199571SPyun YongHyeon }
113216199571SPyun YongHyeon
113316199571SPyun YongHyeon /* Create DMA maps for Tx buffers. */
113416199571SPyun YongHyeon for (i = 0; i < AGE_TX_RING_CNT; i++) {
113516199571SPyun YongHyeon txd = &sc->age_cdata.age_txdesc[i];
113616199571SPyun YongHyeon txd->tx_m = NULL;
113716199571SPyun YongHyeon txd->tx_dmamap = NULL;
113816199571SPyun YongHyeon error = bus_dmamap_create(sc->age_cdata.age_tx_tag, 0,
113916199571SPyun YongHyeon &txd->tx_dmamap);
114016199571SPyun YongHyeon if (error != 0) {
114116199571SPyun YongHyeon device_printf(sc->age_dev,
114216199571SPyun YongHyeon "could not create Tx dmamap.\n");
114316199571SPyun YongHyeon goto fail;
114416199571SPyun YongHyeon }
114516199571SPyun YongHyeon }
114616199571SPyun YongHyeon /* Create DMA maps for Rx buffers. */
114716199571SPyun YongHyeon if ((error = bus_dmamap_create(sc->age_cdata.age_rx_tag, 0,
114816199571SPyun YongHyeon &sc->age_cdata.age_rx_sparemap)) != 0) {
114916199571SPyun YongHyeon device_printf(sc->age_dev,
115016199571SPyun YongHyeon "could not create spare Rx dmamap.\n");
115116199571SPyun YongHyeon goto fail;
115216199571SPyun YongHyeon }
115316199571SPyun YongHyeon for (i = 0; i < AGE_RX_RING_CNT; i++) {
115416199571SPyun YongHyeon rxd = &sc->age_cdata.age_rxdesc[i];
115516199571SPyun YongHyeon rxd->rx_m = NULL;
115616199571SPyun YongHyeon rxd->rx_dmamap = NULL;
115716199571SPyun YongHyeon error = bus_dmamap_create(sc->age_cdata.age_rx_tag, 0,
115816199571SPyun YongHyeon &rxd->rx_dmamap);
115916199571SPyun YongHyeon if (error != 0) {
116016199571SPyun YongHyeon device_printf(sc->age_dev,
116116199571SPyun YongHyeon "could not create Rx dmamap.\n");
116216199571SPyun YongHyeon goto fail;
116316199571SPyun YongHyeon }
116416199571SPyun YongHyeon }
116516199571SPyun YongHyeon
116616199571SPyun YongHyeon fail:
116716199571SPyun YongHyeon return (error);
116816199571SPyun YongHyeon }
116916199571SPyun YongHyeon
117016199571SPyun YongHyeon static void
age_dma_free(struct age_softc * sc)117116199571SPyun YongHyeon age_dma_free(struct age_softc *sc)
117216199571SPyun YongHyeon {
117316199571SPyun YongHyeon struct age_txdesc *txd;
117416199571SPyun YongHyeon struct age_rxdesc *rxd;
117516199571SPyun YongHyeon int i;
117616199571SPyun YongHyeon
117716199571SPyun YongHyeon /* Tx buffers */
117816199571SPyun YongHyeon if (sc->age_cdata.age_tx_tag != NULL) {
117916199571SPyun YongHyeon for (i = 0; i < AGE_TX_RING_CNT; i++) {
118016199571SPyun YongHyeon txd = &sc->age_cdata.age_txdesc[i];
118116199571SPyun YongHyeon if (txd->tx_dmamap != NULL) {
118216199571SPyun YongHyeon bus_dmamap_destroy(sc->age_cdata.age_tx_tag,
118316199571SPyun YongHyeon txd->tx_dmamap);
118416199571SPyun YongHyeon txd->tx_dmamap = NULL;
118516199571SPyun YongHyeon }
118616199571SPyun YongHyeon }
118716199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_tx_tag);
118816199571SPyun YongHyeon sc->age_cdata.age_tx_tag = NULL;
118916199571SPyun YongHyeon }
119016199571SPyun YongHyeon /* Rx buffers */
119116199571SPyun YongHyeon if (sc->age_cdata.age_rx_tag != NULL) {
119216199571SPyun YongHyeon for (i = 0; i < AGE_RX_RING_CNT; i++) {
119316199571SPyun YongHyeon rxd = &sc->age_cdata.age_rxdesc[i];
119416199571SPyun YongHyeon if (rxd->rx_dmamap != NULL) {
119516199571SPyun YongHyeon bus_dmamap_destroy(sc->age_cdata.age_rx_tag,
119616199571SPyun YongHyeon rxd->rx_dmamap);
119716199571SPyun YongHyeon rxd->rx_dmamap = NULL;
119816199571SPyun YongHyeon }
119916199571SPyun YongHyeon }
120016199571SPyun YongHyeon if (sc->age_cdata.age_rx_sparemap != NULL) {
120116199571SPyun YongHyeon bus_dmamap_destroy(sc->age_cdata.age_rx_tag,
120216199571SPyun YongHyeon sc->age_cdata.age_rx_sparemap);
120316199571SPyun YongHyeon sc->age_cdata.age_rx_sparemap = NULL;
120416199571SPyun YongHyeon }
120516199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_rx_tag);
120616199571SPyun YongHyeon sc->age_cdata.age_rx_tag = NULL;
120716199571SPyun YongHyeon }
120816199571SPyun YongHyeon /* Tx ring. */
120916199571SPyun YongHyeon if (sc->age_cdata.age_tx_ring_tag != NULL) {
1210068d8643SJohn Baldwin if (sc->age_rdata.age_tx_ring_paddr != 0)
121116199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_tx_ring_tag,
121216199571SPyun YongHyeon sc->age_cdata.age_tx_ring_map);
1213068d8643SJohn Baldwin if (sc->age_rdata.age_tx_ring != NULL)
121416199571SPyun YongHyeon bus_dmamem_free(sc->age_cdata.age_tx_ring_tag,
121516199571SPyun YongHyeon sc->age_rdata.age_tx_ring,
121616199571SPyun YongHyeon sc->age_cdata.age_tx_ring_map);
1217068d8643SJohn Baldwin sc->age_rdata.age_tx_ring_paddr = 0;
121816199571SPyun YongHyeon sc->age_rdata.age_tx_ring = NULL;
121916199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_tx_ring_tag);
122016199571SPyun YongHyeon sc->age_cdata.age_tx_ring_tag = NULL;
122116199571SPyun YongHyeon }
122216199571SPyun YongHyeon /* Rx ring. */
122316199571SPyun YongHyeon if (sc->age_cdata.age_rx_ring_tag != NULL) {
1224068d8643SJohn Baldwin if (sc->age_rdata.age_rx_ring_paddr != 0)
122516199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_rx_ring_tag,
122616199571SPyun YongHyeon sc->age_cdata.age_rx_ring_map);
1227068d8643SJohn Baldwin if (sc->age_rdata.age_rx_ring != NULL)
122816199571SPyun YongHyeon bus_dmamem_free(sc->age_cdata.age_rx_ring_tag,
122916199571SPyun YongHyeon sc->age_rdata.age_rx_ring,
123016199571SPyun YongHyeon sc->age_cdata.age_rx_ring_map);
1231068d8643SJohn Baldwin sc->age_rdata.age_rx_ring_paddr = 0;
123216199571SPyun YongHyeon sc->age_rdata.age_rx_ring = NULL;
123316199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_rx_ring_tag);
123416199571SPyun YongHyeon sc->age_cdata.age_rx_ring_tag = NULL;
123516199571SPyun YongHyeon }
123616199571SPyun YongHyeon /* Rx return ring. */
123716199571SPyun YongHyeon if (sc->age_cdata.age_rr_ring_tag != NULL) {
1238068d8643SJohn Baldwin if (sc->age_rdata.age_rr_ring_paddr != 0)
123916199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_rr_ring_tag,
124016199571SPyun YongHyeon sc->age_cdata.age_rr_ring_map);
1241068d8643SJohn Baldwin if (sc->age_rdata.age_rr_ring != NULL)
124216199571SPyun YongHyeon bus_dmamem_free(sc->age_cdata.age_rr_ring_tag,
124316199571SPyun YongHyeon sc->age_rdata.age_rr_ring,
124416199571SPyun YongHyeon sc->age_cdata.age_rr_ring_map);
1245068d8643SJohn Baldwin sc->age_rdata.age_rr_ring_paddr = 0;
124616199571SPyun YongHyeon sc->age_rdata.age_rr_ring = NULL;
124716199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_rr_ring_tag);
124816199571SPyun YongHyeon sc->age_cdata.age_rr_ring_tag = NULL;
124916199571SPyun YongHyeon }
125016199571SPyun YongHyeon /* CMB block */
125116199571SPyun YongHyeon if (sc->age_cdata.age_cmb_block_tag != NULL) {
1252068d8643SJohn Baldwin if (sc->age_rdata.age_cmb_block_paddr != 0)
125316199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_cmb_block_tag,
125416199571SPyun YongHyeon sc->age_cdata.age_cmb_block_map);
1255068d8643SJohn Baldwin if (sc->age_rdata.age_cmb_block != NULL)
125616199571SPyun YongHyeon bus_dmamem_free(sc->age_cdata.age_cmb_block_tag,
125716199571SPyun YongHyeon sc->age_rdata.age_cmb_block,
125816199571SPyun YongHyeon sc->age_cdata.age_cmb_block_map);
1259068d8643SJohn Baldwin sc->age_rdata.age_cmb_block_paddr = 0;
126016199571SPyun YongHyeon sc->age_rdata.age_cmb_block = NULL;
126116199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_cmb_block_tag);
126216199571SPyun YongHyeon sc->age_cdata.age_cmb_block_tag = NULL;
126316199571SPyun YongHyeon }
126416199571SPyun YongHyeon /* SMB block */
126516199571SPyun YongHyeon if (sc->age_cdata.age_smb_block_tag != NULL) {
1266068d8643SJohn Baldwin if (sc->age_rdata.age_smb_block_paddr != 0)
126716199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_smb_block_tag,
126816199571SPyun YongHyeon sc->age_cdata.age_smb_block_map);
1269068d8643SJohn Baldwin if (sc->age_rdata.age_smb_block != NULL)
127016199571SPyun YongHyeon bus_dmamem_free(sc->age_cdata.age_smb_block_tag,
127116199571SPyun YongHyeon sc->age_rdata.age_smb_block,
127216199571SPyun YongHyeon sc->age_cdata.age_smb_block_map);
1273068d8643SJohn Baldwin sc->age_rdata.age_smb_block_paddr = 0;
127416199571SPyun YongHyeon sc->age_rdata.age_smb_block = NULL;
127516199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_smb_block_tag);
127616199571SPyun YongHyeon sc->age_cdata.age_smb_block_tag = NULL;
127716199571SPyun YongHyeon }
127816199571SPyun YongHyeon
127916199571SPyun YongHyeon if (sc->age_cdata.age_buffer_tag != NULL) {
128016199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_buffer_tag);
128116199571SPyun YongHyeon sc->age_cdata.age_buffer_tag = NULL;
128216199571SPyun YongHyeon }
128316199571SPyun YongHyeon if (sc->age_cdata.age_parent_tag != NULL) {
128416199571SPyun YongHyeon bus_dma_tag_destroy(sc->age_cdata.age_parent_tag);
128516199571SPyun YongHyeon sc->age_cdata.age_parent_tag = NULL;
128616199571SPyun YongHyeon }
128716199571SPyun YongHyeon }
128816199571SPyun YongHyeon
128916199571SPyun YongHyeon /*
129016199571SPyun YongHyeon * Make sure the interface is stopped at reboot time.
129116199571SPyun YongHyeon */
129216199571SPyun YongHyeon static int
age_shutdown(device_t dev)129316199571SPyun YongHyeon age_shutdown(device_t dev)
129416199571SPyun YongHyeon {
129516199571SPyun YongHyeon
129616199571SPyun YongHyeon return (age_suspend(dev));
129716199571SPyun YongHyeon }
129816199571SPyun YongHyeon
129916199571SPyun YongHyeon static void
age_setwol(struct age_softc * sc)130016199571SPyun YongHyeon age_setwol(struct age_softc *sc)
130116199571SPyun YongHyeon {
130252436412SJustin Hibbits if_t ifp;
130316199571SPyun YongHyeon struct mii_data *mii;
130416199571SPyun YongHyeon uint32_t reg, pmcs;
1305*ddaf6524SJohn Baldwin int aneg, i;
130616199571SPyun YongHyeon
130716199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
130816199571SPyun YongHyeon
1309*ddaf6524SJohn Baldwin if (!pci_has_pm(sc->age_dev)) {
131016199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_WOL_CFG, 0);
131116199571SPyun YongHyeon /*
131216199571SPyun YongHyeon * No PME capability, PHY power down.
131316199571SPyun YongHyeon * XXX
131416199571SPyun YongHyeon * Due to an unknown reason powering down PHY resulted
131516199571SPyun YongHyeon * in unexpected results such as inaccessbility of
131616199571SPyun YongHyeon * hardware of freshly rebooted system. Disable
131716199571SPyun YongHyeon * powering down PHY until I got more information for
131816199571SPyun YongHyeon * Attansic/Atheros PHY hardwares.
131916199571SPyun YongHyeon */
132016199571SPyun YongHyeon #ifdef notyet
132116199571SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
132216199571SPyun YongHyeon MII_BMCR, BMCR_PDOWN);
132316199571SPyun YongHyeon #endif
132416199571SPyun YongHyeon return;
132516199571SPyun YongHyeon }
132616199571SPyun YongHyeon
132716199571SPyun YongHyeon ifp = sc->age_ifp;
132852436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL) != 0) {
132916199571SPyun YongHyeon /*
133016199571SPyun YongHyeon * Note, this driver resets the link speed to 10/100Mbps with
133116199571SPyun YongHyeon * auto-negotiation but we don't know whether that operation
133216199571SPyun YongHyeon * would succeed or not as it have no control after powering
133316199571SPyun YongHyeon * off. If the renegotiation fail WOL may not work. Running
133416199571SPyun YongHyeon * at 1Gbps will draw more power than 375mA at 3.3V which is
133516199571SPyun YongHyeon * specified in PCI specification and that would result in
133616199571SPyun YongHyeon * complete shutdowning power to ethernet controller.
133716199571SPyun YongHyeon *
133816199571SPyun YongHyeon * TODO
133916199571SPyun YongHyeon * Save current negotiated media speed/duplex/flow-control
134016199571SPyun YongHyeon * to softc and restore the same link again after resuming.
134116199571SPyun YongHyeon * PHY handling such as power down/resetting to 100Mbps
134216199571SPyun YongHyeon * may be better handled in suspend method in phy driver.
134316199571SPyun YongHyeon */
134416199571SPyun YongHyeon mii = device_get_softc(sc->age_miibus);
134516199571SPyun YongHyeon mii_pollstat(mii);
134616199571SPyun YongHyeon aneg = 0;
134716199571SPyun YongHyeon if ((mii->mii_media_status & IFM_AVALID) != 0) {
134816199571SPyun YongHyeon switch IFM_SUBTYPE(mii->mii_media_active) {
134916199571SPyun YongHyeon case IFM_10_T:
135016199571SPyun YongHyeon case IFM_100_TX:
135116199571SPyun YongHyeon goto got_link;
135216199571SPyun YongHyeon case IFM_1000_T:
135316199571SPyun YongHyeon aneg++;
135416199571SPyun YongHyeon default:
135516199571SPyun YongHyeon break;
135616199571SPyun YongHyeon }
135716199571SPyun YongHyeon }
135816199571SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
135916199571SPyun YongHyeon MII_100T2CR, 0);
136016199571SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
136116199571SPyun YongHyeon MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD |
136216199571SPyun YongHyeon ANAR_10 | ANAR_CSMA);
136316199571SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
136416199571SPyun YongHyeon MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
136516199571SPyun YongHyeon DELAY(1000);
136616199571SPyun YongHyeon if (aneg != 0) {
13677cdd50e1SKevin Lo /* Poll link state until age(4) get a 10/100 link. */
136816199571SPyun YongHyeon for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
136916199571SPyun YongHyeon mii_pollstat(mii);
137016199571SPyun YongHyeon if ((mii->mii_media_status & IFM_AVALID) != 0) {
137116199571SPyun YongHyeon switch (IFM_SUBTYPE(
137216199571SPyun YongHyeon mii->mii_media_active)) {
137316199571SPyun YongHyeon case IFM_10_T:
137416199571SPyun YongHyeon case IFM_100_TX:
137516199571SPyun YongHyeon age_mac_config(sc);
137616199571SPyun YongHyeon goto got_link;
137716199571SPyun YongHyeon default:
137816199571SPyun YongHyeon break;
137916199571SPyun YongHyeon }
138016199571SPyun YongHyeon }
138116199571SPyun YongHyeon AGE_UNLOCK(sc);
138216199571SPyun YongHyeon pause("agelnk", hz);
138316199571SPyun YongHyeon AGE_LOCK(sc);
138416199571SPyun YongHyeon }
138516199571SPyun YongHyeon if (i == MII_ANEGTICKS_GIGE)
138616199571SPyun YongHyeon device_printf(sc->age_dev,
138716199571SPyun YongHyeon "establishing link failed, "
138816199571SPyun YongHyeon "WOL may not work!");
138916199571SPyun YongHyeon }
139016199571SPyun YongHyeon /*
139116199571SPyun YongHyeon * No link, force MAC to have 100Mbps, full-duplex link.
139216199571SPyun YongHyeon * This is the last resort and may/may not work.
139316199571SPyun YongHyeon */
139416199571SPyun YongHyeon mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
139516199571SPyun YongHyeon mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
139616199571SPyun YongHyeon age_mac_config(sc);
139716199571SPyun YongHyeon }
139816199571SPyun YongHyeon
139916199571SPyun YongHyeon got_link:
140016199571SPyun YongHyeon pmcs = 0;
140152436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
140216199571SPyun YongHyeon pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
140316199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_WOL_CFG, pmcs);
140416199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MAC_CFG);
140516199571SPyun YongHyeon reg &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC);
140616199571SPyun YongHyeon reg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST);
140752436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL_MCAST) != 0)
140816199571SPyun YongHyeon reg |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
140952436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL) != 0) {
141016199571SPyun YongHyeon reg |= MAC_CFG_RX_ENB;
141116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
141216199571SPyun YongHyeon }
141316199571SPyun YongHyeon
141416199571SPyun YongHyeon /* Request PME. */
141552436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
1416*ddaf6524SJohn Baldwin pci_enable_pme(sc->age_dev);
141716199571SPyun YongHyeon #ifdef notyet
141816199571SPyun YongHyeon /* See above for powering down PHY issues. */
141952436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_WOL) == 0) {
142016199571SPyun YongHyeon /* No WOL, PHY power down. */
142116199571SPyun YongHyeon age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
142216199571SPyun YongHyeon MII_BMCR, BMCR_PDOWN);
142316199571SPyun YongHyeon }
142416199571SPyun YongHyeon #endif
142516199571SPyun YongHyeon }
142616199571SPyun YongHyeon
142716199571SPyun YongHyeon static int
age_suspend(device_t dev)142816199571SPyun YongHyeon age_suspend(device_t dev)
142916199571SPyun YongHyeon {
143016199571SPyun YongHyeon struct age_softc *sc;
143116199571SPyun YongHyeon
143216199571SPyun YongHyeon sc = device_get_softc(dev);
143316199571SPyun YongHyeon
143416199571SPyun YongHyeon AGE_LOCK(sc);
143516199571SPyun YongHyeon age_stop(sc);
143616199571SPyun YongHyeon age_setwol(sc);
143716199571SPyun YongHyeon AGE_UNLOCK(sc);
143816199571SPyun YongHyeon
143916199571SPyun YongHyeon return (0);
144016199571SPyun YongHyeon }
144116199571SPyun YongHyeon
144216199571SPyun YongHyeon static int
age_resume(device_t dev)144316199571SPyun YongHyeon age_resume(device_t dev)
144416199571SPyun YongHyeon {
144516199571SPyun YongHyeon struct age_softc *sc;
144652436412SJustin Hibbits if_t ifp;
144716199571SPyun YongHyeon
144816199571SPyun YongHyeon sc = device_get_softc(dev);
144916199571SPyun YongHyeon
145016199571SPyun YongHyeon AGE_LOCK(sc);
145106ca18c1SPyun YongHyeon age_phy_reset(sc);
145216199571SPyun YongHyeon ifp = sc->age_ifp;
145352436412SJustin Hibbits if ((if_getflags(ifp) & IFF_UP) != 0)
145416199571SPyun YongHyeon age_init_locked(sc);
145516199571SPyun YongHyeon
145616199571SPyun YongHyeon AGE_UNLOCK(sc);
145716199571SPyun YongHyeon
145816199571SPyun YongHyeon return (0);
145916199571SPyun YongHyeon }
146016199571SPyun YongHyeon
146116199571SPyun YongHyeon static int
age_encap(struct age_softc * sc,struct mbuf ** m_head)146216199571SPyun YongHyeon age_encap(struct age_softc *sc, struct mbuf **m_head)
146316199571SPyun YongHyeon {
146416199571SPyun YongHyeon struct age_txdesc *txd, *txd_last;
146516199571SPyun YongHyeon struct tx_desc *desc;
146616199571SPyun YongHyeon struct mbuf *m;
146716199571SPyun YongHyeon struct ip *ip;
146816199571SPyun YongHyeon struct tcphdr *tcp;
146916199571SPyun YongHyeon bus_dma_segment_t txsegs[AGE_MAXTXSEGS];
147016199571SPyun YongHyeon bus_dmamap_t map;
14719bdff6ffSPyun YongHyeon uint32_t cflags, hdrlen, ip_off, poff, vtag;
147216199571SPyun YongHyeon int error, i, nsegs, prod, si;
147316199571SPyun YongHyeon
147416199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
147516199571SPyun YongHyeon
147616199571SPyun YongHyeon M_ASSERTPKTHDR((*m_head));
147716199571SPyun YongHyeon
147816199571SPyun YongHyeon m = *m_head;
147916199571SPyun YongHyeon ip = NULL;
148016199571SPyun YongHyeon tcp = NULL;
148116199571SPyun YongHyeon cflags = vtag = 0;
148216199571SPyun YongHyeon ip_off = poff = 0;
148316199571SPyun YongHyeon if ((m->m_pkthdr.csum_flags & (AGE_CSUM_FEATURES | CSUM_TSO)) != 0) {
148416199571SPyun YongHyeon /*
148516199571SPyun YongHyeon * L1 requires offset of TCP/UDP payload in its Tx
148616199571SPyun YongHyeon * descriptor to perform hardware Tx checksum offload.
148716199571SPyun YongHyeon * Additionally, TSO requires IP/TCP header size and
148816199571SPyun YongHyeon * modification of IP/TCP header in order to make TSO
148916199571SPyun YongHyeon * engine work. This kind of operation takes many CPU
149016199571SPyun YongHyeon * cycles on FreeBSD so fast host CPU is needed to get
149116199571SPyun YongHyeon * smooth TSO performance.
149216199571SPyun YongHyeon */
149316199571SPyun YongHyeon struct ether_header *eh;
149416199571SPyun YongHyeon
149516199571SPyun YongHyeon if (M_WRITABLE(m) == 0) {
149616199571SPyun YongHyeon /* Get a writable copy. */
1497c6499eccSGleb Smirnoff m = m_dup(*m_head, M_NOWAIT);
149816199571SPyun YongHyeon /* Release original mbufs. */
149916199571SPyun YongHyeon m_freem(*m_head);
150016199571SPyun YongHyeon if (m == NULL) {
150116199571SPyun YongHyeon *m_head = NULL;
150216199571SPyun YongHyeon return (ENOBUFS);
150316199571SPyun YongHyeon }
150416199571SPyun YongHyeon *m_head = m;
150516199571SPyun YongHyeon }
150616199571SPyun YongHyeon ip_off = sizeof(struct ether_header);
150716199571SPyun YongHyeon m = m_pullup(m, ip_off);
150816199571SPyun YongHyeon if (m == NULL) {
150916199571SPyun YongHyeon *m_head = NULL;
151016199571SPyun YongHyeon return (ENOBUFS);
151116199571SPyun YongHyeon }
151216199571SPyun YongHyeon eh = mtod(m, struct ether_header *);
151316199571SPyun YongHyeon /*
151416199571SPyun YongHyeon * Check if hardware VLAN insertion is off.
151516199571SPyun YongHyeon * Additional check for LLC/SNAP frame?
151616199571SPyun YongHyeon */
151716199571SPyun YongHyeon if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
151816199571SPyun YongHyeon ip_off = sizeof(struct ether_vlan_header);
151916199571SPyun YongHyeon m = m_pullup(m, ip_off);
152016199571SPyun YongHyeon if (m == NULL) {
152116199571SPyun YongHyeon *m_head = NULL;
152216199571SPyun YongHyeon return (ENOBUFS);
152316199571SPyun YongHyeon }
152416199571SPyun YongHyeon }
152516199571SPyun YongHyeon m = m_pullup(m, ip_off + sizeof(struct ip));
152616199571SPyun YongHyeon if (m == NULL) {
152716199571SPyun YongHyeon *m_head = NULL;
152816199571SPyun YongHyeon return (ENOBUFS);
152916199571SPyun YongHyeon }
153016199571SPyun YongHyeon ip = (struct ip *)(mtod(m, char *) + ip_off);
153116199571SPyun YongHyeon poff = ip_off + (ip->ip_hl << 2);
153216199571SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
153316199571SPyun YongHyeon m = m_pullup(m, poff + sizeof(struct tcphdr));
153416199571SPyun YongHyeon if (m == NULL) {
153516199571SPyun YongHyeon *m_head = NULL;
153616199571SPyun YongHyeon return (ENOBUFS);
153716199571SPyun YongHyeon }
153816199571SPyun YongHyeon tcp = (struct tcphdr *)(mtod(m, char *) + poff);
15399bdff6ffSPyun YongHyeon m = m_pullup(m, poff + (tcp->th_off << 2));
15409bdff6ffSPyun YongHyeon if (m == NULL) {
15419bdff6ffSPyun YongHyeon *m_head = NULL;
15429bdff6ffSPyun YongHyeon return (ENOBUFS);
15439bdff6ffSPyun YongHyeon }
154416199571SPyun YongHyeon /*
154516199571SPyun YongHyeon * L1 requires IP/TCP header size and offset as
154616199571SPyun YongHyeon * well as TCP pseudo checksum which complicates
154716199571SPyun YongHyeon * TSO configuration. I guess this comes from the
154816199571SPyun YongHyeon * adherence to Microsoft NDIS Large Send
154916199571SPyun YongHyeon * specification which requires insertion of
155016199571SPyun YongHyeon * pseudo checksum by upper stack. The pseudo
155116199571SPyun YongHyeon * checksum that NDIS refers to doesn't include
155216199571SPyun YongHyeon * TCP payload length so age(4) should recompute
155316199571SPyun YongHyeon * the pseudo checksum here. Hopefully this wouldn't
155416199571SPyun YongHyeon * be much burden on modern CPUs.
155516199571SPyun YongHyeon * Reset IP checksum and recompute TCP pseudo
155616199571SPyun YongHyeon * checksum as NDIS specification said.
155716199571SPyun YongHyeon */
15589bdff6ffSPyun YongHyeon ip = (struct ip *)(mtod(m, char *) + ip_off);
15599bdff6ffSPyun YongHyeon tcp = (struct tcphdr *)(mtod(m, char *) + poff);
156016199571SPyun YongHyeon ip->ip_sum = 0;
156116199571SPyun YongHyeon tcp->th_sum = in_pseudo(ip->ip_src.s_addr,
156216199571SPyun YongHyeon ip->ip_dst.s_addr, htons(IPPROTO_TCP));
156316199571SPyun YongHyeon }
156416199571SPyun YongHyeon *m_head = m;
156516199571SPyun YongHyeon }
156616199571SPyun YongHyeon
156716199571SPyun YongHyeon si = prod = sc->age_cdata.age_tx_prod;
156816199571SPyun YongHyeon txd = &sc->age_cdata.age_txdesc[prod];
156916199571SPyun YongHyeon txd_last = txd;
157016199571SPyun YongHyeon map = txd->tx_dmamap;
157116199571SPyun YongHyeon
157216199571SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->age_cdata.age_tx_tag, map,
157316199571SPyun YongHyeon *m_head, txsegs, &nsegs, 0);
157416199571SPyun YongHyeon if (error == EFBIG) {
1575c6499eccSGleb Smirnoff m = m_collapse(*m_head, M_NOWAIT, AGE_MAXTXSEGS);
157616199571SPyun YongHyeon if (m == NULL) {
157716199571SPyun YongHyeon m_freem(*m_head);
157816199571SPyun YongHyeon *m_head = NULL;
157916199571SPyun YongHyeon return (ENOMEM);
158016199571SPyun YongHyeon }
158116199571SPyun YongHyeon *m_head = m;
158216199571SPyun YongHyeon error = bus_dmamap_load_mbuf_sg(sc->age_cdata.age_tx_tag, map,
158316199571SPyun YongHyeon *m_head, txsegs, &nsegs, 0);
158416199571SPyun YongHyeon if (error != 0) {
158516199571SPyun YongHyeon m_freem(*m_head);
158616199571SPyun YongHyeon *m_head = NULL;
158716199571SPyun YongHyeon return (error);
158816199571SPyun YongHyeon }
158916199571SPyun YongHyeon } else if (error != 0)
159016199571SPyun YongHyeon return (error);
159116199571SPyun YongHyeon if (nsegs == 0) {
159216199571SPyun YongHyeon m_freem(*m_head);
159316199571SPyun YongHyeon *m_head = NULL;
159416199571SPyun YongHyeon return (EIO);
159516199571SPyun YongHyeon }
159616199571SPyun YongHyeon
159716199571SPyun YongHyeon /* Check descriptor overrun. */
159816199571SPyun YongHyeon if (sc->age_cdata.age_tx_cnt + nsegs >= AGE_TX_RING_CNT - 2) {
159916199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_tx_tag, map);
160016199571SPyun YongHyeon return (ENOBUFS);
160116199571SPyun YongHyeon }
160216199571SPyun YongHyeon
160316199571SPyun YongHyeon m = *m_head;
16049bdff6ffSPyun YongHyeon /* Configure VLAN hardware tag insertion. */
16059bdff6ffSPyun YongHyeon if ((m->m_flags & M_VLANTAG) != 0) {
16069bdff6ffSPyun YongHyeon vtag = AGE_TX_VLAN_TAG(m->m_pkthdr.ether_vtag);
16079bdff6ffSPyun YongHyeon vtag = ((vtag << AGE_TD_VLAN_SHIFT) & AGE_TD_VLAN_MASK);
16089bdff6ffSPyun YongHyeon cflags |= AGE_TD_INSERT_VLAN_TAG;
16099bdff6ffSPyun YongHyeon }
16109bdff6ffSPyun YongHyeon
16119bdff6ffSPyun YongHyeon desc = NULL;
16129bdff6ffSPyun YongHyeon i = 0;
161316199571SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
161416199571SPyun YongHyeon /* Request TSO and set MSS. */
161516199571SPyun YongHyeon cflags |= AGE_TD_TSO_IPV4;
161616199571SPyun YongHyeon cflags |= AGE_TD_IPCSUM | AGE_TD_TCPCSUM;
161716199571SPyun YongHyeon cflags |= ((uint32_t)m->m_pkthdr.tso_segsz <<
161816199571SPyun YongHyeon AGE_TD_TSO_MSS_SHIFT);
161916199571SPyun YongHyeon /* Set IP/TCP header size. */
162016199571SPyun YongHyeon cflags |= ip->ip_hl << AGE_TD_IPHDR_LEN_SHIFT;
162116199571SPyun YongHyeon cflags |= tcp->th_off << AGE_TD_TSO_TCPHDR_LEN_SHIFT;
16229bdff6ffSPyun YongHyeon /*
16239bdff6ffSPyun YongHyeon * L1 requires the first buffer should only hold IP/TCP
16249bdff6ffSPyun YongHyeon * header data. TCP payload should be handled in other
16259bdff6ffSPyun YongHyeon * descriptors.
16269bdff6ffSPyun YongHyeon */
16279bdff6ffSPyun YongHyeon hdrlen = poff + (tcp->th_off << 2);
16289bdff6ffSPyun YongHyeon desc = &sc->age_rdata.age_tx_ring[prod];
16299bdff6ffSPyun YongHyeon desc->addr = htole64(txsegs[0].ds_addr);
16309bdff6ffSPyun YongHyeon desc->len = htole32(AGE_TX_BYTES(hdrlen) | vtag);
16319bdff6ffSPyun YongHyeon desc->flags = htole32(cflags);
16329bdff6ffSPyun YongHyeon sc->age_cdata.age_tx_cnt++;
16339bdff6ffSPyun YongHyeon AGE_DESC_INC(prod, AGE_TX_RING_CNT);
16349bdff6ffSPyun YongHyeon if (m->m_len - hdrlen > 0) {
16359bdff6ffSPyun YongHyeon /* Handle remaining payload of the 1st fragment. */
16369bdff6ffSPyun YongHyeon desc = &sc->age_rdata.age_tx_ring[prod];
16379bdff6ffSPyun YongHyeon desc->addr = htole64(txsegs[0].ds_addr + hdrlen);
16389bdff6ffSPyun YongHyeon desc->len = htole32(AGE_TX_BYTES(m->m_len - hdrlen) |
16399bdff6ffSPyun YongHyeon vtag);
16409bdff6ffSPyun YongHyeon desc->flags = htole32(cflags);
16419bdff6ffSPyun YongHyeon sc->age_cdata.age_tx_cnt++;
16429bdff6ffSPyun YongHyeon AGE_DESC_INC(prod, AGE_TX_RING_CNT);
16439bdff6ffSPyun YongHyeon }
16449bdff6ffSPyun YongHyeon /* Handle remaining fragments. */
16459bdff6ffSPyun YongHyeon i = 1;
16466da6d0a9SPyun YongHyeon } else if ((m->m_pkthdr.csum_flags & AGE_CSUM_FEATURES) != 0) {
16476da6d0a9SPyun YongHyeon /* Configure Tx IP/TCP/UDP checksum offload. */
16486da6d0a9SPyun YongHyeon cflags |= AGE_TD_CSUM;
16496da6d0a9SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
16506da6d0a9SPyun YongHyeon cflags |= AGE_TD_TCPCSUM;
16516da6d0a9SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
16526da6d0a9SPyun YongHyeon cflags |= AGE_TD_UDPCSUM;
16536da6d0a9SPyun YongHyeon /* Set checksum start offset. */
16546da6d0a9SPyun YongHyeon cflags |= (poff << AGE_TD_CSUM_PLOADOFFSET_SHIFT);
16556da6d0a9SPyun YongHyeon /* Set checksum insertion position of TCP/UDP. */
16566da6d0a9SPyun YongHyeon cflags |= ((poff + m->m_pkthdr.csum_data) <<
16576da6d0a9SPyun YongHyeon AGE_TD_CSUM_XSUMOFFSET_SHIFT);
165816199571SPyun YongHyeon }
16599bdff6ffSPyun YongHyeon for (; i < nsegs; i++) {
166016199571SPyun YongHyeon desc = &sc->age_rdata.age_tx_ring[prod];
166116199571SPyun YongHyeon desc->addr = htole64(txsegs[i].ds_addr);
166216199571SPyun YongHyeon desc->len = htole32(AGE_TX_BYTES(txsegs[i].ds_len) | vtag);
166316199571SPyun YongHyeon desc->flags = htole32(cflags);
166416199571SPyun YongHyeon sc->age_cdata.age_tx_cnt++;
166516199571SPyun YongHyeon AGE_DESC_INC(prod, AGE_TX_RING_CNT);
166616199571SPyun YongHyeon }
166716199571SPyun YongHyeon /* Update producer index. */
166816199571SPyun YongHyeon sc->age_cdata.age_tx_prod = prod;
166916199571SPyun YongHyeon
167016199571SPyun YongHyeon /* Set EOP on the last descriptor. */
167116199571SPyun YongHyeon prod = (prod + AGE_TX_RING_CNT - 1) % AGE_TX_RING_CNT;
167216199571SPyun YongHyeon desc = &sc->age_rdata.age_tx_ring[prod];
167316199571SPyun YongHyeon desc->flags |= htole32(AGE_TD_EOP);
167416199571SPyun YongHyeon
167516199571SPyun YongHyeon /* Lastly set TSO header and modify IP/TCP header for TSO operation. */
167616199571SPyun YongHyeon if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
167716199571SPyun YongHyeon desc = &sc->age_rdata.age_tx_ring[si];
167816199571SPyun YongHyeon desc->flags |= htole32(AGE_TD_TSO_HDR);
167916199571SPyun YongHyeon }
168016199571SPyun YongHyeon
168116199571SPyun YongHyeon /* Swap dmamap of the first and the last. */
168216199571SPyun YongHyeon txd = &sc->age_cdata.age_txdesc[prod];
168316199571SPyun YongHyeon map = txd_last->tx_dmamap;
168416199571SPyun YongHyeon txd_last->tx_dmamap = txd->tx_dmamap;
168516199571SPyun YongHyeon txd->tx_dmamap = map;
168616199571SPyun YongHyeon txd->tx_m = m;
168716199571SPyun YongHyeon
168816199571SPyun YongHyeon /* Sync descriptors. */
168916199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_tx_tag, map, BUS_DMASYNC_PREWRITE);
169016199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_tx_ring_tag,
169116199571SPyun YongHyeon sc->age_cdata.age_tx_ring_map,
169216199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
169316199571SPyun YongHyeon
169416199571SPyun YongHyeon return (0);
169516199571SPyun YongHyeon }
169616199571SPyun YongHyeon
169716199571SPyun YongHyeon static void
age_start(if_t ifp)169852436412SJustin Hibbits age_start(if_t ifp)
169916199571SPyun YongHyeon {
170032341ad6SJohn Baldwin struct age_softc *sc;
170116199571SPyun YongHyeon
170252436412SJustin Hibbits sc = if_getsoftc(ifp);
170332341ad6SJohn Baldwin AGE_LOCK(sc);
170432341ad6SJohn Baldwin age_start_locked(ifp);
170532341ad6SJohn Baldwin AGE_UNLOCK(sc);
170616199571SPyun YongHyeon }
170716199571SPyun YongHyeon
170816199571SPyun YongHyeon static void
age_start_locked(if_t ifp)170952436412SJustin Hibbits age_start_locked(if_t ifp)
171016199571SPyun YongHyeon {
171116199571SPyun YongHyeon struct age_softc *sc;
171216199571SPyun YongHyeon struct mbuf *m_head;
171316199571SPyun YongHyeon int enq;
171416199571SPyun YongHyeon
171552436412SJustin Hibbits sc = if_getsoftc(ifp);
171616199571SPyun YongHyeon
171732341ad6SJohn Baldwin AGE_LOCK_ASSERT(sc);
171816199571SPyun YongHyeon
171952436412SJustin Hibbits if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
172032341ad6SJohn Baldwin IFF_DRV_RUNNING || (sc->age_flags & AGE_FLAG_LINK) == 0)
172116199571SPyun YongHyeon return;
172216199571SPyun YongHyeon
172352436412SJustin Hibbits for (enq = 0; !if_sendq_empty(ifp); ) {
172452436412SJustin Hibbits m_head = if_dequeue(ifp);
172516199571SPyun YongHyeon if (m_head == NULL)
172616199571SPyun YongHyeon break;
172716199571SPyun YongHyeon /*
172816199571SPyun YongHyeon * Pack the data into the transmit ring. If we
172916199571SPyun YongHyeon * don't have room, set the OACTIVE flag and wait
173016199571SPyun YongHyeon * for the NIC to drain the ring.
173116199571SPyun YongHyeon */
173216199571SPyun YongHyeon if (age_encap(sc, &m_head)) {
173316199571SPyun YongHyeon if (m_head == NULL)
173416199571SPyun YongHyeon break;
173552436412SJustin Hibbits if_sendq_prepend(ifp, m_head);
173652436412SJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
173716199571SPyun YongHyeon break;
173816199571SPyun YongHyeon }
173916199571SPyun YongHyeon
174016199571SPyun YongHyeon enq++;
174116199571SPyun YongHyeon /*
174216199571SPyun YongHyeon * If there's a BPF listener, bounce a copy of this frame
174316199571SPyun YongHyeon * to him.
174416199571SPyun YongHyeon */
174516199571SPyun YongHyeon ETHER_BPF_MTAP(ifp, m_head);
174616199571SPyun YongHyeon }
174716199571SPyun YongHyeon
174816199571SPyun YongHyeon if (enq > 0) {
174916199571SPyun YongHyeon /* Update mbox. */
175016199571SPyun YongHyeon AGE_COMMIT_MBOX(sc);
175116199571SPyun YongHyeon /* Set a timeout in case the chip goes out to lunch. */
175216199571SPyun YongHyeon sc->age_watchdog_timer = AGE_TX_TIMEOUT;
175316199571SPyun YongHyeon }
175416199571SPyun YongHyeon }
175516199571SPyun YongHyeon
175616199571SPyun YongHyeon static void
age_watchdog(struct age_softc * sc)175716199571SPyun YongHyeon age_watchdog(struct age_softc *sc)
175816199571SPyun YongHyeon {
175952436412SJustin Hibbits if_t ifp;
176016199571SPyun YongHyeon
176116199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
176216199571SPyun YongHyeon
176316199571SPyun YongHyeon if (sc->age_watchdog_timer == 0 || --sc->age_watchdog_timer)
176416199571SPyun YongHyeon return;
176516199571SPyun YongHyeon
176616199571SPyun YongHyeon ifp = sc->age_ifp;
176716199571SPyun YongHyeon if ((sc->age_flags & AGE_FLAG_LINK) == 0) {
176816199571SPyun YongHyeon if_printf(sc->age_ifp, "watchdog timeout (missed link)\n");
17691209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
177052436412SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
177116199571SPyun YongHyeon age_init_locked(sc);
177216199571SPyun YongHyeon return;
177316199571SPyun YongHyeon }
177416199571SPyun YongHyeon if (sc->age_cdata.age_tx_cnt == 0) {
177516199571SPyun YongHyeon if_printf(sc->age_ifp,
177616199571SPyun YongHyeon "watchdog timeout (missed Tx interrupts) -- recovering\n");
177752436412SJustin Hibbits if (!if_sendq_empty(ifp))
177832341ad6SJohn Baldwin age_start_locked(ifp);
177916199571SPyun YongHyeon return;
178016199571SPyun YongHyeon }
178116199571SPyun YongHyeon if_printf(sc->age_ifp, "watchdog timeout\n");
17821209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
178352436412SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
178416199571SPyun YongHyeon age_init_locked(sc);
178552436412SJustin Hibbits if (!if_sendq_empty(ifp))
178632341ad6SJohn Baldwin age_start_locked(ifp);
178716199571SPyun YongHyeon }
178816199571SPyun YongHyeon
178916199571SPyun YongHyeon static int
age_ioctl(if_t ifp,u_long cmd,caddr_t data)179052436412SJustin Hibbits age_ioctl(if_t ifp, u_long cmd, caddr_t data)
179116199571SPyun YongHyeon {
179216199571SPyun YongHyeon struct age_softc *sc;
179316199571SPyun YongHyeon struct ifreq *ifr;
179416199571SPyun YongHyeon struct mii_data *mii;
179516199571SPyun YongHyeon uint32_t reg;
179616199571SPyun YongHyeon int error, mask;
179716199571SPyun YongHyeon
179852436412SJustin Hibbits sc = if_getsoftc(ifp);
179916199571SPyun YongHyeon ifr = (struct ifreq *)data;
180016199571SPyun YongHyeon error = 0;
180116199571SPyun YongHyeon switch (cmd) {
180216199571SPyun YongHyeon case SIOCSIFMTU:
180316199571SPyun YongHyeon if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > AGE_JUMBO_MTU)
180416199571SPyun YongHyeon error = EINVAL;
180552436412SJustin Hibbits else if (if_getmtu(ifp) != ifr->ifr_mtu) {
180616199571SPyun YongHyeon AGE_LOCK(sc);
180752436412SJustin Hibbits if_setmtu(ifp, ifr->ifr_mtu);
180852436412SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
180952436412SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
181016199571SPyun YongHyeon age_init_locked(sc);
18113ca447daSPyun YongHyeon }
181216199571SPyun YongHyeon AGE_UNLOCK(sc);
181316199571SPyun YongHyeon }
181416199571SPyun YongHyeon break;
181516199571SPyun YongHyeon case SIOCSIFFLAGS:
181616199571SPyun YongHyeon AGE_LOCK(sc);
181752436412SJustin Hibbits if ((if_getflags(ifp) & IFF_UP) != 0) {
181852436412SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
181952436412SJustin Hibbits if (((if_getflags(ifp) ^ sc->age_if_flags)
182016199571SPyun YongHyeon & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
182116199571SPyun YongHyeon age_rxfilter(sc);
182216199571SPyun YongHyeon } else {
182316199571SPyun YongHyeon if ((sc->age_flags & AGE_FLAG_DETACH) == 0)
182416199571SPyun YongHyeon age_init_locked(sc);
182516199571SPyun YongHyeon }
182616199571SPyun YongHyeon } else {
182752436412SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
182816199571SPyun YongHyeon age_stop(sc);
182916199571SPyun YongHyeon }
183052436412SJustin Hibbits sc->age_if_flags = if_getflags(ifp);
183116199571SPyun YongHyeon AGE_UNLOCK(sc);
183216199571SPyun YongHyeon break;
183316199571SPyun YongHyeon case SIOCADDMULTI:
183416199571SPyun YongHyeon case SIOCDELMULTI:
183516199571SPyun YongHyeon AGE_LOCK(sc);
183652436412SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
183716199571SPyun YongHyeon age_rxfilter(sc);
183816199571SPyun YongHyeon AGE_UNLOCK(sc);
183916199571SPyun YongHyeon break;
184016199571SPyun YongHyeon case SIOCSIFMEDIA:
184116199571SPyun YongHyeon case SIOCGIFMEDIA:
184216199571SPyun YongHyeon mii = device_get_softc(sc->age_miibus);
184316199571SPyun YongHyeon error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
184416199571SPyun YongHyeon break;
184516199571SPyun YongHyeon case SIOCSIFCAP:
184616199571SPyun YongHyeon AGE_LOCK(sc);
184752436412SJustin Hibbits mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
184816199571SPyun YongHyeon if ((mask & IFCAP_TXCSUM) != 0 &&
184952436412SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
185052436412SJustin Hibbits if_togglecapenable(ifp, IFCAP_TXCSUM);
185152436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
185252436412SJustin Hibbits if_sethwassistbits(ifp, AGE_CSUM_FEATURES, 0);
185316199571SPyun YongHyeon else
185452436412SJustin Hibbits if_sethwassistbits(ifp, 0, AGE_CSUM_FEATURES);
185516199571SPyun YongHyeon }
185616199571SPyun YongHyeon if ((mask & IFCAP_RXCSUM) != 0 &&
185752436412SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0) {
185852436412SJustin Hibbits if_togglecapenable(ifp, IFCAP_RXCSUM);
185916199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MAC_CFG);
186016199571SPyun YongHyeon reg &= ~MAC_CFG_RXCSUM_ENB;
186152436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
186216199571SPyun YongHyeon reg |= MAC_CFG_RXCSUM_ENB;
186316199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
186416199571SPyun YongHyeon }
186516199571SPyun YongHyeon if ((mask & IFCAP_TSO4) != 0 &&
186652436412SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
186752436412SJustin Hibbits if_togglecapenable(ifp, IFCAP_TSO4);
186852436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_TSO4) != 0)
186952436412SJustin Hibbits if_sethwassistbits(ifp, CSUM_TSO, 0);
187016199571SPyun YongHyeon else
187152436412SJustin Hibbits if_sethwassistbits(ifp, 0, CSUM_TSO);
187216199571SPyun YongHyeon }
187316199571SPyun YongHyeon
187416199571SPyun YongHyeon if ((mask & IFCAP_WOL_MCAST) != 0 &&
187552436412SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_WOL_MCAST) != 0)
187652436412SJustin Hibbits if_togglecapenable(ifp, IFCAP_WOL_MCAST);
187716199571SPyun YongHyeon if ((mask & IFCAP_WOL_MAGIC) != 0 &&
187852436412SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_WOL_MAGIC) != 0)
187952436412SJustin Hibbits if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
188016199571SPyun YongHyeon if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
188152436412SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_VLAN_HWCSUM) != 0)
188252436412SJustin Hibbits if_togglecapenable(ifp, IFCAP_VLAN_HWCSUM);
188316199571SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
188452436412SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
188552436412SJustin Hibbits if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
18860fe060a8SPyun YongHyeon if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
188752436412SJustin Hibbits (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
188852436412SJustin Hibbits if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
188952436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
189052436412SJustin Hibbits if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
18910fe060a8SPyun YongHyeon age_rxvlan(sc);
18920fe060a8SPyun YongHyeon }
189316199571SPyun YongHyeon AGE_UNLOCK(sc);
189416199571SPyun YongHyeon VLAN_CAPABILITIES(ifp);
189516199571SPyun YongHyeon break;
189616199571SPyun YongHyeon default:
189716199571SPyun YongHyeon error = ether_ioctl(ifp, cmd, data);
189816199571SPyun YongHyeon break;
189916199571SPyun YongHyeon }
190016199571SPyun YongHyeon
190116199571SPyun YongHyeon return (error);
190216199571SPyun YongHyeon }
190316199571SPyun YongHyeon
190416199571SPyun YongHyeon static void
age_mac_config(struct age_softc * sc)190516199571SPyun YongHyeon age_mac_config(struct age_softc *sc)
190616199571SPyun YongHyeon {
190716199571SPyun YongHyeon struct mii_data *mii;
190816199571SPyun YongHyeon uint32_t reg;
190916199571SPyun YongHyeon
191016199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
191116199571SPyun YongHyeon
191216199571SPyun YongHyeon mii = device_get_softc(sc->age_miibus);
191316199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MAC_CFG);
191416199571SPyun YongHyeon reg &= ~MAC_CFG_FULL_DUPLEX;
191516199571SPyun YongHyeon reg &= ~(MAC_CFG_TX_FC | MAC_CFG_RX_FC);
191616199571SPyun YongHyeon reg &= ~MAC_CFG_SPEED_MASK;
191716199571SPyun YongHyeon /* Reprogram MAC with resolved speed/duplex. */
191816199571SPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) {
191916199571SPyun YongHyeon case IFM_10_T:
192016199571SPyun YongHyeon case IFM_100_TX:
192116199571SPyun YongHyeon reg |= MAC_CFG_SPEED_10_100;
192216199571SPyun YongHyeon break;
192316199571SPyun YongHyeon case IFM_1000_T:
192416199571SPyun YongHyeon reg |= MAC_CFG_SPEED_1000;
192516199571SPyun YongHyeon break;
192616199571SPyun YongHyeon }
192716199571SPyun YongHyeon if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
192816199571SPyun YongHyeon reg |= MAC_CFG_FULL_DUPLEX;
192916199571SPyun YongHyeon #ifdef notyet
193016199571SPyun YongHyeon if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
193116199571SPyun YongHyeon reg |= MAC_CFG_TX_FC;
193216199571SPyun YongHyeon if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
193316199571SPyun YongHyeon reg |= MAC_CFG_RX_FC;
193416199571SPyun YongHyeon #endif
193516199571SPyun YongHyeon }
193616199571SPyun YongHyeon
193716199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
193816199571SPyun YongHyeon }
193916199571SPyun YongHyeon
194016199571SPyun YongHyeon static void
age_link_task(void * arg,int pending)194116199571SPyun YongHyeon age_link_task(void *arg, int pending)
194216199571SPyun YongHyeon {
194316199571SPyun YongHyeon struct age_softc *sc;
194416199571SPyun YongHyeon struct mii_data *mii;
194552436412SJustin Hibbits if_t ifp;
194616199571SPyun YongHyeon uint32_t reg;
194716199571SPyun YongHyeon
194816199571SPyun YongHyeon sc = (struct age_softc *)arg;
194916199571SPyun YongHyeon
195016199571SPyun YongHyeon AGE_LOCK(sc);
195116199571SPyun YongHyeon mii = device_get_softc(sc->age_miibus);
195216199571SPyun YongHyeon ifp = sc->age_ifp;
195316199571SPyun YongHyeon if (mii == NULL || ifp == NULL ||
195452436412SJustin Hibbits (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
195516199571SPyun YongHyeon AGE_UNLOCK(sc);
195616199571SPyun YongHyeon return;
195716199571SPyun YongHyeon }
195816199571SPyun YongHyeon
195916199571SPyun YongHyeon sc->age_flags &= ~AGE_FLAG_LINK;
196016199571SPyun YongHyeon if ((mii->mii_media_status & IFM_AVALID) != 0) {
196116199571SPyun YongHyeon switch (IFM_SUBTYPE(mii->mii_media_active)) {
196216199571SPyun YongHyeon case IFM_10_T:
196316199571SPyun YongHyeon case IFM_100_TX:
196416199571SPyun YongHyeon case IFM_1000_T:
196516199571SPyun YongHyeon sc->age_flags |= AGE_FLAG_LINK;
196616199571SPyun YongHyeon break;
196716199571SPyun YongHyeon default:
196816199571SPyun YongHyeon break;
196916199571SPyun YongHyeon }
197016199571SPyun YongHyeon }
197116199571SPyun YongHyeon
197216199571SPyun YongHyeon /* Stop Rx/Tx MACs. */
197316199571SPyun YongHyeon age_stop_rxmac(sc);
197416199571SPyun YongHyeon age_stop_txmac(sc);
197516199571SPyun YongHyeon
197616199571SPyun YongHyeon /* Program MACs with resolved speed/duplex/flow-control. */
197716199571SPyun YongHyeon if ((sc->age_flags & AGE_FLAG_LINK) != 0) {
197816199571SPyun YongHyeon age_mac_config(sc);
197916199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MAC_CFG);
198016199571SPyun YongHyeon /* Restart DMA engine and Tx/Rx MAC. */
198116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DMA_CFG, CSR_READ_4(sc, AGE_DMA_CFG) |
198216199571SPyun YongHyeon DMA_CFG_RD_ENB | DMA_CFG_WR_ENB);
198316199571SPyun YongHyeon reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
198416199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
198516199571SPyun YongHyeon }
198616199571SPyun YongHyeon
198716199571SPyun YongHyeon AGE_UNLOCK(sc);
198816199571SPyun YongHyeon }
198916199571SPyun YongHyeon
199016199571SPyun YongHyeon static void
age_stats_update(struct age_softc * sc)199116199571SPyun YongHyeon age_stats_update(struct age_softc *sc)
199216199571SPyun YongHyeon {
199316199571SPyun YongHyeon struct age_stats *stat;
199416199571SPyun YongHyeon struct smb *smb;
199552436412SJustin Hibbits if_t ifp;
199616199571SPyun YongHyeon
199716199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
199816199571SPyun YongHyeon
199916199571SPyun YongHyeon stat = &sc->age_stat;
200016199571SPyun YongHyeon
200116199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_smb_block_tag,
200216199571SPyun YongHyeon sc->age_cdata.age_smb_block_map,
200316199571SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
200416199571SPyun YongHyeon
200516199571SPyun YongHyeon smb = sc->age_rdata.age_smb_block;
200616199571SPyun YongHyeon if (smb->updated == 0)
200716199571SPyun YongHyeon return;
200816199571SPyun YongHyeon
200916199571SPyun YongHyeon ifp = sc->age_ifp;
201016199571SPyun YongHyeon /* Rx stats. */
201116199571SPyun YongHyeon stat->rx_frames += smb->rx_frames;
201216199571SPyun YongHyeon stat->rx_bcast_frames += smb->rx_bcast_frames;
201316199571SPyun YongHyeon stat->rx_mcast_frames += smb->rx_mcast_frames;
201416199571SPyun YongHyeon stat->rx_pause_frames += smb->rx_pause_frames;
201516199571SPyun YongHyeon stat->rx_control_frames += smb->rx_control_frames;
201616199571SPyun YongHyeon stat->rx_crcerrs += smb->rx_crcerrs;
201716199571SPyun YongHyeon stat->rx_lenerrs += smb->rx_lenerrs;
201816199571SPyun YongHyeon stat->rx_bytes += smb->rx_bytes;
201916199571SPyun YongHyeon stat->rx_runts += smb->rx_runts;
202016199571SPyun YongHyeon stat->rx_fragments += smb->rx_fragments;
202116199571SPyun YongHyeon stat->rx_pkts_64 += smb->rx_pkts_64;
202216199571SPyun YongHyeon stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
202316199571SPyun YongHyeon stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
202416199571SPyun YongHyeon stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
202516199571SPyun YongHyeon stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
202616199571SPyun YongHyeon stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
202716199571SPyun YongHyeon stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
202816199571SPyun YongHyeon stat->rx_pkts_truncated += smb->rx_pkts_truncated;
202916199571SPyun YongHyeon stat->rx_fifo_oflows += smb->rx_fifo_oflows;
203016199571SPyun YongHyeon stat->rx_desc_oflows += smb->rx_desc_oflows;
203116199571SPyun YongHyeon stat->rx_alignerrs += smb->rx_alignerrs;
203216199571SPyun YongHyeon stat->rx_bcast_bytes += smb->rx_bcast_bytes;
203316199571SPyun YongHyeon stat->rx_mcast_bytes += smb->rx_mcast_bytes;
203416199571SPyun YongHyeon stat->rx_pkts_filtered += smb->rx_pkts_filtered;
203516199571SPyun YongHyeon
203616199571SPyun YongHyeon /* Tx stats. */
203716199571SPyun YongHyeon stat->tx_frames += smb->tx_frames;
203816199571SPyun YongHyeon stat->tx_bcast_frames += smb->tx_bcast_frames;
203916199571SPyun YongHyeon stat->tx_mcast_frames += smb->tx_mcast_frames;
204016199571SPyun YongHyeon stat->tx_pause_frames += smb->tx_pause_frames;
204116199571SPyun YongHyeon stat->tx_excess_defer += smb->tx_excess_defer;
204216199571SPyun YongHyeon stat->tx_control_frames += smb->tx_control_frames;
204316199571SPyun YongHyeon stat->tx_deferred += smb->tx_deferred;
204416199571SPyun YongHyeon stat->tx_bytes += smb->tx_bytes;
204516199571SPyun YongHyeon stat->tx_pkts_64 += smb->tx_pkts_64;
204616199571SPyun YongHyeon stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
204716199571SPyun YongHyeon stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
204816199571SPyun YongHyeon stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
204916199571SPyun YongHyeon stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
205016199571SPyun YongHyeon stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
205116199571SPyun YongHyeon stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
205216199571SPyun YongHyeon stat->tx_single_colls += smb->tx_single_colls;
205316199571SPyun YongHyeon stat->tx_multi_colls += smb->tx_multi_colls;
205416199571SPyun YongHyeon stat->tx_late_colls += smb->tx_late_colls;
205516199571SPyun YongHyeon stat->tx_excess_colls += smb->tx_excess_colls;
205616199571SPyun YongHyeon stat->tx_underrun += smb->tx_underrun;
205716199571SPyun YongHyeon stat->tx_desc_underrun += smb->tx_desc_underrun;
205816199571SPyun YongHyeon stat->tx_lenerrs += smb->tx_lenerrs;
205916199571SPyun YongHyeon stat->tx_pkts_truncated += smb->tx_pkts_truncated;
206016199571SPyun YongHyeon stat->tx_bcast_bytes += smb->tx_bcast_bytes;
206116199571SPyun YongHyeon stat->tx_mcast_bytes += smb->tx_mcast_bytes;
206216199571SPyun YongHyeon
206316199571SPyun YongHyeon /* Update counters in ifnet. */
20641209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OPACKETS, smb->tx_frames);
206516199571SPyun YongHyeon
20661209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_COLLISIONS, smb->tx_single_colls +
206716199571SPyun YongHyeon smb->tx_multi_colls + smb->tx_late_colls +
20681209989cSGleb Smirnoff smb->tx_excess_colls * HDPX_CFG_RETRY_DEFAULT);
206916199571SPyun YongHyeon
20701209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_OERRORS, smb->tx_excess_colls +
207116199571SPyun YongHyeon smb->tx_late_colls + smb->tx_underrun +
20721209989cSGleb Smirnoff smb->tx_pkts_truncated);
207316199571SPyun YongHyeon
20741209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IPACKETS, smb->rx_frames);
207516199571SPyun YongHyeon
20761209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IERRORS, smb->rx_crcerrs +
20771209989cSGleb Smirnoff smb->rx_lenerrs + smb->rx_runts + smb->rx_pkts_truncated +
207816199571SPyun YongHyeon smb->rx_fifo_oflows + smb->rx_desc_oflows +
20791209989cSGleb Smirnoff smb->rx_alignerrs);
208016199571SPyun YongHyeon
208116199571SPyun YongHyeon /* Update done, clear. */
208216199571SPyun YongHyeon smb->updated = 0;
208316199571SPyun YongHyeon
208416199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_smb_block_tag,
208516199571SPyun YongHyeon sc->age_cdata.age_smb_block_map,
208616199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
208716199571SPyun YongHyeon }
208816199571SPyun YongHyeon
208916199571SPyun YongHyeon static int
age_intr(void * arg)209016199571SPyun YongHyeon age_intr(void *arg)
209116199571SPyun YongHyeon {
209216199571SPyun YongHyeon struct age_softc *sc;
209316199571SPyun YongHyeon uint32_t status;
209416199571SPyun YongHyeon
209516199571SPyun YongHyeon sc = (struct age_softc *)arg;
209616199571SPyun YongHyeon
209716199571SPyun YongHyeon status = CSR_READ_4(sc, AGE_INTR_STATUS);
209816199571SPyun YongHyeon if (status == 0 || (status & AGE_INTRS) == 0)
209916199571SPyun YongHyeon return (FILTER_STRAY);
210016199571SPyun YongHyeon /* Disable interrupts. */
210116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_INTR_STATUS, status | INTR_DIS_INT);
210216199571SPyun YongHyeon taskqueue_enqueue(sc->age_tq, &sc->age_int_task);
210316199571SPyun YongHyeon
210416199571SPyun YongHyeon return (FILTER_HANDLED);
210516199571SPyun YongHyeon }
210616199571SPyun YongHyeon
210716199571SPyun YongHyeon static void
age_int_task(void * arg,int pending)210816199571SPyun YongHyeon age_int_task(void *arg, int pending)
210916199571SPyun YongHyeon {
211016199571SPyun YongHyeon struct age_softc *sc;
211152436412SJustin Hibbits if_t ifp;
211216199571SPyun YongHyeon struct cmb *cmb;
211316199571SPyun YongHyeon uint32_t status;
211416199571SPyun YongHyeon
211516199571SPyun YongHyeon sc = (struct age_softc *)arg;
211616199571SPyun YongHyeon
211716199571SPyun YongHyeon AGE_LOCK(sc);
211816199571SPyun YongHyeon
211916199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_cmb_block_tag,
212016199571SPyun YongHyeon sc->age_cdata.age_cmb_block_map,
212116199571SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
212216199571SPyun YongHyeon cmb = sc->age_rdata.age_cmb_block;
212316199571SPyun YongHyeon status = le32toh(cmb->intr_status);
212416199571SPyun YongHyeon if (sc->age_morework != 0)
212516199571SPyun YongHyeon status |= INTR_CMB_RX;
212616199571SPyun YongHyeon if ((status & AGE_INTRS) == 0)
212716199571SPyun YongHyeon goto done;
212816199571SPyun YongHyeon
212916199571SPyun YongHyeon sc->age_tpd_cons = (le32toh(cmb->tpd_cons) & TPD_CONS_MASK) >>
213016199571SPyun YongHyeon TPD_CONS_SHIFT;
213116199571SPyun YongHyeon sc->age_rr_prod = (le32toh(cmb->rprod_cons) & RRD_PROD_MASK) >>
213216199571SPyun YongHyeon RRD_PROD_SHIFT;
213316199571SPyun YongHyeon /* Let hardware know CMB was served. */
213416199571SPyun YongHyeon cmb->intr_status = 0;
213516199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_cmb_block_tag,
213616199571SPyun YongHyeon sc->age_cdata.age_cmb_block_map,
213716199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
213816199571SPyun YongHyeon
213916199571SPyun YongHyeon ifp = sc->age_ifp;
214052436412SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
214116199571SPyun YongHyeon if ((status & INTR_CMB_RX) != 0)
214216199571SPyun YongHyeon sc->age_morework = age_rxintr(sc, sc->age_rr_prod,
214316199571SPyun YongHyeon sc->age_process_limit);
214416199571SPyun YongHyeon if ((status & INTR_CMB_TX) != 0)
214516199571SPyun YongHyeon age_txintr(sc, sc->age_tpd_cons);
214616199571SPyun YongHyeon if ((status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST)) != 0) {
214716199571SPyun YongHyeon if ((status & INTR_DMA_RD_TO_RST) != 0)
214816199571SPyun YongHyeon device_printf(sc->age_dev,
214916199571SPyun YongHyeon "DMA read error! -- resetting\n");
215016199571SPyun YongHyeon if ((status & INTR_DMA_WR_TO_RST) != 0)
215116199571SPyun YongHyeon device_printf(sc->age_dev,
215216199571SPyun YongHyeon "DMA write error! -- resetting\n");
215352436412SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
215416199571SPyun YongHyeon age_init_locked(sc);
215516199571SPyun YongHyeon }
215652436412SJustin Hibbits if (!if_sendq_empty(ifp))
215732341ad6SJohn Baldwin age_start_locked(ifp);
215816199571SPyun YongHyeon if ((status & INTR_SMB) != 0)
215916199571SPyun YongHyeon age_stats_update(sc);
216016199571SPyun YongHyeon }
216116199571SPyun YongHyeon
216216199571SPyun YongHyeon /* Check whether CMB was updated while serving Tx/Rx/SMB handler. */
216316199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_cmb_block_tag,
216416199571SPyun YongHyeon sc->age_cdata.age_cmb_block_map,
216516199571SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
216616199571SPyun YongHyeon status = le32toh(cmb->intr_status);
216716199571SPyun YongHyeon if (sc->age_morework != 0 || (status & AGE_INTRS) != 0) {
216816199571SPyun YongHyeon taskqueue_enqueue(sc->age_tq, &sc->age_int_task);
216916199571SPyun YongHyeon AGE_UNLOCK(sc);
217016199571SPyun YongHyeon return;
217116199571SPyun YongHyeon }
217216199571SPyun YongHyeon
217316199571SPyun YongHyeon done:
217416199571SPyun YongHyeon /* Re-enable interrupts. */
217516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_INTR_STATUS, 0);
217616199571SPyun YongHyeon AGE_UNLOCK(sc);
217716199571SPyun YongHyeon }
217816199571SPyun YongHyeon
217916199571SPyun YongHyeon static void
age_txintr(struct age_softc * sc,int tpd_cons)218016199571SPyun YongHyeon age_txintr(struct age_softc *sc, int tpd_cons)
218116199571SPyun YongHyeon {
218252436412SJustin Hibbits if_t ifp;
218316199571SPyun YongHyeon struct age_txdesc *txd;
218416199571SPyun YongHyeon int cons, prog;
218516199571SPyun YongHyeon
218616199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
218716199571SPyun YongHyeon
218816199571SPyun YongHyeon ifp = sc->age_ifp;
218916199571SPyun YongHyeon
219016199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_tx_ring_tag,
219116199571SPyun YongHyeon sc->age_cdata.age_tx_ring_map,
219216199571SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
219316199571SPyun YongHyeon
219416199571SPyun YongHyeon /*
219516199571SPyun YongHyeon * Go through our Tx list and free mbufs for those
219616199571SPyun YongHyeon * frames which have been transmitted.
219716199571SPyun YongHyeon */
219816199571SPyun YongHyeon cons = sc->age_cdata.age_tx_cons;
219916199571SPyun YongHyeon for (prog = 0; cons != tpd_cons; AGE_DESC_INC(cons, AGE_TX_RING_CNT)) {
220016199571SPyun YongHyeon if (sc->age_cdata.age_tx_cnt <= 0)
220116199571SPyun YongHyeon break;
220216199571SPyun YongHyeon prog++;
220352436412SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
220416199571SPyun YongHyeon sc->age_cdata.age_tx_cnt--;
220516199571SPyun YongHyeon txd = &sc->age_cdata.age_txdesc[cons];
220616199571SPyun YongHyeon /*
220716199571SPyun YongHyeon * Clear Tx descriptors, it's not required but would
220816199571SPyun YongHyeon * help debugging in case of Tx issues.
220916199571SPyun YongHyeon */
221016199571SPyun YongHyeon txd->tx_desc->addr = 0;
221116199571SPyun YongHyeon txd->tx_desc->len = 0;
221216199571SPyun YongHyeon txd->tx_desc->flags = 0;
221316199571SPyun YongHyeon
221416199571SPyun YongHyeon if (txd->tx_m == NULL)
221516199571SPyun YongHyeon continue;
221616199571SPyun YongHyeon /* Reclaim transmitted mbufs. */
221716199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_tx_tag, txd->tx_dmamap,
221816199571SPyun YongHyeon BUS_DMASYNC_POSTWRITE);
221916199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_tx_tag, txd->tx_dmamap);
222016199571SPyun YongHyeon m_freem(txd->tx_m);
222116199571SPyun YongHyeon txd->tx_m = NULL;
222216199571SPyun YongHyeon }
222316199571SPyun YongHyeon
222416199571SPyun YongHyeon if (prog > 0) {
222516199571SPyun YongHyeon sc->age_cdata.age_tx_cons = cons;
222616199571SPyun YongHyeon
222716199571SPyun YongHyeon /*
222816199571SPyun YongHyeon * Unarm watchdog timer only when there are no pending
222916199571SPyun YongHyeon * Tx descriptors in queue.
223016199571SPyun YongHyeon */
223116199571SPyun YongHyeon if (sc->age_cdata.age_tx_cnt == 0)
223216199571SPyun YongHyeon sc->age_watchdog_timer = 0;
223316199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_tx_ring_tag,
223416199571SPyun YongHyeon sc->age_cdata.age_tx_ring_map,
223516199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
223616199571SPyun YongHyeon }
223716199571SPyun YongHyeon }
223816199571SPyun YongHyeon
2239088dd4b7SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2240088dd4b7SPyun YongHyeon static struct mbuf *
age_fixup_rx(if_t ifp,struct mbuf * m)224152436412SJustin Hibbits age_fixup_rx(if_t ifp, struct mbuf *m)
2242088dd4b7SPyun YongHyeon {
2243088dd4b7SPyun YongHyeon struct mbuf *n;
2244088dd4b7SPyun YongHyeon int i;
2245088dd4b7SPyun YongHyeon uint16_t *src, *dst;
2246088dd4b7SPyun YongHyeon
2247088dd4b7SPyun YongHyeon src = mtod(m, uint16_t *);
2248088dd4b7SPyun YongHyeon dst = src - 3;
2249088dd4b7SPyun YongHyeon
2250088dd4b7SPyun YongHyeon if (m->m_next == NULL) {
2251088dd4b7SPyun YongHyeon for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
2252088dd4b7SPyun YongHyeon *dst++ = *src++;
2253088dd4b7SPyun YongHyeon m->m_data -= 6;
2254088dd4b7SPyun YongHyeon return (m);
2255088dd4b7SPyun YongHyeon }
2256088dd4b7SPyun YongHyeon /*
2257088dd4b7SPyun YongHyeon * Append a new mbuf to received mbuf chain and copy ethernet
2258088dd4b7SPyun YongHyeon * header from the mbuf chain. This can save lots of CPU
2259088dd4b7SPyun YongHyeon * cycles for jumbo frame.
2260088dd4b7SPyun YongHyeon */
2261088dd4b7SPyun YongHyeon MGETHDR(n, M_NOWAIT, MT_DATA);
2262088dd4b7SPyun YongHyeon if (n == NULL) {
22631209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2264088dd4b7SPyun YongHyeon m_freem(m);
2265088dd4b7SPyun YongHyeon return (NULL);
2266088dd4b7SPyun YongHyeon }
2267088dd4b7SPyun YongHyeon bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
2268088dd4b7SPyun YongHyeon m->m_data += ETHER_HDR_LEN;
2269088dd4b7SPyun YongHyeon m->m_len -= ETHER_HDR_LEN;
2270088dd4b7SPyun YongHyeon n->m_len = ETHER_HDR_LEN;
2271088dd4b7SPyun YongHyeon M_MOVE_PKTHDR(n, m);
2272088dd4b7SPyun YongHyeon n->m_next = m;
2273088dd4b7SPyun YongHyeon return (n);
2274088dd4b7SPyun YongHyeon }
2275088dd4b7SPyun YongHyeon #endif
2276088dd4b7SPyun YongHyeon
227716199571SPyun YongHyeon /* Receive a frame. */
227816199571SPyun YongHyeon static void
age_rxeof(struct age_softc * sc,struct rx_rdesc * rxrd)227916199571SPyun YongHyeon age_rxeof(struct age_softc *sc, struct rx_rdesc *rxrd)
228016199571SPyun YongHyeon {
228116199571SPyun YongHyeon struct age_rxdesc *rxd;
228252436412SJustin Hibbits if_t ifp;
228316199571SPyun YongHyeon struct mbuf *mp, *m;
228416199571SPyun YongHyeon uint32_t status, index, vtag;
2285088dd4b7SPyun YongHyeon int count, nsegs;
228616199571SPyun YongHyeon int rx_cons;
228716199571SPyun YongHyeon
228816199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
228916199571SPyun YongHyeon
229016199571SPyun YongHyeon ifp = sc->age_ifp;
229116199571SPyun YongHyeon status = le32toh(rxrd->flags);
229216199571SPyun YongHyeon index = le32toh(rxrd->index);
229316199571SPyun YongHyeon rx_cons = AGE_RX_CONS(index);
229416199571SPyun YongHyeon nsegs = AGE_RX_NSEGS(index);
229516199571SPyun YongHyeon
229616199571SPyun YongHyeon sc->age_cdata.age_rxlen = AGE_RX_BYTES(le32toh(rxrd->len));
2297088dd4b7SPyun YongHyeon if ((status & (AGE_RRD_ERROR | AGE_RRD_LENGTH_NOK)) != 0) {
229816199571SPyun YongHyeon /*
229916199571SPyun YongHyeon * We want to pass the following frames to upper
230016199571SPyun YongHyeon * layer regardless of error status of Rx return
230116199571SPyun YongHyeon * ring.
230216199571SPyun YongHyeon *
230316199571SPyun YongHyeon * o IP/TCP/UDP checksum is bad.
230416199571SPyun YongHyeon * o frame length and protocol specific length
230516199571SPyun YongHyeon * does not match.
230616199571SPyun YongHyeon */
2307088dd4b7SPyun YongHyeon status |= AGE_RRD_IPCSUM_NOK | AGE_RRD_TCP_UDPCSUM_NOK;
2308088dd4b7SPyun YongHyeon if ((status & (AGE_RRD_CRC | AGE_RRD_CODE | AGE_RRD_DRIBBLE |
2309088dd4b7SPyun YongHyeon AGE_RRD_RUNT | AGE_RRD_OFLOW | AGE_RRD_TRUNC)) != 0)
231016199571SPyun YongHyeon return;
231116199571SPyun YongHyeon }
231216199571SPyun YongHyeon
231316199571SPyun YongHyeon for (count = 0; count < nsegs; count++,
231416199571SPyun YongHyeon AGE_DESC_INC(rx_cons, AGE_RX_RING_CNT)) {
231516199571SPyun YongHyeon rxd = &sc->age_cdata.age_rxdesc[rx_cons];
231616199571SPyun YongHyeon mp = rxd->rx_m;
231716199571SPyun YongHyeon /* Add a new receive buffer to the ring. */
231816199571SPyun YongHyeon if (age_newbuf(sc, rxd) != 0) {
23191209989cSGleb Smirnoff if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
232016199571SPyun YongHyeon /* Reuse Rx buffers. */
2321088dd4b7SPyun YongHyeon if (sc->age_cdata.age_rxhead != NULL)
232216199571SPyun YongHyeon m_freem(sc->age_cdata.age_rxhead);
232316199571SPyun YongHyeon break;
232416199571SPyun YongHyeon }
232516199571SPyun YongHyeon
2326088dd4b7SPyun YongHyeon /*
2327088dd4b7SPyun YongHyeon * Assume we've received a full sized frame.
2328088dd4b7SPyun YongHyeon * Actual size is fixed when we encounter the end of
2329088dd4b7SPyun YongHyeon * multi-segmented frame.
2330088dd4b7SPyun YongHyeon */
2331088dd4b7SPyun YongHyeon mp->m_len = AGE_RX_BUF_SIZE;
233216199571SPyun YongHyeon
233316199571SPyun YongHyeon /* Chain received mbufs. */
233416199571SPyun YongHyeon if (sc->age_cdata.age_rxhead == NULL) {
233516199571SPyun YongHyeon sc->age_cdata.age_rxhead = mp;
233616199571SPyun YongHyeon sc->age_cdata.age_rxtail = mp;
233716199571SPyun YongHyeon } else {
233816199571SPyun YongHyeon mp->m_flags &= ~M_PKTHDR;
233916199571SPyun YongHyeon sc->age_cdata.age_rxprev_tail =
234016199571SPyun YongHyeon sc->age_cdata.age_rxtail;
234116199571SPyun YongHyeon sc->age_cdata.age_rxtail->m_next = mp;
234216199571SPyun YongHyeon sc->age_cdata.age_rxtail = mp;
234316199571SPyun YongHyeon }
234416199571SPyun YongHyeon
234516199571SPyun YongHyeon if (count == nsegs - 1) {
2346088dd4b7SPyun YongHyeon /* Last desc. for this frame. */
2347088dd4b7SPyun YongHyeon m = sc->age_cdata.age_rxhead;
2348088dd4b7SPyun YongHyeon m->m_flags |= M_PKTHDR;
234916199571SPyun YongHyeon /*
235016199571SPyun YongHyeon * It seems that L1 controller has no way
235116199571SPyun YongHyeon * to tell hardware to strip CRC bytes.
235216199571SPyun YongHyeon */
2353088dd4b7SPyun YongHyeon m->m_pkthdr.len = sc->age_cdata.age_rxlen -
2354088dd4b7SPyun YongHyeon ETHER_CRC_LEN;
235516199571SPyun YongHyeon if (nsegs > 1) {
2356088dd4b7SPyun YongHyeon /* Set last mbuf size. */
2357088dd4b7SPyun YongHyeon mp->m_len = sc->age_cdata.age_rxlen -
2358088dd4b7SPyun YongHyeon ((nsegs - 1) * AGE_RX_BUF_SIZE);
235916199571SPyun YongHyeon /* Remove the CRC bytes in chained mbufs. */
236016199571SPyun YongHyeon if (mp->m_len <= ETHER_CRC_LEN) {
236116199571SPyun YongHyeon sc->age_cdata.age_rxtail =
236216199571SPyun YongHyeon sc->age_cdata.age_rxprev_tail;
236316199571SPyun YongHyeon sc->age_cdata.age_rxtail->m_len -=
236416199571SPyun YongHyeon (ETHER_CRC_LEN - mp->m_len);
236516199571SPyun YongHyeon sc->age_cdata.age_rxtail->m_next = NULL;
236616199571SPyun YongHyeon m_freem(mp);
236716199571SPyun YongHyeon } else {
236816199571SPyun YongHyeon mp->m_len -= ETHER_CRC_LEN;
236916199571SPyun YongHyeon }
2370088dd4b7SPyun YongHyeon } else
2371088dd4b7SPyun YongHyeon m->m_len = m->m_pkthdr.len;
237216199571SPyun YongHyeon m->m_pkthdr.rcvif = ifp;
237316199571SPyun YongHyeon /*
237416199571SPyun YongHyeon * Set checksum information.
237516199571SPyun YongHyeon * It seems that L1 controller can compute partial
237616199571SPyun YongHyeon * checksum. The partial checksum value can be used
237716199571SPyun YongHyeon * to accelerate checksum computation for fragmented
237816199571SPyun YongHyeon * TCP/UDP packets. Upper network stack already
237916199571SPyun YongHyeon * takes advantage of the partial checksum value in
238016199571SPyun YongHyeon * IP reassembly stage. But I'm not sure the
238116199571SPyun YongHyeon * correctness of the partial hardware checksum
238216199571SPyun YongHyeon * assistance due to lack of data sheet. If it is
238316199571SPyun YongHyeon * proven to work on L1 I'll enable it.
238416199571SPyun YongHyeon */
238552436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0 &&
238616199571SPyun YongHyeon (status & AGE_RRD_IPV4) != 0) {
238716199571SPyun YongHyeon if ((status & AGE_RRD_IPCSUM_NOK) == 0)
2388088dd4b7SPyun YongHyeon m->m_pkthdr.csum_flags |=
2389088dd4b7SPyun YongHyeon CSUM_IP_CHECKED | CSUM_IP_VALID;
239016199571SPyun YongHyeon if ((status & (AGE_RRD_TCP | AGE_RRD_UDP)) &&
239116199571SPyun YongHyeon (status & AGE_RRD_TCP_UDPCSUM_NOK) == 0) {
239216199571SPyun YongHyeon m->m_pkthdr.csum_flags |=
239316199571SPyun YongHyeon CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
239416199571SPyun YongHyeon m->m_pkthdr.csum_data = 0xffff;
239516199571SPyun YongHyeon }
239616199571SPyun YongHyeon /*
239716199571SPyun YongHyeon * Don't mark bad checksum for TCP/UDP frames
239816199571SPyun YongHyeon * as fragmented frames may always have set
239916199571SPyun YongHyeon * bad checksummed bit of descriptor status.
240016199571SPyun YongHyeon */
240116199571SPyun YongHyeon }
240216199571SPyun YongHyeon
240316199571SPyun YongHyeon /* Check for VLAN tagged frames. */
240452436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0 &&
240516199571SPyun YongHyeon (status & AGE_RRD_VLAN) != 0) {
240616199571SPyun YongHyeon vtag = AGE_RX_VLAN(le32toh(rxrd->vtags));
240716199571SPyun YongHyeon m->m_pkthdr.ether_vtag = AGE_RX_VLAN_TAG(vtag);
240816199571SPyun YongHyeon m->m_flags |= M_VLANTAG;
240916199571SPyun YongHyeon }
2410088dd4b7SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
2411088dd4b7SPyun YongHyeon m = age_fixup_rx(ifp, m);
2412088dd4b7SPyun YongHyeon if (m != NULL)
2413088dd4b7SPyun YongHyeon #endif
2414088dd4b7SPyun YongHyeon {
241516199571SPyun YongHyeon /* Pass it on. */
241616199571SPyun YongHyeon AGE_UNLOCK(sc);
241752436412SJustin Hibbits if_input(ifp, m);
241816199571SPyun YongHyeon AGE_LOCK(sc);
2419088dd4b7SPyun YongHyeon }
2420088dd4b7SPyun YongHyeon }
2421088dd4b7SPyun YongHyeon }
242216199571SPyun YongHyeon
242316199571SPyun YongHyeon /* Reset mbuf chains. */
242416199571SPyun YongHyeon AGE_RXCHAIN_RESET(sc);
242516199571SPyun YongHyeon }
242616199571SPyun YongHyeon
242716199571SPyun YongHyeon static int
age_rxintr(struct age_softc * sc,int rr_prod,int count)242816199571SPyun YongHyeon age_rxintr(struct age_softc *sc, int rr_prod, int count)
242916199571SPyun YongHyeon {
243016199571SPyun YongHyeon struct rx_rdesc *rxrd;
243116199571SPyun YongHyeon int rr_cons, nsegs, pktlen, prog;
243216199571SPyun YongHyeon
243316199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
243416199571SPyun YongHyeon
243516199571SPyun YongHyeon rr_cons = sc->age_cdata.age_rr_cons;
243616199571SPyun YongHyeon if (rr_cons == rr_prod)
243716199571SPyun YongHyeon return (0);
243816199571SPyun YongHyeon
243916199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rr_ring_tag,
244016199571SPyun YongHyeon sc->age_cdata.age_rr_ring_map,
244116199571SPyun YongHyeon BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2442595615e6SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rx_ring_tag,
2443595615e6SPyun YongHyeon sc->age_cdata.age_rx_ring_map, BUS_DMASYNC_POSTWRITE);
244416199571SPyun YongHyeon
244516199571SPyun YongHyeon for (prog = 0; rr_cons != rr_prod; prog++) {
24460203558fSMark Johnston if (count-- <= 0)
244716199571SPyun YongHyeon break;
244816199571SPyun YongHyeon rxrd = &sc->age_rdata.age_rr_ring[rr_cons];
244916199571SPyun YongHyeon nsegs = AGE_RX_NSEGS(le32toh(rxrd->index));
245016199571SPyun YongHyeon if (nsegs == 0)
245116199571SPyun YongHyeon break;
245216199571SPyun YongHyeon /*
245316199571SPyun YongHyeon * Check number of segments against received bytes.
245416199571SPyun YongHyeon * Non-matching value would indicate that hardware
245516199571SPyun YongHyeon * is still trying to update Rx return descriptors.
245616199571SPyun YongHyeon * I'm not sure whether this check is really needed.
245716199571SPyun YongHyeon */
245816199571SPyun YongHyeon pktlen = AGE_RX_BYTES(le32toh(rxrd->len));
2459057b4402SPedro F. Giffuni if (nsegs != howmany(pktlen, AGE_RX_BUF_SIZE))
246016199571SPyun YongHyeon break;
246116199571SPyun YongHyeon
246216199571SPyun YongHyeon /* Received a frame. */
246316199571SPyun YongHyeon age_rxeof(sc, rxrd);
246416199571SPyun YongHyeon /* Clear return ring. */
246516199571SPyun YongHyeon rxrd->index = 0;
246616199571SPyun YongHyeon AGE_DESC_INC(rr_cons, AGE_RR_RING_CNT);
2467088dd4b7SPyun YongHyeon sc->age_cdata.age_rx_cons += nsegs;
2468088dd4b7SPyun YongHyeon sc->age_cdata.age_rx_cons %= AGE_RX_RING_CNT;
246916199571SPyun YongHyeon }
247016199571SPyun YongHyeon
247116199571SPyun YongHyeon if (prog > 0) {
247216199571SPyun YongHyeon /* Update the consumer index. */
247316199571SPyun YongHyeon sc->age_cdata.age_rr_cons = rr_cons;
247416199571SPyun YongHyeon
2475595615e6SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rx_ring_tag,
2476595615e6SPyun YongHyeon sc->age_cdata.age_rx_ring_map, BUS_DMASYNC_PREWRITE);
247716199571SPyun YongHyeon /* Sync descriptors. */
247816199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rr_ring_tag,
247916199571SPyun YongHyeon sc->age_cdata.age_rr_ring_map,
248016199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
248116199571SPyun YongHyeon
248216199571SPyun YongHyeon /* Notify hardware availability of new Rx buffers. */
248316199571SPyun YongHyeon AGE_COMMIT_MBOX(sc);
248416199571SPyun YongHyeon }
248516199571SPyun YongHyeon
248616199571SPyun YongHyeon return (count > 0 ? 0 : EAGAIN);
248716199571SPyun YongHyeon }
248816199571SPyun YongHyeon
248916199571SPyun YongHyeon static void
age_tick(void * arg)249016199571SPyun YongHyeon age_tick(void *arg)
249116199571SPyun YongHyeon {
249216199571SPyun YongHyeon struct age_softc *sc;
249316199571SPyun YongHyeon struct mii_data *mii;
249416199571SPyun YongHyeon
249516199571SPyun YongHyeon sc = (struct age_softc *)arg;
249616199571SPyun YongHyeon
249716199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
249816199571SPyun YongHyeon
249916199571SPyun YongHyeon mii = device_get_softc(sc->age_miibus);
250016199571SPyun YongHyeon mii_tick(mii);
250116199571SPyun YongHyeon age_watchdog(sc);
250216199571SPyun YongHyeon callout_reset(&sc->age_tick_ch, hz, age_tick, sc);
250316199571SPyun YongHyeon }
250416199571SPyun YongHyeon
250516199571SPyun YongHyeon static void
age_reset(struct age_softc * sc)250616199571SPyun YongHyeon age_reset(struct age_softc *sc)
250716199571SPyun YongHyeon {
250816199571SPyun YongHyeon uint32_t reg;
250916199571SPyun YongHyeon int i;
251016199571SPyun YongHyeon
251116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MASTER_CFG, MASTER_RESET);
251206ca18c1SPyun YongHyeon CSR_READ_4(sc, AGE_MASTER_CFG);
251306ca18c1SPyun YongHyeon DELAY(1000);
251416199571SPyun YongHyeon for (i = AGE_RESET_TIMEOUT; i > 0; i--) {
251516199571SPyun YongHyeon if ((reg = CSR_READ_4(sc, AGE_IDLE_STATUS)) == 0)
251616199571SPyun YongHyeon break;
251716199571SPyun YongHyeon DELAY(10);
251816199571SPyun YongHyeon }
251916199571SPyun YongHyeon
252016199571SPyun YongHyeon if (i == 0)
252116199571SPyun YongHyeon device_printf(sc->age_dev, "reset timeout(0x%08x)!\n", reg);
252216199571SPyun YongHyeon /* Initialize PCIe module. From Linux. */
252316199571SPyun YongHyeon CSR_WRITE_4(sc, 0x12FC, 0x6500);
252416199571SPyun YongHyeon CSR_WRITE_4(sc, 0x1008, CSR_READ_4(sc, 0x1008) | 0x8000);
252516199571SPyun YongHyeon }
252616199571SPyun YongHyeon
252716199571SPyun YongHyeon static void
age_init(void * xsc)252816199571SPyun YongHyeon age_init(void *xsc)
252916199571SPyun YongHyeon {
253016199571SPyun YongHyeon struct age_softc *sc;
253116199571SPyun YongHyeon
253216199571SPyun YongHyeon sc = (struct age_softc *)xsc;
253316199571SPyun YongHyeon AGE_LOCK(sc);
253416199571SPyun YongHyeon age_init_locked(sc);
253516199571SPyun YongHyeon AGE_UNLOCK(sc);
253616199571SPyun YongHyeon }
253716199571SPyun YongHyeon
253816199571SPyun YongHyeon static void
age_init_locked(struct age_softc * sc)253916199571SPyun YongHyeon age_init_locked(struct age_softc *sc)
254016199571SPyun YongHyeon {
254152436412SJustin Hibbits if_t ifp;
254216199571SPyun YongHyeon struct mii_data *mii;
254316199571SPyun YongHyeon uint8_t eaddr[ETHER_ADDR_LEN];
254416199571SPyun YongHyeon bus_addr_t paddr;
254516199571SPyun YongHyeon uint32_t reg, fsize;
254616199571SPyun YongHyeon uint32_t rxf_hi, rxf_lo, rrd_hi, rrd_lo;
254716199571SPyun YongHyeon int error;
254816199571SPyun YongHyeon
254916199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
255016199571SPyun YongHyeon
255116199571SPyun YongHyeon ifp = sc->age_ifp;
255216199571SPyun YongHyeon mii = device_get_softc(sc->age_miibus);
255316199571SPyun YongHyeon
255452436412SJustin Hibbits if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0)
25553ca447daSPyun YongHyeon return;
25563ca447daSPyun YongHyeon
255716199571SPyun YongHyeon /*
255816199571SPyun YongHyeon * Cancel any pending I/O.
255916199571SPyun YongHyeon */
256016199571SPyun YongHyeon age_stop(sc);
256116199571SPyun YongHyeon
256216199571SPyun YongHyeon /*
256316199571SPyun YongHyeon * Reset the chip to a known state.
256416199571SPyun YongHyeon */
256516199571SPyun YongHyeon age_reset(sc);
256616199571SPyun YongHyeon
256716199571SPyun YongHyeon /* Initialize descriptors. */
256816199571SPyun YongHyeon error = age_init_rx_ring(sc);
256916199571SPyun YongHyeon if (error != 0) {
257016199571SPyun YongHyeon device_printf(sc->age_dev, "no memory for Rx buffers.\n");
257116199571SPyun YongHyeon age_stop(sc);
257216199571SPyun YongHyeon return;
257316199571SPyun YongHyeon }
257416199571SPyun YongHyeon age_init_rr_ring(sc);
257516199571SPyun YongHyeon age_init_tx_ring(sc);
257616199571SPyun YongHyeon age_init_cmb_block(sc);
257716199571SPyun YongHyeon age_init_smb_block(sc);
257816199571SPyun YongHyeon
257916199571SPyun YongHyeon /* Reprogram the station address. */
258052436412SJustin Hibbits bcopy(if_getlladdr(ifp), eaddr, ETHER_ADDR_LEN);
258116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_PAR0,
258216199571SPyun YongHyeon eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
258316199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_PAR1, eaddr[0] << 8 | eaddr[1]);
258416199571SPyun YongHyeon
258516199571SPyun YongHyeon /* Set descriptor base addresses. */
258616199571SPyun YongHyeon paddr = sc->age_rdata.age_tx_ring_paddr;
258716199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DESC_ADDR_HI, AGE_ADDR_HI(paddr));
258816199571SPyun YongHyeon paddr = sc->age_rdata.age_rx_ring_paddr;
258916199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DESC_RD_ADDR_LO, AGE_ADDR_LO(paddr));
259016199571SPyun YongHyeon paddr = sc->age_rdata.age_rr_ring_paddr;
259116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DESC_RRD_ADDR_LO, AGE_ADDR_LO(paddr));
259216199571SPyun YongHyeon paddr = sc->age_rdata.age_tx_ring_paddr;
259316199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DESC_TPD_ADDR_LO, AGE_ADDR_LO(paddr));
259416199571SPyun YongHyeon paddr = sc->age_rdata.age_cmb_block_paddr;
259516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DESC_CMB_ADDR_LO, AGE_ADDR_LO(paddr));
259616199571SPyun YongHyeon paddr = sc->age_rdata.age_smb_block_paddr;
259716199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DESC_SMB_ADDR_LO, AGE_ADDR_LO(paddr));
259816199571SPyun YongHyeon /* Set Rx/Rx return descriptor counter. */
259916199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DESC_RRD_RD_CNT,
260016199571SPyun YongHyeon ((AGE_RR_RING_CNT << DESC_RRD_CNT_SHIFT) &
260116199571SPyun YongHyeon DESC_RRD_CNT_MASK) |
260216199571SPyun YongHyeon ((AGE_RX_RING_CNT << DESC_RD_CNT_SHIFT) & DESC_RD_CNT_MASK));
260316199571SPyun YongHyeon /* Set Tx descriptor counter. */
260416199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DESC_TPD_CNT,
260516199571SPyun YongHyeon (AGE_TX_RING_CNT << DESC_TPD_CNT_SHIFT) & DESC_TPD_CNT_MASK);
260616199571SPyun YongHyeon
260716199571SPyun YongHyeon /* Tell hardware that we're ready to load descriptors. */
260816199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DMA_BLOCK, DMA_BLOCK_LOAD);
260916199571SPyun YongHyeon
261016199571SPyun YongHyeon /*
261116199571SPyun YongHyeon * Initialize mailbox register.
261216199571SPyun YongHyeon * Updated producer/consumer index information is exchanged
261316199571SPyun YongHyeon * through this mailbox register. However Tx producer and
261416199571SPyun YongHyeon * Rx return consumer/Rx producer are all shared such that
261516199571SPyun YongHyeon * it's hard to separate code path between Tx and Rx without
261616199571SPyun YongHyeon * locking. If L1 hardware have a separate mail box register
261716199571SPyun YongHyeon * for Tx and Rx consumer/producer management we could have
2618f207bdd2SGordon Bergling * independent Tx/Rx handler which in turn Rx handler could have
261916199571SPyun YongHyeon * been run without any locking.
262016199571SPyun YongHyeon */
262116199571SPyun YongHyeon AGE_COMMIT_MBOX(sc);
262216199571SPyun YongHyeon
262316199571SPyun YongHyeon /* Configure IPG/IFG parameters. */
262416199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_IPG_IFG_CFG,
262516199571SPyun YongHyeon ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) & IPG_IFG_IPG2_MASK) |
262616199571SPyun YongHyeon ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) & IPG_IFG_IPG1_MASK) |
262716199571SPyun YongHyeon ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) & IPG_IFG_MIFG_MASK) |
262816199571SPyun YongHyeon ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) & IPG_IFG_IPGT_MASK));
262916199571SPyun YongHyeon
263016199571SPyun YongHyeon /* Set parameters for half-duplex media. */
263116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_HDPX_CFG,
263216199571SPyun YongHyeon ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
263316199571SPyun YongHyeon HDPX_CFG_LCOL_MASK) |
263416199571SPyun YongHyeon ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
263516199571SPyun YongHyeon HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
263616199571SPyun YongHyeon ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
263716199571SPyun YongHyeon HDPX_CFG_ABEBT_MASK) |
263816199571SPyun YongHyeon ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
263916199571SPyun YongHyeon HDPX_CFG_JAMIPG_MASK));
264016199571SPyun YongHyeon
264116199571SPyun YongHyeon /* Configure interrupt moderation timer. */
264216199571SPyun YongHyeon CSR_WRITE_2(sc, AGE_IM_TIMER, AGE_USECS(sc->age_int_mod));
264316199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MASTER_CFG);
264416199571SPyun YongHyeon reg &= ~MASTER_MTIMER_ENB;
264516199571SPyun YongHyeon if (AGE_USECS(sc->age_int_mod) == 0)
264616199571SPyun YongHyeon reg &= ~MASTER_ITIMER_ENB;
264716199571SPyun YongHyeon else
264816199571SPyun YongHyeon reg |= MASTER_ITIMER_ENB;
264916199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MASTER_CFG, reg);
2650dca3a3a0SPyun YongHyeon if (bootverbose)
265116199571SPyun YongHyeon device_printf(sc->age_dev, "interrupt moderation is %d us.\n",
265216199571SPyun YongHyeon sc->age_int_mod);
265316199571SPyun YongHyeon CSR_WRITE_2(sc, AGE_INTR_CLR_TIMER, AGE_USECS(1000));
265416199571SPyun YongHyeon
265516199571SPyun YongHyeon /* Set Maximum frame size but don't let MTU be lass than ETHER_MTU. */
265652436412SJustin Hibbits if (if_getmtu(ifp) < ETHERMTU)
265716199571SPyun YongHyeon sc->age_max_frame_size = ETHERMTU;
265816199571SPyun YongHyeon else
265952436412SJustin Hibbits sc->age_max_frame_size = if_getmtu(ifp);
266016199571SPyun YongHyeon sc->age_max_frame_size += ETHER_HDR_LEN +
266116199571SPyun YongHyeon sizeof(struct ether_vlan_header) + ETHER_CRC_LEN;
266216199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_FRAME_SIZE, sc->age_max_frame_size);
266316199571SPyun YongHyeon /* Configure jumbo frame. */
266416199571SPyun YongHyeon fsize = roundup(sc->age_max_frame_size, sizeof(uint64_t));
266516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_RXQ_JUMBO_CFG,
266616199571SPyun YongHyeon (((fsize / sizeof(uint64_t)) <<
266716199571SPyun YongHyeon RXQ_JUMBO_CFG_SZ_THRESH_SHIFT) & RXQ_JUMBO_CFG_SZ_THRESH_MASK) |
266816199571SPyun YongHyeon ((RXQ_JUMBO_CFG_LKAH_DEFAULT <<
266916199571SPyun YongHyeon RXQ_JUMBO_CFG_LKAH_SHIFT) & RXQ_JUMBO_CFG_LKAH_MASK) |
267016199571SPyun YongHyeon ((AGE_USECS(8) << RXQ_JUMBO_CFG_RRD_TIMER_SHIFT) &
267116199571SPyun YongHyeon RXQ_JUMBO_CFG_RRD_TIMER_MASK));
267216199571SPyun YongHyeon
267316199571SPyun YongHyeon /* Configure flow-control parameters. From Linux. */
267416199571SPyun YongHyeon if ((sc->age_flags & AGE_FLAG_PCIE) != 0) {
267516199571SPyun YongHyeon /*
267616199571SPyun YongHyeon * Magic workaround for old-L1.
267716199571SPyun YongHyeon * Don't know which hw revision requires this magic.
267816199571SPyun YongHyeon */
267916199571SPyun YongHyeon CSR_WRITE_4(sc, 0x12FC, 0x6500);
268016199571SPyun YongHyeon /*
268116199571SPyun YongHyeon * Another magic workaround for flow-control mode
268216199571SPyun YongHyeon * change. From Linux.
268316199571SPyun YongHyeon */
268416199571SPyun YongHyeon CSR_WRITE_4(sc, 0x1008, CSR_READ_4(sc, 0x1008) | 0x8000);
268516199571SPyun YongHyeon }
268616199571SPyun YongHyeon /*
268716199571SPyun YongHyeon * TODO
268816199571SPyun YongHyeon * Should understand pause parameter relationships between FIFO
268916199571SPyun YongHyeon * size and number of Rx descriptors and Rx return descriptors.
269016199571SPyun YongHyeon *
269116199571SPyun YongHyeon * Magic parameters came from Linux.
269216199571SPyun YongHyeon */
269316199571SPyun YongHyeon switch (sc->age_chip_rev) {
269416199571SPyun YongHyeon case 0x8001:
269516199571SPyun YongHyeon case 0x9001:
269616199571SPyun YongHyeon case 0x9002:
269716199571SPyun YongHyeon case 0x9003:
269816199571SPyun YongHyeon rxf_hi = AGE_RX_RING_CNT / 16;
269916199571SPyun YongHyeon rxf_lo = (AGE_RX_RING_CNT * 7) / 8;
270016199571SPyun YongHyeon rrd_hi = (AGE_RR_RING_CNT * 7) / 8;
270116199571SPyun YongHyeon rrd_lo = AGE_RR_RING_CNT / 16;
270216199571SPyun YongHyeon break;
270316199571SPyun YongHyeon default:
270416199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_SRAM_RX_FIFO_LEN);
270516199571SPyun YongHyeon rxf_lo = reg / 16;
270616199571SPyun YongHyeon if (rxf_lo < 192)
270716199571SPyun YongHyeon rxf_lo = 192;
270816199571SPyun YongHyeon rxf_hi = (reg * 7) / 8;
270916199571SPyun YongHyeon if (rxf_hi < rxf_lo)
271016199571SPyun YongHyeon rxf_hi = rxf_lo + 16;
271116199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_SRAM_RRD_LEN);
271216199571SPyun YongHyeon rrd_lo = reg / 8;
271316199571SPyun YongHyeon rrd_hi = (reg * 7) / 8;
271416199571SPyun YongHyeon if (rrd_lo < 2)
271516199571SPyun YongHyeon rrd_lo = 2;
271616199571SPyun YongHyeon if (rrd_hi < rrd_lo)
271716199571SPyun YongHyeon rrd_hi = rrd_lo + 3;
271816199571SPyun YongHyeon break;
271916199571SPyun YongHyeon }
272016199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_RXQ_FIFO_PAUSE_THRESH,
272116199571SPyun YongHyeon ((rxf_lo << RXQ_FIFO_PAUSE_THRESH_LO_SHIFT) &
272216199571SPyun YongHyeon RXQ_FIFO_PAUSE_THRESH_LO_MASK) |
272316199571SPyun YongHyeon ((rxf_hi << RXQ_FIFO_PAUSE_THRESH_HI_SHIFT) &
272416199571SPyun YongHyeon RXQ_FIFO_PAUSE_THRESH_HI_MASK));
272516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_RXQ_RRD_PAUSE_THRESH,
272616199571SPyun YongHyeon ((rrd_lo << RXQ_RRD_PAUSE_THRESH_LO_SHIFT) &
272716199571SPyun YongHyeon RXQ_RRD_PAUSE_THRESH_LO_MASK) |
272816199571SPyun YongHyeon ((rrd_hi << RXQ_RRD_PAUSE_THRESH_HI_SHIFT) &
272916199571SPyun YongHyeon RXQ_RRD_PAUSE_THRESH_HI_MASK));
273016199571SPyun YongHyeon
273116199571SPyun YongHyeon /* Configure RxQ. */
273216199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_RXQ_CFG,
273316199571SPyun YongHyeon ((RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
273416199571SPyun YongHyeon RXQ_CFG_RD_BURST_MASK) |
273516199571SPyun YongHyeon ((RXQ_CFG_RRD_BURST_THRESH_DEFAULT <<
273616199571SPyun YongHyeon RXQ_CFG_RRD_BURST_THRESH_SHIFT) & RXQ_CFG_RRD_BURST_THRESH_MASK) |
273716199571SPyun YongHyeon ((RXQ_CFG_RD_PREF_MIN_IPG_DEFAULT <<
273816199571SPyun YongHyeon RXQ_CFG_RD_PREF_MIN_IPG_SHIFT) & RXQ_CFG_RD_PREF_MIN_IPG_MASK) |
273916199571SPyun YongHyeon RXQ_CFG_CUT_THROUGH_ENB | RXQ_CFG_ENB);
274016199571SPyun YongHyeon
274116199571SPyun YongHyeon /* Configure TxQ. */
274216199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_TXQ_CFG,
274316199571SPyun YongHyeon ((TXQ_CFG_TPD_BURST_DEFAULT << TXQ_CFG_TPD_BURST_SHIFT) &
274416199571SPyun YongHyeon TXQ_CFG_TPD_BURST_MASK) |
274516199571SPyun YongHyeon ((TXQ_CFG_TX_FIFO_BURST_DEFAULT << TXQ_CFG_TX_FIFO_BURST_SHIFT) &
274616199571SPyun YongHyeon TXQ_CFG_TX_FIFO_BURST_MASK) |
274716199571SPyun YongHyeon ((TXQ_CFG_TPD_FETCH_DEFAULT <<
274816199571SPyun YongHyeon TXQ_CFG_TPD_FETCH_THRESH_SHIFT) & TXQ_CFG_TPD_FETCH_THRESH_MASK) |
274916199571SPyun YongHyeon TXQ_CFG_ENB);
275016199571SPyun YongHyeon
275116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_TX_JUMBO_TPD_TH_IPG,
275216199571SPyun YongHyeon (((fsize / sizeof(uint64_t) << TX_JUMBO_TPD_TH_SHIFT)) &
275316199571SPyun YongHyeon TX_JUMBO_TPD_TH_MASK) |
275416199571SPyun YongHyeon ((TX_JUMBO_TPD_IPG_DEFAULT << TX_JUMBO_TPD_IPG_SHIFT) &
275516199571SPyun YongHyeon TX_JUMBO_TPD_IPG_MASK));
275616199571SPyun YongHyeon /* Configure DMA parameters. */
275716199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DMA_CFG,
275816199571SPyun YongHyeon DMA_CFG_ENH_ORDER | DMA_CFG_RCB_64 |
275916199571SPyun YongHyeon sc->age_dma_rd_burst | DMA_CFG_RD_ENB |
276016199571SPyun YongHyeon sc->age_dma_wr_burst | DMA_CFG_WR_ENB);
276116199571SPyun YongHyeon
276216199571SPyun YongHyeon /* Configure CMB DMA write threshold. */
276316199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_CMB_WR_THRESH,
276416199571SPyun YongHyeon ((CMB_WR_THRESH_RRD_DEFAULT << CMB_WR_THRESH_RRD_SHIFT) &
276516199571SPyun YongHyeon CMB_WR_THRESH_RRD_MASK) |
276616199571SPyun YongHyeon ((CMB_WR_THRESH_TPD_DEFAULT << CMB_WR_THRESH_TPD_SHIFT) &
276716199571SPyun YongHyeon CMB_WR_THRESH_TPD_MASK));
276816199571SPyun YongHyeon
276916199571SPyun YongHyeon /* Set CMB/SMB timer and enable them. */
277016199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_CMB_WR_TIMER,
277116199571SPyun YongHyeon ((AGE_USECS(2) << CMB_WR_TIMER_TX_SHIFT) & CMB_WR_TIMER_TX_MASK) |
277216199571SPyun YongHyeon ((AGE_USECS(2) << CMB_WR_TIMER_RX_SHIFT) & CMB_WR_TIMER_RX_MASK));
277316199571SPyun YongHyeon /* Request SMB updates for every seconds. */
277416199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_SMB_TIMER, AGE_USECS(1000 * 1000));
277516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_CSMB_CTRL, CSMB_CTRL_SMB_ENB | CSMB_CTRL_CMB_ENB);
277616199571SPyun YongHyeon
277716199571SPyun YongHyeon /*
277816199571SPyun YongHyeon * Disable all WOL bits as WOL can interfere normal Rx
277916199571SPyun YongHyeon * operation.
278016199571SPyun YongHyeon */
278116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_WOL_CFG, 0);
278216199571SPyun YongHyeon
278316199571SPyun YongHyeon /*
278416199571SPyun YongHyeon * Configure Tx/Rx MACs.
278516199571SPyun YongHyeon * - Auto-padding for short frames.
278616199571SPyun YongHyeon * - Enable CRC generation.
278716199571SPyun YongHyeon * Start with full-duplex/1000Mbps media. Actual reconfiguration
278816199571SPyun YongHyeon * of MAC is followed after link establishment.
278916199571SPyun YongHyeon */
279016199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG,
279116199571SPyun YongHyeon MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD |
279216199571SPyun YongHyeon MAC_CFG_FULL_DUPLEX | MAC_CFG_SPEED_1000 |
279316199571SPyun YongHyeon ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
279416199571SPyun YongHyeon MAC_CFG_PREAMBLE_MASK));
279516199571SPyun YongHyeon /* Set up the receive filter. */
279616199571SPyun YongHyeon age_rxfilter(sc);
279716199571SPyun YongHyeon age_rxvlan(sc);
279816199571SPyun YongHyeon
279916199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MAC_CFG);
280052436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
280116199571SPyun YongHyeon reg |= MAC_CFG_RXCSUM_ENB;
280216199571SPyun YongHyeon
280316199571SPyun YongHyeon /* Ack all pending interrupts and clear it. */
280416199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_INTR_STATUS, 0);
280516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_INTR_MASK, AGE_INTRS);
280616199571SPyun YongHyeon
280716199571SPyun YongHyeon /* Finally enable Tx/Rx MAC. */
280816199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, reg | MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
280916199571SPyun YongHyeon
281016199571SPyun YongHyeon sc->age_flags &= ~AGE_FLAG_LINK;
281116199571SPyun YongHyeon /* Switch to the current media. */
281216199571SPyun YongHyeon mii_mediachg(mii);
281316199571SPyun YongHyeon
281416199571SPyun YongHyeon callout_reset(&sc->age_tick_ch, hz, age_tick, sc);
281516199571SPyun YongHyeon
281652436412SJustin Hibbits if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
281752436412SJustin Hibbits if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
281816199571SPyun YongHyeon }
281916199571SPyun YongHyeon
282016199571SPyun YongHyeon static void
age_stop(struct age_softc * sc)282116199571SPyun YongHyeon age_stop(struct age_softc *sc)
282216199571SPyun YongHyeon {
282352436412SJustin Hibbits if_t ifp;
282416199571SPyun YongHyeon struct age_txdesc *txd;
282516199571SPyun YongHyeon struct age_rxdesc *rxd;
282616199571SPyun YongHyeon uint32_t reg;
282716199571SPyun YongHyeon int i;
282816199571SPyun YongHyeon
282916199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
283016199571SPyun YongHyeon /*
283116199571SPyun YongHyeon * Mark the interface down and cancel the watchdog timer.
283216199571SPyun YongHyeon */
283316199571SPyun YongHyeon ifp = sc->age_ifp;
283452436412SJustin Hibbits if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
283516199571SPyun YongHyeon sc->age_flags &= ~AGE_FLAG_LINK;
283616199571SPyun YongHyeon callout_stop(&sc->age_tick_ch);
283716199571SPyun YongHyeon sc->age_watchdog_timer = 0;
283816199571SPyun YongHyeon
283916199571SPyun YongHyeon /*
284016199571SPyun YongHyeon * Disable interrupts.
284116199571SPyun YongHyeon */
284216199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_INTR_MASK, 0);
284316199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_INTR_STATUS, 0xFFFFFFFF);
284416199571SPyun YongHyeon /* Stop CMB/SMB updates. */
284516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_CSMB_CTRL, 0);
284616199571SPyun YongHyeon /* Stop Rx/Tx MAC. */
284716199571SPyun YongHyeon age_stop_rxmac(sc);
284816199571SPyun YongHyeon age_stop_txmac(sc);
284916199571SPyun YongHyeon /* Stop DMA. */
285016199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DMA_CFG,
285116199571SPyun YongHyeon CSR_READ_4(sc, AGE_DMA_CFG) & ~(DMA_CFG_RD_ENB | DMA_CFG_WR_ENB));
285216199571SPyun YongHyeon /* Stop TxQ/RxQ. */
285316199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_TXQ_CFG,
285416199571SPyun YongHyeon CSR_READ_4(sc, AGE_TXQ_CFG) & ~TXQ_CFG_ENB);
285516199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_RXQ_CFG,
285616199571SPyun YongHyeon CSR_READ_4(sc, AGE_RXQ_CFG) & ~RXQ_CFG_ENB);
285716199571SPyun YongHyeon for (i = AGE_RESET_TIMEOUT; i > 0; i--) {
285816199571SPyun YongHyeon if ((reg = CSR_READ_4(sc, AGE_IDLE_STATUS)) == 0)
285916199571SPyun YongHyeon break;
286016199571SPyun YongHyeon DELAY(10);
286116199571SPyun YongHyeon }
286216199571SPyun YongHyeon if (i == 0)
286316199571SPyun YongHyeon device_printf(sc->age_dev,
286416199571SPyun YongHyeon "stopping Rx/Tx MACs timed out(0x%08x)!\n", reg);
286516199571SPyun YongHyeon
286616199571SPyun YongHyeon /* Reclaim Rx buffers that have been processed. */
286716199571SPyun YongHyeon if (sc->age_cdata.age_rxhead != NULL)
286816199571SPyun YongHyeon m_freem(sc->age_cdata.age_rxhead);
286916199571SPyun YongHyeon AGE_RXCHAIN_RESET(sc);
287016199571SPyun YongHyeon /*
287116199571SPyun YongHyeon * Free RX and TX mbufs still in the queues.
287216199571SPyun YongHyeon */
287316199571SPyun YongHyeon for (i = 0; i < AGE_RX_RING_CNT; i++) {
287416199571SPyun YongHyeon rxd = &sc->age_cdata.age_rxdesc[i];
287516199571SPyun YongHyeon if (rxd->rx_m != NULL) {
287616199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rx_tag,
287716199571SPyun YongHyeon rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
287816199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_rx_tag,
287916199571SPyun YongHyeon rxd->rx_dmamap);
288016199571SPyun YongHyeon m_freem(rxd->rx_m);
288116199571SPyun YongHyeon rxd->rx_m = NULL;
288216199571SPyun YongHyeon }
288316199571SPyun YongHyeon }
288416199571SPyun YongHyeon for (i = 0; i < AGE_TX_RING_CNT; i++) {
288516199571SPyun YongHyeon txd = &sc->age_cdata.age_txdesc[i];
288616199571SPyun YongHyeon if (txd->tx_m != NULL) {
288716199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_tx_tag,
288816199571SPyun YongHyeon txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
288916199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_tx_tag,
289016199571SPyun YongHyeon txd->tx_dmamap);
289116199571SPyun YongHyeon m_freem(txd->tx_m);
289216199571SPyun YongHyeon txd->tx_m = NULL;
289316199571SPyun YongHyeon }
289416199571SPyun YongHyeon }
289516199571SPyun YongHyeon }
289616199571SPyun YongHyeon
289716199571SPyun YongHyeon static void
age_stop_txmac(struct age_softc * sc)289816199571SPyun YongHyeon age_stop_txmac(struct age_softc *sc)
289916199571SPyun YongHyeon {
290016199571SPyun YongHyeon uint32_t reg;
290116199571SPyun YongHyeon int i;
290216199571SPyun YongHyeon
290316199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
290416199571SPyun YongHyeon
290516199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MAC_CFG);
290616199571SPyun YongHyeon if ((reg & MAC_CFG_TX_ENB) != 0) {
290716199571SPyun YongHyeon reg &= ~MAC_CFG_TX_ENB;
290816199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
290916199571SPyun YongHyeon }
291016199571SPyun YongHyeon /* Stop Tx DMA engine. */
291116199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_DMA_CFG);
291216199571SPyun YongHyeon if ((reg & DMA_CFG_RD_ENB) != 0) {
291316199571SPyun YongHyeon reg &= ~DMA_CFG_RD_ENB;
291416199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DMA_CFG, reg);
291516199571SPyun YongHyeon }
291616199571SPyun YongHyeon for (i = AGE_RESET_TIMEOUT; i > 0; i--) {
291716199571SPyun YongHyeon if ((CSR_READ_4(sc, AGE_IDLE_STATUS) &
291816199571SPyun YongHyeon (IDLE_STATUS_TXMAC | IDLE_STATUS_DMARD)) == 0)
291916199571SPyun YongHyeon break;
292016199571SPyun YongHyeon DELAY(10);
292116199571SPyun YongHyeon }
292216199571SPyun YongHyeon if (i == 0)
292316199571SPyun YongHyeon device_printf(sc->age_dev, "stopping TxMAC timeout!\n");
292416199571SPyun YongHyeon }
292516199571SPyun YongHyeon
292616199571SPyun YongHyeon static void
age_stop_rxmac(struct age_softc * sc)292716199571SPyun YongHyeon age_stop_rxmac(struct age_softc *sc)
292816199571SPyun YongHyeon {
292916199571SPyun YongHyeon uint32_t reg;
293016199571SPyun YongHyeon int i;
293116199571SPyun YongHyeon
293216199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
293316199571SPyun YongHyeon
293416199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MAC_CFG);
293516199571SPyun YongHyeon if ((reg & MAC_CFG_RX_ENB) != 0) {
293616199571SPyun YongHyeon reg &= ~MAC_CFG_RX_ENB;
293716199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
293816199571SPyun YongHyeon }
293916199571SPyun YongHyeon /* Stop Rx DMA engine. */
294016199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_DMA_CFG);
294116199571SPyun YongHyeon if ((reg & DMA_CFG_WR_ENB) != 0) {
294216199571SPyun YongHyeon reg &= ~DMA_CFG_WR_ENB;
294316199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_DMA_CFG, reg);
294416199571SPyun YongHyeon }
294516199571SPyun YongHyeon for (i = AGE_RESET_TIMEOUT; i > 0; i--) {
294616199571SPyun YongHyeon if ((CSR_READ_4(sc, AGE_IDLE_STATUS) &
294716199571SPyun YongHyeon (IDLE_STATUS_RXMAC | IDLE_STATUS_DMAWR)) == 0)
294816199571SPyun YongHyeon break;
294916199571SPyun YongHyeon DELAY(10);
295016199571SPyun YongHyeon }
295116199571SPyun YongHyeon if (i == 0)
295216199571SPyun YongHyeon device_printf(sc->age_dev, "stopping RxMAC timeout!\n");
295316199571SPyun YongHyeon }
295416199571SPyun YongHyeon
295516199571SPyun YongHyeon static void
age_init_tx_ring(struct age_softc * sc)295616199571SPyun YongHyeon age_init_tx_ring(struct age_softc *sc)
295716199571SPyun YongHyeon {
295816199571SPyun YongHyeon struct age_ring_data *rd;
295916199571SPyun YongHyeon struct age_txdesc *txd;
296016199571SPyun YongHyeon int i;
296116199571SPyun YongHyeon
296216199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
296316199571SPyun YongHyeon
296416199571SPyun YongHyeon sc->age_cdata.age_tx_prod = 0;
296516199571SPyun YongHyeon sc->age_cdata.age_tx_cons = 0;
296616199571SPyun YongHyeon sc->age_cdata.age_tx_cnt = 0;
296716199571SPyun YongHyeon
296816199571SPyun YongHyeon rd = &sc->age_rdata;
296916199571SPyun YongHyeon bzero(rd->age_tx_ring, AGE_TX_RING_SZ);
297016199571SPyun YongHyeon for (i = 0; i < AGE_TX_RING_CNT; i++) {
297116199571SPyun YongHyeon txd = &sc->age_cdata.age_txdesc[i];
297216199571SPyun YongHyeon txd->tx_desc = &rd->age_tx_ring[i];
297316199571SPyun YongHyeon txd->tx_m = NULL;
297416199571SPyun YongHyeon }
297516199571SPyun YongHyeon
297616199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_tx_ring_tag,
297716199571SPyun YongHyeon sc->age_cdata.age_tx_ring_map,
297816199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
297916199571SPyun YongHyeon }
298016199571SPyun YongHyeon
298116199571SPyun YongHyeon static int
age_init_rx_ring(struct age_softc * sc)298216199571SPyun YongHyeon age_init_rx_ring(struct age_softc *sc)
298316199571SPyun YongHyeon {
298416199571SPyun YongHyeon struct age_ring_data *rd;
298516199571SPyun YongHyeon struct age_rxdesc *rxd;
298616199571SPyun YongHyeon int i;
298716199571SPyun YongHyeon
298816199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
298916199571SPyun YongHyeon
299016199571SPyun YongHyeon sc->age_cdata.age_rx_cons = AGE_RX_RING_CNT - 1;
299116199571SPyun YongHyeon sc->age_morework = 0;
299216199571SPyun YongHyeon rd = &sc->age_rdata;
299316199571SPyun YongHyeon bzero(rd->age_rx_ring, AGE_RX_RING_SZ);
299416199571SPyun YongHyeon for (i = 0; i < AGE_RX_RING_CNT; i++) {
299516199571SPyun YongHyeon rxd = &sc->age_cdata.age_rxdesc[i];
299616199571SPyun YongHyeon rxd->rx_m = NULL;
299716199571SPyun YongHyeon rxd->rx_desc = &rd->age_rx_ring[i];
299816199571SPyun YongHyeon if (age_newbuf(sc, rxd) != 0)
299916199571SPyun YongHyeon return (ENOBUFS);
300016199571SPyun YongHyeon }
300116199571SPyun YongHyeon
300216199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rx_ring_tag,
3003595615e6SPyun YongHyeon sc->age_cdata.age_rx_ring_map, BUS_DMASYNC_PREWRITE);
300416199571SPyun YongHyeon
300516199571SPyun YongHyeon return (0);
300616199571SPyun YongHyeon }
300716199571SPyun YongHyeon
300816199571SPyun YongHyeon static void
age_init_rr_ring(struct age_softc * sc)300916199571SPyun YongHyeon age_init_rr_ring(struct age_softc *sc)
301016199571SPyun YongHyeon {
301116199571SPyun YongHyeon struct age_ring_data *rd;
301216199571SPyun YongHyeon
301316199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
301416199571SPyun YongHyeon
301516199571SPyun YongHyeon sc->age_cdata.age_rr_cons = 0;
301616199571SPyun YongHyeon AGE_RXCHAIN_RESET(sc);
301716199571SPyun YongHyeon
301816199571SPyun YongHyeon rd = &sc->age_rdata;
301916199571SPyun YongHyeon bzero(rd->age_rr_ring, AGE_RR_RING_SZ);
302016199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rr_ring_tag,
302116199571SPyun YongHyeon sc->age_cdata.age_rr_ring_map,
302216199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
302316199571SPyun YongHyeon }
302416199571SPyun YongHyeon
302516199571SPyun YongHyeon static void
age_init_cmb_block(struct age_softc * sc)302616199571SPyun YongHyeon age_init_cmb_block(struct age_softc *sc)
302716199571SPyun YongHyeon {
302816199571SPyun YongHyeon struct age_ring_data *rd;
302916199571SPyun YongHyeon
303016199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
303116199571SPyun YongHyeon
303216199571SPyun YongHyeon rd = &sc->age_rdata;
303316199571SPyun YongHyeon bzero(rd->age_cmb_block, AGE_CMB_BLOCK_SZ);
303416199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_cmb_block_tag,
303516199571SPyun YongHyeon sc->age_cdata.age_cmb_block_map,
303616199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
303716199571SPyun YongHyeon }
303816199571SPyun YongHyeon
303916199571SPyun YongHyeon static void
age_init_smb_block(struct age_softc * sc)304016199571SPyun YongHyeon age_init_smb_block(struct age_softc *sc)
304116199571SPyun YongHyeon {
304216199571SPyun YongHyeon struct age_ring_data *rd;
304316199571SPyun YongHyeon
304416199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
304516199571SPyun YongHyeon
304616199571SPyun YongHyeon rd = &sc->age_rdata;
304716199571SPyun YongHyeon bzero(rd->age_smb_block, AGE_SMB_BLOCK_SZ);
304816199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_smb_block_tag,
304916199571SPyun YongHyeon sc->age_cdata.age_smb_block_map,
305016199571SPyun YongHyeon BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
305116199571SPyun YongHyeon }
305216199571SPyun YongHyeon
305316199571SPyun YongHyeon static int
age_newbuf(struct age_softc * sc,struct age_rxdesc * rxd)305416199571SPyun YongHyeon age_newbuf(struct age_softc *sc, struct age_rxdesc *rxd)
305516199571SPyun YongHyeon {
305616199571SPyun YongHyeon struct rx_desc *desc;
305716199571SPyun YongHyeon struct mbuf *m;
305816199571SPyun YongHyeon bus_dma_segment_t segs[1];
305916199571SPyun YongHyeon bus_dmamap_t map;
306016199571SPyun YongHyeon int nsegs;
306116199571SPyun YongHyeon
306216199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
306316199571SPyun YongHyeon
3064c6499eccSGleb Smirnoff m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
306516199571SPyun YongHyeon if (m == NULL)
306616199571SPyun YongHyeon return (ENOBUFS);
306716199571SPyun YongHyeon m->m_len = m->m_pkthdr.len = MCLBYTES;
3068088dd4b7SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
3069088dd4b7SPyun YongHyeon m_adj(m, AGE_RX_BUF_ALIGN);
3070088dd4b7SPyun YongHyeon #endif
307116199571SPyun YongHyeon
307216199571SPyun YongHyeon if (bus_dmamap_load_mbuf_sg(sc->age_cdata.age_rx_tag,
307316199571SPyun YongHyeon sc->age_cdata.age_rx_sparemap, m, segs, &nsegs, 0) != 0) {
307416199571SPyun YongHyeon m_freem(m);
307516199571SPyun YongHyeon return (ENOBUFS);
307616199571SPyun YongHyeon }
307716199571SPyun YongHyeon KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
307816199571SPyun YongHyeon
307916199571SPyun YongHyeon if (rxd->rx_m != NULL) {
308016199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rx_tag, rxd->rx_dmamap,
308116199571SPyun YongHyeon BUS_DMASYNC_POSTREAD);
308216199571SPyun YongHyeon bus_dmamap_unload(sc->age_cdata.age_rx_tag, rxd->rx_dmamap);
308316199571SPyun YongHyeon }
308416199571SPyun YongHyeon map = rxd->rx_dmamap;
308516199571SPyun YongHyeon rxd->rx_dmamap = sc->age_cdata.age_rx_sparemap;
308616199571SPyun YongHyeon sc->age_cdata.age_rx_sparemap = map;
308716199571SPyun YongHyeon bus_dmamap_sync(sc->age_cdata.age_rx_tag, rxd->rx_dmamap,
308816199571SPyun YongHyeon BUS_DMASYNC_PREREAD);
308916199571SPyun YongHyeon rxd->rx_m = m;
309016199571SPyun YongHyeon
309116199571SPyun YongHyeon desc = rxd->rx_desc;
309216199571SPyun YongHyeon desc->addr = htole64(segs[0].ds_addr);
309316199571SPyun YongHyeon desc->len = htole32((segs[0].ds_len & AGE_RD_LEN_MASK) <<
309416199571SPyun YongHyeon AGE_RD_LEN_SHIFT);
309516199571SPyun YongHyeon return (0);
309616199571SPyun YongHyeon }
309716199571SPyun YongHyeon
309816199571SPyun YongHyeon static void
age_rxvlan(struct age_softc * sc)309916199571SPyun YongHyeon age_rxvlan(struct age_softc *sc)
310016199571SPyun YongHyeon {
310152436412SJustin Hibbits if_t ifp;
310216199571SPyun YongHyeon uint32_t reg;
310316199571SPyun YongHyeon
310416199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
310516199571SPyun YongHyeon
310616199571SPyun YongHyeon ifp = sc->age_ifp;
310716199571SPyun YongHyeon reg = CSR_READ_4(sc, AGE_MAC_CFG);
310816199571SPyun YongHyeon reg &= ~MAC_CFG_VLAN_TAG_STRIP;
310952436412SJustin Hibbits if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
311016199571SPyun YongHyeon reg |= MAC_CFG_VLAN_TAG_STRIP;
311116199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
311216199571SPyun YongHyeon }
311316199571SPyun YongHyeon
31148f49db67SGleb Smirnoff static u_int
age_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)31158f49db67SGleb Smirnoff age_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
31168f49db67SGleb Smirnoff {
31178f49db67SGleb Smirnoff uint32_t *mchash = arg;
31188f49db67SGleb Smirnoff uint32_t crc;
31198f49db67SGleb Smirnoff
31208f49db67SGleb Smirnoff crc = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN);
31218f49db67SGleb Smirnoff mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
31228f49db67SGleb Smirnoff
31238f49db67SGleb Smirnoff return (1);
31248f49db67SGleb Smirnoff }
31258f49db67SGleb Smirnoff
312616199571SPyun YongHyeon static void
age_rxfilter(struct age_softc * sc)312716199571SPyun YongHyeon age_rxfilter(struct age_softc *sc)
312816199571SPyun YongHyeon {
312952436412SJustin Hibbits if_t ifp;
313016199571SPyun YongHyeon uint32_t mchash[2];
313116199571SPyun YongHyeon uint32_t rxcfg;
313216199571SPyun YongHyeon
313316199571SPyun YongHyeon AGE_LOCK_ASSERT(sc);
313416199571SPyun YongHyeon
313516199571SPyun YongHyeon ifp = sc->age_ifp;
313616199571SPyun YongHyeon
313716199571SPyun YongHyeon rxcfg = CSR_READ_4(sc, AGE_MAC_CFG);
313816199571SPyun YongHyeon rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
313952436412SJustin Hibbits if ((if_getflags(ifp) & IFF_BROADCAST) != 0)
314016199571SPyun YongHyeon rxcfg |= MAC_CFG_BCAST;
314152436412SJustin Hibbits if ((if_getflags(ifp) & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
314252436412SJustin Hibbits if ((if_getflags(ifp) & IFF_PROMISC) != 0)
314316199571SPyun YongHyeon rxcfg |= MAC_CFG_PROMISC;
314452436412SJustin Hibbits if ((if_getflags(ifp) & IFF_ALLMULTI) != 0)
314516199571SPyun YongHyeon rxcfg |= MAC_CFG_ALLMULTI;
314616199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAR0, 0xFFFFFFFF);
314716199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAR1, 0xFFFFFFFF);
314816199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, rxcfg);
314916199571SPyun YongHyeon return;
315016199571SPyun YongHyeon }
315116199571SPyun YongHyeon
315216199571SPyun YongHyeon /* Program new filter. */
315316199571SPyun YongHyeon bzero(mchash, sizeof(mchash));
31548f49db67SGleb Smirnoff if_foreach_llmaddr(ifp, age_hash_maddr, mchash);
315516199571SPyun YongHyeon
315616199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAR0, mchash[0]);
315716199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAR1, mchash[1]);
315816199571SPyun YongHyeon CSR_WRITE_4(sc, AGE_MAC_CFG, rxcfg);
315916199571SPyun YongHyeon }
316016199571SPyun YongHyeon
316116199571SPyun YongHyeon static int
sysctl_age_stats(SYSCTL_HANDLER_ARGS)316216199571SPyun YongHyeon sysctl_age_stats(SYSCTL_HANDLER_ARGS)
316316199571SPyun YongHyeon {
316416199571SPyun YongHyeon struct age_softc *sc;
316516199571SPyun YongHyeon struct age_stats *stats;
316616199571SPyun YongHyeon int error, result;
316716199571SPyun YongHyeon
316816199571SPyun YongHyeon result = -1;
316916199571SPyun YongHyeon error = sysctl_handle_int(oidp, &result, 0, req);
317016199571SPyun YongHyeon
317116199571SPyun YongHyeon if (error != 0 || req->newptr == NULL)
317216199571SPyun YongHyeon return (error);
317316199571SPyun YongHyeon
317416199571SPyun YongHyeon if (result != 1)
317516199571SPyun YongHyeon return (error);
317616199571SPyun YongHyeon
317716199571SPyun YongHyeon sc = (struct age_softc *)arg1;
317816199571SPyun YongHyeon stats = &sc->age_stat;
317916199571SPyun YongHyeon printf("%s statistics:\n", device_get_nameunit(sc->age_dev));
318016199571SPyun YongHyeon printf("Transmit good frames : %ju\n",
318116199571SPyun YongHyeon (uintmax_t)stats->tx_frames);
318216199571SPyun YongHyeon printf("Transmit good broadcast frames : %ju\n",
318316199571SPyun YongHyeon (uintmax_t)stats->tx_bcast_frames);
318416199571SPyun YongHyeon printf("Transmit good multicast frames : %ju\n",
318516199571SPyun YongHyeon (uintmax_t)stats->tx_mcast_frames);
318616199571SPyun YongHyeon printf("Transmit pause control frames : %u\n",
318716199571SPyun YongHyeon stats->tx_pause_frames);
318816199571SPyun YongHyeon printf("Transmit control frames : %u\n",
318916199571SPyun YongHyeon stats->tx_control_frames);
319016199571SPyun YongHyeon printf("Transmit frames with excessive deferrals : %u\n",
319116199571SPyun YongHyeon stats->tx_excess_defer);
319216199571SPyun YongHyeon printf("Transmit deferrals : %u\n",
319316199571SPyun YongHyeon stats->tx_deferred);
319416199571SPyun YongHyeon printf("Transmit good octets : %ju\n",
319516199571SPyun YongHyeon (uintmax_t)stats->tx_bytes);
319616199571SPyun YongHyeon printf("Transmit good broadcast octets : %ju\n",
319716199571SPyun YongHyeon (uintmax_t)stats->tx_bcast_bytes);
319816199571SPyun YongHyeon printf("Transmit good multicast octets : %ju\n",
319916199571SPyun YongHyeon (uintmax_t)stats->tx_mcast_bytes);
320016199571SPyun YongHyeon printf("Transmit frames 64 bytes : %ju\n",
320116199571SPyun YongHyeon (uintmax_t)stats->tx_pkts_64);
320216199571SPyun YongHyeon printf("Transmit frames 65 to 127 bytes : %ju\n",
320316199571SPyun YongHyeon (uintmax_t)stats->tx_pkts_65_127);
320416199571SPyun YongHyeon printf("Transmit frames 128 to 255 bytes : %ju\n",
320516199571SPyun YongHyeon (uintmax_t)stats->tx_pkts_128_255);
320616199571SPyun YongHyeon printf("Transmit frames 256 to 511 bytes : %ju\n",
320716199571SPyun YongHyeon (uintmax_t)stats->tx_pkts_256_511);
320816199571SPyun YongHyeon printf("Transmit frames 512 to 1024 bytes : %ju\n",
320916199571SPyun YongHyeon (uintmax_t)stats->tx_pkts_512_1023);
321016199571SPyun YongHyeon printf("Transmit frames 1024 to 1518 bytes : %ju\n",
321116199571SPyun YongHyeon (uintmax_t)stats->tx_pkts_1024_1518);
321216199571SPyun YongHyeon printf("Transmit frames 1519 to MTU bytes : %ju\n",
321316199571SPyun YongHyeon (uintmax_t)stats->tx_pkts_1519_max);
321416199571SPyun YongHyeon printf("Transmit single collisions : %u\n",
321516199571SPyun YongHyeon stats->tx_single_colls);
321616199571SPyun YongHyeon printf("Transmit multiple collisions : %u\n",
321716199571SPyun YongHyeon stats->tx_multi_colls);
321816199571SPyun YongHyeon printf("Transmit late collisions : %u\n",
321916199571SPyun YongHyeon stats->tx_late_colls);
322016199571SPyun YongHyeon printf("Transmit abort due to excessive collisions : %u\n",
322116199571SPyun YongHyeon stats->tx_excess_colls);
322216199571SPyun YongHyeon printf("Transmit underruns due to FIFO underruns : %u\n",
322316199571SPyun YongHyeon stats->tx_underrun);
322416199571SPyun YongHyeon printf("Transmit descriptor write-back errors : %u\n",
322516199571SPyun YongHyeon stats->tx_desc_underrun);
322616199571SPyun YongHyeon printf("Transmit frames with length mismatched frame size : %u\n",
322716199571SPyun YongHyeon stats->tx_lenerrs);
322816199571SPyun YongHyeon printf("Transmit frames with truncated due to MTU size : %u\n",
322916199571SPyun YongHyeon stats->tx_lenerrs);
323016199571SPyun YongHyeon
323116199571SPyun YongHyeon printf("Receive good frames : %ju\n",
323216199571SPyun YongHyeon (uintmax_t)stats->rx_frames);
323316199571SPyun YongHyeon printf("Receive good broadcast frames : %ju\n",
323416199571SPyun YongHyeon (uintmax_t)stats->rx_bcast_frames);
323516199571SPyun YongHyeon printf("Receive good multicast frames : %ju\n",
323616199571SPyun YongHyeon (uintmax_t)stats->rx_mcast_frames);
323716199571SPyun YongHyeon printf("Receive pause control frames : %u\n",
323816199571SPyun YongHyeon stats->rx_pause_frames);
323916199571SPyun YongHyeon printf("Receive control frames : %u\n",
324016199571SPyun YongHyeon stats->rx_control_frames);
324116199571SPyun YongHyeon printf("Receive CRC errors : %u\n",
324216199571SPyun YongHyeon stats->rx_crcerrs);
324316199571SPyun YongHyeon printf("Receive frames with length errors : %u\n",
324416199571SPyun YongHyeon stats->rx_lenerrs);
324516199571SPyun YongHyeon printf("Receive good octets : %ju\n",
324616199571SPyun YongHyeon (uintmax_t)stats->rx_bytes);
324716199571SPyun YongHyeon printf("Receive good broadcast octets : %ju\n",
324816199571SPyun YongHyeon (uintmax_t)stats->rx_bcast_bytes);
324916199571SPyun YongHyeon printf("Receive good multicast octets : %ju\n",
325016199571SPyun YongHyeon (uintmax_t)stats->rx_mcast_bytes);
325116199571SPyun YongHyeon printf("Receive frames too short : %u\n",
325216199571SPyun YongHyeon stats->rx_runts);
325316199571SPyun YongHyeon printf("Receive fragmented frames : %ju\n",
325416199571SPyun YongHyeon (uintmax_t)stats->rx_fragments);
325516199571SPyun YongHyeon printf("Receive frames 64 bytes : %ju\n",
325616199571SPyun YongHyeon (uintmax_t)stats->rx_pkts_64);
325716199571SPyun YongHyeon printf("Receive frames 65 to 127 bytes : %ju\n",
325816199571SPyun YongHyeon (uintmax_t)stats->rx_pkts_65_127);
325916199571SPyun YongHyeon printf("Receive frames 128 to 255 bytes : %ju\n",
326016199571SPyun YongHyeon (uintmax_t)stats->rx_pkts_128_255);
326116199571SPyun YongHyeon printf("Receive frames 256 to 511 bytes : %ju\n",
326216199571SPyun YongHyeon (uintmax_t)stats->rx_pkts_256_511);
326316199571SPyun YongHyeon printf("Receive frames 512 to 1024 bytes : %ju\n",
326416199571SPyun YongHyeon (uintmax_t)stats->rx_pkts_512_1023);
326516199571SPyun YongHyeon printf("Receive frames 1024 to 1518 bytes : %ju\n",
326616199571SPyun YongHyeon (uintmax_t)stats->rx_pkts_1024_1518);
326716199571SPyun YongHyeon printf("Receive frames 1519 to MTU bytes : %ju\n",
326816199571SPyun YongHyeon (uintmax_t)stats->rx_pkts_1519_max);
326916199571SPyun YongHyeon printf("Receive frames too long : %ju\n",
327016199571SPyun YongHyeon (uint64_t)stats->rx_pkts_truncated);
327116199571SPyun YongHyeon printf("Receive frames with FIFO overflow : %u\n",
327216199571SPyun YongHyeon stats->rx_fifo_oflows);
327316199571SPyun YongHyeon printf("Receive frames with return descriptor overflow : %u\n",
327416199571SPyun YongHyeon stats->rx_desc_oflows);
327516199571SPyun YongHyeon printf("Receive frames with alignment errors : %u\n",
327616199571SPyun YongHyeon stats->rx_alignerrs);
327716199571SPyun YongHyeon printf("Receive frames dropped due to address filtering : %ju\n",
327816199571SPyun YongHyeon (uint64_t)stats->rx_pkts_filtered);
327916199571SPyun YongHyeon
328016199571SPyun YongHyeon return (error);
328116199571SPyun YongHyeon }
328216199571SPyun YongHyeon
328316199571SPyun YongHyeon static int
sysctl_int_range(SYSCTL_HANDLER_ARGS,int low,int high)328416199571SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
328516199571SPyun YongHyeon {
328616199571SPyun YongHyeon int error, value;
328716199571SPyun YongHyeon
328816199571SPyun YongHyeon if (arg1 == NULL)
328916199571SPyun YongHyeon return (EINVAL);
329016199571SPyun YongHyeon value = *(int *)arg1;
329116199571SPyun YongHyeon error = sysctl_handle_int(oidp, &value, 0, req);
329216199571SPyun YongHyeon if (error || req->newptr == NULL)
329316199571SPyun YongHyeon return (error);
329416199571SPyun YongHyeon if (value < low || value > high)
329516199571SPyun YongHyeon return (EINVAL);
329616199571SPyun YongHyeon *(int *)arg1 = value;
329716199571SPyun YongHyeon
329816199571SPyun YongHyeon return (0);
329916199571SPyun YongHyeon }
330016199571SPyun YongHyeon
330116199571SPyun YongHyeon static int
sysctl_hw_age_proc_limit(SYSCTL_HANDLER_ARGS)330216199571SPyun YongHyeon sysctl_hw_age_proc_limit(SYSCTL_HANDLER_ARGS)
330316199571SPyun YongHyeon {
330416199571SPyun YongHyeon return (sysctl_int_range(oidp, arg1, arg2, req,
330516199571SPyun YongHyeon AGE_PROC_MIN, AGE_PROC_MAX));
330616199571SPyun YongHyeon }
330716199571SPyun YongHyeon
330816199571SPyun YongHyeon static int
sysctl_hw_age_int_mod(SYSCTL_HANDLER_ARGS)330916199571SPyun YongHyeon sysctl_hw_age_int_mod(SYSCTL_HANDLER_ARGS)
331016199571SPyun YongHyeon {
331116199571SPyun YongHyeon
331216199571SPyun YongHyeon return (sysctl_int_range(oidp, arg1, arg2, req, AGE_IM_TIMER_MIN,
331316199571SPyun YongHyeon AGE_IM_TIMER_MAX));
331416199571SPyun YongHyeon }
3315