diff options
| author | bouyer <bouyer@NetBSD.org> | 2008-10-11 21:54:11 +0000 |
|---|---|---|
| committer | bouyer <bouyer@NetBSD.org> | 2008-10-11 21:54:11 +0000 |
| commit | 540cab1ec8c53aa815d6a89fdb69aefe9e5ac059 (patch) | |
| tree | 446b923744c1f059ce18c2e5556de6bb92b48658 /sys/dev | |
| parent | 9fac915750adac14c8be50a03bd67b3ea5a0a0b0 (diff) | |
jme(4), a driver for JMicron Technologies JME250 Gigabit Ethernet and
JME260 Fast Ethernet PCI Express controllers.
Written with a lot of cut-n-paste from the FreeBSD jme(4) driver.
No support for jumbo ethernet frames yet (but should come soon).
Thanks to JMicron Technologies for providing me sample boards and
documentation for this work.
Diffstat (limited to 'sys/dev')
| -rw-r--r-- | sys/dev/DEVNAMES | 3 | ||||
| -rw-r--r-- | sys/dev/pci/files.pci | 7 | ||||
| -rw-r--r-- | sys/dev/pci/if_jme.c | 2062 | ||||
| -rw-r--r-- | sys/dev/pci/if_jmereg.h | 1043 |
4 files changed, 3113 insertions, 2 deletions
diff --git a/sys/dev/DEVNAMES b/sys/dev/DEVNAMES index aa095c36abe..c3219eb73af 100644 --- a/sys/dev/DEVNAMES +++ b/sys/dev/DEVNAMES @@ -1,4 +1,4 @@ -# $NetBSD: DEVNAMES,v 1.241 2008/09/30 16:52:39 jmcneill Exp $ +# $NetBSD: DEVNAMES,v 1.242 2008/10/11 21:54:12 bouyer Exp $ # # This file contains all used device names and defined attributes in # alphabetical order. New devices added to the system somewhere should first @@ -685,6 +685,7 @@ j720lcd hpcarm j720pwr hpcarm j720ssp hpcarm j720tp hpcarm +jme MI joy arc joy bebox joy cats diff --git a/sys/dev/pci/files.pci b/sys/dev/pci/files.pci index 5ced6bdb912..d41bb314e82 100644 --- a/sys/dev/pci/files.pci +++ b/sys/dev/pci/files.pci @@ -1,4 +1,4 @@ -# $NetBSD: files.pci,v 1.305 2008/07/21 21:36:05 joerg Exp $ +# $NetBSD: files.pci,v 1.306 2008/10/11 21:54:12 bouyer Exp $ # # Config file and device description for machine-independent PCI code. # Included by ports that need it. Requires that the SCSI files be @@ -363,6 +363,11 @@ file dev/pci/if_hme_pci.c hme_pci attach gem at pci with gem_pci file dev/pci/if_gem_pci.c gem_pci +# JMicron JMC2[56]0 ethernet controllers +device jme: ether, ifnet, arp, mii +attach jme at pci +file dev/pci/if_jme.c jme + # NE2000-compatible PCI Ethernet cards defparam opt_ipkdb.h IPKDB_NE_PCISLOT defflag opt_ipkdb.h IPKDB_NE_PCI : IPKDB_NE diff --git a/sys/dev/pci/if_jme.c b/sys/dev/pci/if_jme.c new file mode 100644 index 00000000000..ff37561fbd2 --- /dev/null +++ b/sys/dev/pci/if_jme.c @@ -0,0 +1,2062 @@ +/* $NetBSD: if_jme.c,v 1.1 2008/10/11 21:54:12 bouyer Exp $ */ + +/* + * Copyright (c) 2008 Manuel Bouyer. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Manuel Bouyer. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/*- + * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + + +/* + * Driver for JMicron Technologies JMC250 (Giganbit) and JMC260 (Fast) + * Ethernet Controllers. + */ + +#include <sys/cdefs.h> +__KERNEL_RCSID(0, "$NetBSD: if_jme.c,v 1.1 2008/10/11 21:54:12 bouyer Exp $"); + + +#include <sys/param.h> +#include <sys/systm.h> +#include <sys/mbuf.h> +#include <sys/protosw.h> +#include <sys/socket.h> +#include <sys/ioctl.h> +#include <sys/errno.h> +#include <sys/malloc.h> +#include <sys/kernel.h> +#include <sys/proc.h> /* only for declaration of wakeup() used by vm.h */ +#include <sys/device.h> +#include <sys/syslog.h> +#include <sys/sysctl.h> + +#include <net/if.h> +#if defined(SIOCSIFMEDIA) +#include <net/if_media.h> +#endif +#include <net/if_types.h> +#include <net/if_dl.h> +#include <net/route.h> +#include <net/netisr.h> + +#include "bpfilter.h" +#if NBPFILTER > 0 +#include <net/bpf.h> +#include <net/bpfdesc.h> +#endif + +#include "rnd.h" +#if NRND > 0 +#include <sys/rnd.h> +#endif + +#ifdef INET +#include <netinet/in.h> +#include <netinet/in_systm.h> +#include <netinet/in_var.h> +#include <netinet/ip.h> +#include <netinet/tcp.h> +#endif + + +#include <net/if_ether.h> +#include <uvm/uvm_extern.h> +#if defined(INET) +#include <netinet/if_inarp.h> +#endif + +#include <sys/bus.h> +#include <sys/intr.h> + +#include <dev/pci/pcireg.h> +#include <dev/pci/pcivar.h> +#include <dev/pci/pcidevs.h> +#include <dev/pci/if_jmereg.h> + +#include <dev/mii/mii.h> +#include <dev/mii/miivar.h> + +struct jme_product_desc { + u_int32_t jme_product; + const char *jme_desc; +}; + +/* number of entries in transmit and receive rings */ +#define JME_NBUFS (PAGE_SIZE / sizeof(struct jme_desc)) + +#define JME_DESC_INC(x, y) ((x) = ((x) + 1) % (y)) + +/* Water mark to kick reclaiming Tx buffers. */ +#define JME_TX_DESC_HIWAT (JME_NBUFS - (((JME_NBUFS) * 3) / 10)) + + +struct jme_softc { + device_t jme_dev; /* base device */ + bus_space_tag_t jme_bt_mac; + bus_space_handle_t jme_bh_mac; /* Mac registers */ + bus_space_tag_t jme_bt_phy; + bus_space_handle_t jme_bh_phy; /* PHY registers */ + bus_space_tag_t jme_bt_misc; + bus_space_handle_t jme_bh_misc; /* Misc registers */ + bus_dma_tag_t jme_dmatag; + bus_dma_segment_t jme_txseg; /* transmit ring seg */ + bus_dmamap_t jme_txmap; /* transmit ring DMA map */ + struct jme_desc* jme_txring; /* transmit ring */ + bus_dmamap_t jme_txmbufm[JME_NBUFS]; /* transmit mbufs DMA map */ + struct mbuf *jme_txmbuf[JME_NBUFS]; /* mbufs being transmitted */ + int jme_tx_cons; /* transmit ring consumer */ + int jme_tx_prod; /* transmit ring producer */ + int jme_tx_cnt; /* transmit ring active count */ + bus_dma_segment_t jme_rxseg; /* receive ring seg */ + bus_dmamap_t jme_rxmap; /* receive ring DMA map */ + struct jme_desc* jme_rxring; /* receive ring */ + bus_dmamap_t jme_rxmbufm[JME_NBUFS]; /* receive mbufs DMA map */ + struct mbuf *jme_rxmbuf[JME_NBUFS]; /* mbufs being received */ + int jme_rx_cons; /* receive ring consumer */ + void* jme_ih; /* our interrupt */ + struct ethercom jme_ec; + struct callout jme_tick_ch; /* tick callout */ + u_int8_t jme_enaddr[ETHER_ADDR_LEN];/* hardware address */ + u_int8_t jme_phyaddr; /* address of integrated phy */ + u_int8_t jme_chip_rev; /* chip revision */ + u_int8_t jme_rev; /* PCI revision */ + mii_data_t jme_mii; /* mii bus */ + u_int32_t jme_flags; /* device features, see below */ + uint32_t jme_txcsr; /* TX config register */ + uint32_t jme_rxcsr; /* RX config register */ +#if NRND > 0 + rndsource_element_t rnd_source; +#endif + /* interrupt coalition parameters */ + struct sysctllog *jme_clog; + int jme_intrxto; /* interrupt RX timeout */ + int jme_intrxct; /* interrupt RX packets counter */ + int jme_inttxto; /* interrupt TX timeout */ + int jme_inttxct; /* interrupt TX packets counter */ +}; + +#define JME_FLAG_FPGA 0x0001 /* FPGA version */ +#define JME_FLAG_GIGA 0x0002 /* giga Ethernet capable */ + + +#define jme_if jme_ec.ec_if +#define jme_bpf jme_if.if_bpf + +typedef struct jme_softc jme_softc_t; +typedef u_long ioctl_cmd_t; + +static int jme_pci_match(device_t, cfdata_t, void *); +static void jme_pci_attach(device_t, device_t, void *); +static int jme_intr(void *); + +static int jme_ifioctl(struct ifnet *, ioctl_cmd_t, void *); +static int jme_mediachange(struct ifnet *); +static void jme_ifwatchdog(struct ifnet *); +static void jme_shutdown(void *); + +static void jme_txeof(struct jme_softc *); +static void jme_ifstart(struct ifnet *); +static void jme_reset(jme_softc_t *); +static int jme_ifinit(struct ifnet *); +static int jme_init(struct ifnet *, int); +static void jme_stop(struct ifnet *, int); +// static void jme_restart(void *); +static void jme_ticks(void *); +static void jme_mac_config(jme_softc_t *); +static void jme_set_filter(jme_softc_t *); + +int jme_mii_read(device_t, int, int); +void jme_mii_write(device_t, int, int, int); +void jme_statchg(device_t); + +static int jme_eeprom_read_byte(struct jme_softc *, uint8_t, uint8_t *); +static int jme_eeprom_macaddr(struct jme_softc *); + +#define JME_TIMEOUT 1000 +#define JME_PHY_TIMEOUT 1000 +#define JME_EEPROM_TIMEOUT 1000 + +static int jme_sysctl_intrxto(SYSCTLFN_PROTO); +static int jme_sysctl_intrxct(SYSCTLFN_PROTO); +static int jme_sysctl_inttxto(SYSCTLFN_PROTO); +static int jme_sysctl_inttxct(SYSCTLFN_PROTO); +static int jme_root_num; + + +CFATTACH_DECL_NEW(jme, sizeof(jme_softc_t), + jme_pci_match, jme_pci_attach, NULL, NULL); + +static const struct jme_product_desc jme_products[] = { + { PCI_PRODUCT_JMICRON_JMC250, + "JMicron JMC250 Gigabit Ethernet Controller" }, + { PCI_PRODUCT_JMICRON_JMC260, + "JMicron JMC260 Gigabit Ethernet Controller" }, + { 0, NULL }, +}; + +static const struct jme_product_desc *jme_lookup_product(uint32_t); + +static const struct jme_product_desc * +jme_lookup_product(uint32_t id) +{ + const struct jme_product_desc *jp; + + for (jp = jme_products ; jp->jme_desc != NULL; jp++) + if (PCI_PRODUCT(id) == jp->jme_product) + return jp; + + return NULL; +} + +static int +jme_pci_match(device_t parent, cfdata_t cf, void *aux) +{ + struct pci_attach_args *pa = (struct pci_attach_args *)aux; + + if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_JMICRON) + return 0; + + if (jme_lookup_product(pa->pa_id) != NULL) + return 1; + + return 0; +} + +static void +jme_pci_attach(device_t parent, device_t self, void *aux) +{ + jme_softc_t *sc = device_private(self); + struct pci_attach_args * const pa = (struct pci_attach_args *)aux; + const struct jme_product_desc *jp; + struct ifnet * const ifp = &sc->jme_if; + bus_space_tag_t iot1, iot2, memt; + bus_space_handle_t ioh1, ioh2, memh; + bus_size_t size, size2; + pci_intr_handle_t intrhandle; + const char *intrstr; + pcireg_t csr; + int nsegs, i; + const struct sysctlnode *node; + int jme_nodenum; + + sc->jme_dev = self; + aprint_normal("\n"); + callout_init(&sc->jme_tick_ch, 0); + + jp = jme_lookup_product(pa->pa_id); + if (jp == NULL) + panic("jme_pci_attach: impossible"); + + if (jp->jme_product == PCI_PRODUCT_JMICRON_JMC250) + sc->jme_flags = JME_FLAG_GIGA; + + /* + * Map the card space. Try Mem first. + */ + if (pci_mapreg_map(pa, JME_PCI_BAR0, + PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, + 0, &memt, &memh, NULL, &size) == 0) { + sc->jme_bt_mac = memt; + sc->jme_bh_mac = memh; + sc->jme_bt_phy = memt; + if (bus_space_subregion(memt, memh, JME_PHY_EEPROM_BASE_MEMOFF, + JME_PHY_EEPROM_SIZE, &sc->jme_bh_phy) != 0) { + aprint_error_dev(self, "can't subregion PHY space\n"); + bus_space_unmap(memt, memh, size); + return; + } + sc->jme_bt_misc = memt; + if (bus_space_subregion(memt, memh, JME_MISC_BASE_MEMOFF, + JME_MISC_SIZE, &sc->jme_bh_misc) != 0) { + aprint_error_dev(self, "can't subregion misc space\n"); + bus_space_unmap(memt, memh, size); + return; + } + } else { + if (pci_mapreg_map(pa, JME_PCI_BAR1, PCI_MAPREG_TYPE_IO, + 0, &iot1, &ioh1, NULL, &size) != 0) { + aprint_error_dev(self, "can't map I/O space 1\n"); + return; + } + sc->jme_bt_mac = iot1; + sc->jme_bh_mac = ioh1; + if (pci_mapreg_map(pa, JME_PCI_BAR2, PCI_MAPREG_TYPE_IO, + 0, &iot2, &ioh2, NULL, &size2) != 0) { + aprint_error_dev(self, "can't map I/O space 2\n"); + bus_space_unmap(iot1, ioh1, size); + return; + } + sc->jme_bt_phy = iot2; + sc->jme_bh_phy = ioh2; + sc->jme_bt_misc = iot2; + if (bus_space_subregion(iot2, ioh2, JME_MISC_BASE_IOOFF, + JME_MISC_SIZE, &sc->jme_bh_misc) != 0) { + aprint_error_dev(self, "can't subregion misc space\n"); + bus_space_unmap(iot1, ioh1, size); + bus_space_unmap(iot2, ioh2, size2); + return; + } + } + + if (pci_dma64_available(pa)) + sc->jme_dmatag = pa->pa_dmat64; + else + sc->jme_dmatag = pa->pa_dmat; + + /* Enable the device. */ + csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG); + pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, + csr | PCI_COMMAND_MASTER_ENABLE); + + aprint_normal_dev(self, "%s\n", jp->jme_desc); + + sc->jme_rev = PCI_REVISION(pa->pa_class); + + csr = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_CHIPMODE); + if (((csr & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT) != + CHIPMODE_NOT_FPGA) + sc->jme_flags |= JME_FLAG_FPGA; + sc->jme_chip_rev = (csr & CHIPMODE_REV_MASK) >> CHIPMODE_REV_SHIFT; + aprint_verbose_dev(self, "PCI device revision : 0x%x, Chip revision: " + "0x%x", sc->jme_rev, sc->jme_chip_rev); + if (sc->jme_flags & JME_FLAG_FPGA) + aprint_verbose(" FPGA revision: 0x%x", + (csr & CHIPMODE_FPGA_REV_MASK) >> CHIPMODE_FPGA_REV_SHIFT); + aprint_verbose("\n"); + + /* + * Save PHY address. + * Integrated JR0211 has fixed PHY address whereas FPGA version + * requires PHY probing to get correct PHY address. + */ + if ((sc->jme_flags & JME_FLAG_FPGA) == 0) { + sc->jme_phyaddr = + bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, + JME_GPREG0) & GPREG0_PHY_ADDR_MASK; + } else + sc->jme_phyaddr = 0; + + + jme_reset(sc); + + /* read mac addr */ + if (jme_eeprom_macaddr(sc)) { + aprint_error_dev(self, "error reading Ethernet address\n"); + /* return; */ + } + aprint_normal_dev(self, "Ethernet address %s\n", + ether_sprintf(sc->jme_enaddr)); + + /* Map and establish interrupts */ + if (pci_intr_map(pa, &intrhandle)) { + aprint_error_dev(self, "couldn't map interrupt\n"); + return; + } + intrstr = pci_intr_string(pa->pa_pc, intrhandle); + sc->jme_if.if_softc = sc; + sc->jme_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET, + jme_intr, sc); + if (sc->jme_ih == NULL) { + aprint_error_dev(self, "couldn't establish interrupt"); + if (intrstr != NULL) + aprint_error(" at %s", intrstr); + aprint_error("\n"); + return; + } + aprint_normal_dev(self, "interrupting at %s\n", intrstr); + + /* allocate and map DMA-safe memory for transmit ring */ + if (bus_dmamem_alloc(sc->jme_dmatag, PAGE_SIZE, 0, PAGE_SIZE, + &sc->jme_txseg, 1, &nsegs, BUS_DMA_NOWAIT) != 0 || + bus_dmamem_map(sc->jme_dmatag, &sc->jme_txseg, + nsegs, PAGE_SIZE, (void **)&sc->jme_txring, + BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0 || + bus_dmamap_create(sc->jme_dmatag, PAGE_SIZE, 1, PAGE_SIZE, 0, + BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->jme_txmap) != 0 || + bus_dmamap_load(sc->jme_dmatag, sc->jme_txmap, sc->jme_txring, + PAGE_SIZE, NULL, BUS_DMA_NOWAIT) != 0) { + aprint_error_dev(self, "can't allocate DMA memory TX ring\n"); + return; + } + /* allocate and map DMA-safe memory for receive ring */ + if (bus_dmamem_alloc(sc->jme_dmatag, PAGE_SIZE, 0, PAGE_SIZE, + &sc->jme_rxseg, 1, &nsegs, BUS_DMA_NOWAIT) != 0 || + bus_dmamem_map(sc->jme_dmatag, &sc->jme_rxseg, + nsegs, PAGE_SIZE, (void **)&sc->jme_rxring, + BUS_DMA_NOWAIT | BUS_DMA_COHERENT) != 0 || + bus_dmamap_create(sc->jme_dmatag, PAGE_SIZE, 1, PAGE_SIZE, 0, + BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &sc->jme_rxmap) != 0 || + bus_dmamap_load(sc->jme_dmatag, sc->jme_rxmap, sc->jme_rxring, + PAGE_SIZE, NULL, BUS_DMA_NOWAIT) != 0) { + aprint_error_dev(self, "can't allocate DMA memory RX ring\n"); + return; + } + for (i = 0; i < JME_NBUFS; i++) { + sc->jme_txmbuf[i] = sc->jme_rxmbuf[i] = NULL; + if (bus_dmamap_create(sc->jme_dmatag, JME_MAX_TX_LEN, + JME_NBUFS, JME_MAX_TX_LEN, 0, BUS_DMA_NOWAIT, + &sc->jme_txmbufm[i]) != 0) { + aprint_error_dev(self, "can't allocate DMA TX map\n"); + return; + } + if (bus_dmamap_create(sc->jme_dmatag, JME_MAX_RX_LEN, + 1, JME_MAX_RX_LEN, 0, BUS_DMA_NOWAIT, + &sc->jme_rxmbufm[i]) != 0) { + aprint_error_dev(self, "can't allocate DMA RX map\n"); + return; + } + } + /* + * Add shutdown hook so that DMA is disabled prior to reboot. + */ + (void)shutdownhook_establish(jme_shutdown, ifp); + + /* + * Initialize our media structures and probe the MII. + * + * Note that we don't care about the media instance. We + * are expecting to have multiple PHYs on the 10/100 cards, + * and on those cards we exclude the internal PHY from providing + * 10baseT. By ignoring the instance, it allows us to not have + * to specify it on the command line when switching media. + */ + sc->jme_mii.mii_ifp = ifp; + sc->jme_mii.mii_readreg = jme_mii_read; + sc->jme_mii.mii_writereg = jme_mii_write; + sc->jme_mii.mii_statchg = jme_statchg; + sc->jme_ec.ec_mii = &sc->jme_mii; + ifmedia_init(&sc->jme_mii.mii_media, IFM_IMASK, jme_mediachange, + ether_mediastatus); + mii_attach(self, &sc->jme_mii, 0xffffffff, MII_PHY_ANY, + MII_OFFSET_ANY, 0); + if (LIST_FIRST(&sc->jme_mii.mii_phys) == NULL) { + ifmedia_add(&sc->jme_mii.mii_media, IFM_ETHER|IFM_NONE, 0, NULL); + ifmedia_set(&sc->jme_mii.mii_media, IFM_ETHER|IFM_NONE); + } else + ifmedia_set(&sc->jme_mii.mii_media, IFM_ETHER|IFM_AUTO); + + /* + * We can support 802.1Q VLAN-sized frames. + */ + sc->jme_ec.ec_capabilities |= + ETHERCAP_VLAN_MTU | ETHERCAP_VLAN_HWTAGGING; + + strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); + ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST; + ifp->if_ioctl = jme_ifioctl; + ifp->if_start = jme_ifstart; + ifp->if_watchdog = jme_ifwatchdog; + ifp->if_init = jme_ifinit; + ifp->if_stop = jme_stop; + ifp->if_timer = 0; + ifp->if_capabilities |= + IFCAP_CSUM_IPv4_Tx | IFCAP_CSUM_IPv4_Rx | + IFCAP_CSUM_TCPv4_Tx | IFCAP_CSUM_TCPv4_Rx | + IFCAP_CSUM_UDPv4_Tx | IFCAP_CSUM_UDPv4_Rx | + IFCAP_CSUM_TCPv6_Tx | /* IFCAP_CSUM_TCPv6_Rx | hardware bug */ + IFCAP_CSUM_UDPv6_Tx | /* IFCAP_CSUM_UDPv6_Rx | hardware bug */ + IFCAP_TSOv4 | IFCAP_TSOv6; + IFQ_SET_READY(&ifp->if_snd); + if_attach(ifp); + ether_ifattach(&(sc)->jme_if, (sc)->jme_enaddr); + +#if NRND > 0 + rnd_attach_source(&sc->rnd_source, device_xname(self), + RND_TYPE_NET, 0); +#endif + sc->jme_intrxto = PCCRX_COAL_TO_DEFAULT; + sc->jme_intrxct = PCCRX_COAL_PKT_DEFAULT; + sc->jme_inttxto = PCCTX_COAL_TO_DEFAULT; + sc->jme_inttxct = PCCTX_COAL_PKT_DEFAULT; + if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, + 0, CTLTYPE_NODE, device_xname(sc->jme_dev), + SYSCTL_DESCR("jme per-controller controls"), + NULL, 0, NULL, 0, CTL_HW, jme_root_num, CTL_CREATE, + CTL_EOL) != 0) { + aprint_normal_dev(sc->jme_dev, "couldn't create sysctl node\n"); + return; + } + jme_nodenum = node->sysctl_num; + + /* interrupt moderation sysctls */ + if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, + CTLFLAG_READWRITE, + CTLTYPE_INT, "int_rxto", + SYSCTL_DESCR("jme RX interrupt moderation timer"), + jme_sysctl_intrxto, 0, sc, + 0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE, + CTL_EOL) != 0) { + aprint_normal_dev(sc->jme_dev, + "couldn't create int_rxto sysctl node\n"); + } + if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, + CTLFLAG_READWRITE, + CTLTYPE_INT, "int_rxct", + SYSCTL_DESCR("jme RX interrupt moderation packet counter"), + jme_sysctl_intrxct, 0, sc, + 0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE, + CTL_EOL) != 0) { + aprint_normal_dev(sc->jme_dev, + "couldn't create int_rxct sysctl node\n"); + } + if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, + CTLFLAG_READWRITE, + CTLTYPE_INT, "int_txto", + SYSCTL_DESCR("jme TX interrupt moderation timer"), + jme_sysctl_inttxto, 0, sc, + 0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE, + CTL_EOL) != 0) { + aprint_normal_dev(sc->jme_dev, + "couldn't create int_txto sysctl node\n"); + } + if (sysctl_createv(&sc->jme_clog, 0, NULL, &node, + CTLFLAG_READWRITE, + CTLTYPE_INT, "int_txct", + SYSCTL_DESCR("jme TX interrupt moderation packet counter"), + jme_sysctl_inttxct, 0, sc, + 0, CTL_HW, jme_root_num, jme_nodenum, CTL_CREATE, + CTL_EOL) != 0) { + aprint_normal_dev(sc->jme_dev, + "couldn't create int_txct sysctl node\n"); + } +} + +static void +jme_stop_rx(jme_softc_t *sc) +{ + uint32_t reg; + int i; + + reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR); + if ((reg & RXCSR_RX_ENB) == 0) + return; + reg &= ~RXCSR_RX_ENB; + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR, reg); + for (i = JME_TIMEOUT / 10; i > 0; i--) { + DELAY(10); + if ((bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_RXCSR) & RXCSR_RX_ENB) == 0) + break; + } + if (i == 0) + aprint_error_dev(sc->jme_dev, "stopping recevier timeout!\n"); + +} + +static void +jme_stop_tx(jme_softc_t *sc) +{ + uint32_t reg; + int i; + + reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR); + if ((reg & TXCSR_TX_ENB) == 0) + return; + reg &= ~TXCSR_TX_ENB; + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR, reg); + for (i = JME_TIMEOUT / 10; i > 0; i--) { + DELAY(10); + if ((bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_TXCSR) & TXCSR_TX_ENB) == 0) + break; + } + if (i == 0) + aprint_error_dev(sc->jme_dev, + "stopping transmitter timeout!\n"); +} + +static void +jme_reset(jme_softc_t *sc) +{ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, GHC_RESET); + DELAY(10); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, 0); +} + +static void +jme_shutdown(void *v) +{ + + jme_stop(v, 1); +} + +static void +jme_stop(struct ifnet *ifp, int disable) +{ + jme_softc_t *sc = ifp->if_softc; + int i; + /* Stop receiver, transmitter. */ + jme_stop_rx(sc); + jme_stop_tx(sc); + /* free receive mbufs */ + for (i = 0; i < JME_NBUFS; i++) { + if (sc->jme_rxmbuf[i]) { + bus_dmamap_unload(sc->jme_dmatag, sc->jme_rxmbufm[i]); + m_freem(sc->jme_rxmbuf[i]); + } + } + /* process completed transmits */ + jme_txeof(sc); + /* free abort pending transmits */ + for (i = 0; i < JME_NBUFS; i++) { + if (sc->jme_txmbuf[i]) { + bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[i]); + m_freem(sc->jme_txmbuf[i]); + sc->jme_txmbuf[i] = NULL; + } + } + ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); + ifp->if_timer = 0; +} + +#if 0 +static void +jme_restart(void *v) +{ + + jme_init(v); +} +#endif + +static int +jme_add_rxbuf(jme_softc_t *sc, struct mbuf *m, int i) +{ + int error; + bus_dmamap_t map; + if (m == NULL) { + sc->jme_rxmbuf[i] = NULL; + MGETHDR(m, M_DONTWAIT, MT_DATA); + if (m == NULL) + return (ENOBUFS); + MCLGET(m, M_DONTWAIT); + if ((m->m_flags & M_EXT) == 0) { + m_freem(m); + return (ENOBUFS); + } + } + map = sc->jme_rxmbufm[i]; + m->m_len = m->m_pkthdr.len = m->m_ext.ext_size; + error = bus_dmamap_load_mbuf(sc->jme_dmatag, map, m, + BUS_DMA_READ|BUS_DMA_NOWAIT); + if (error) { + sc->jme_rxmbuf[i] = NULL; + aprint_error_dev(sc->jme_dev, + "unable to load rx DMA map %d, error = %d\n", + i, error); + m_freem(m); + return (error); + } + bus_dmamap_sync(sc->jme_dmatag, map, 0, map->dm_mapsize, + BUS_DMASYNC_PREREAD); + + sc->jme_rxmbuf[i] = m; + + sc->jme_rxring[i].buflen = htole32(map->dm_segs[0].ds_len); + sc->jme_rxring[i].addr_lo = + htole32(JME_ADDR_LO(map->dm_segs[0].ds_addr)); + sc->jme_rxring[i].addr_hi = + htole32(JME_ADDR_HI(map->dm_segs[0].ds_addr)); + sc->jme_rxring[i].flags = + htole32(JME_RD_OWN | JME_RD_INTR | JME_RD_64BIT); + bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmap, + i * sizeof(struct jme_desc), sizeof(struct jme_desc), + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); + return (0); +} + +static int +jme_ifinit(struct ifnet *ifp) +{ + return jme_init(ifp, 1); +} + +static int +jme_init(struct ifnet *ifp, int do_ifinit) +{ + jme_softc_t *sc = ifp->if_softc; + int i, s; + uint8_t eaddr[ETHER_ADDR_LEN]; + uint32_t reg; + + s = splnet(); + /* cancel any pending IO */ + jme_stop(ifp, 1); + jme_reset(sc); + if ((sc->jme_if.if_flags & IFF_UP) == 0) { + splx(s); + return 0; + } + /* allocate receive ring */ + for (i = 0; i < JME_NBUFS; i++) { + if (jme_add_rxbuf(sc, NULL, i) < 0) { + aprint_error_dev(sc->jme_dev, + "can't allocate rx mbuf\n"); + for (i--; i >= 0; i--) { + bus_dmamap_unload(sc->jme_dmatag, + sc->jme_rxmbufm[i]); + m_freem(sc->jme_rxmbuf[i]); + sc->jme_rxmbuf[i] = NULL; + } + splx(s); + return ENOMEM; + } + } + /* init TX ring */ + memset(sc->jme_txring, 0, JME_NBUFS * sizeof(struct jme_desc)); + bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, + 0, JME_NBUFS * sizeof(struct jme_desc), + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); + for (i = 0; i < JME_NBUFS; i++) + sc->jme_txmbuf[i] = NULL; + sc->jme_tx_cons = sc->jme_tx_prod = sc->jme_tx_cnt = 0; + + /* Reprogram the station address. */ + bcopy(CLLADDR(ifp->if_sadl), eaddr, ETHER_ADDR_LEN); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR0, + eaddr[3] << 24 | eaddr[2] << 16 | eaddr[1] << 8 | eaddr[0]); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_PAR1, eaddr[5] << 8 | eaddr[4]); + + /* + * Configure Tx queue. + * Tx priority queue weight value : 0 + * Tx FIFO threshold for processing next packet : 16QW + * Maximum Tx DMA length : 512 + * Allow Tx DMA burst. + */ + sc->jme_txcsr = TXCSR_TXQ_N_SEL(TXCSR_TXQ0); + sc->jme_txcsr |= TXCSR_TXQ_WEIGHT(TXCSR_TXQ_WEIGHT_MIN); + sc->jme_txcsr |= TXCSR_FIFO_THRESH_16QW; + sc->jme_txcsr |= TXCSR_DMA_SIZE_512; + sc->jme_txcsr |= TXCSR_DMA_BURST; + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_TXCSR, sc->jme_txcsr); + + /* Set Tx descriptor counter. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_TXQDC, JME_NBUFS); + + /* Set Tx ring address to the hardware. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI, + JME_ADDR_HI(sc->jme_txmap->dm_segs[0].ds_addr)); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO, + JME_ADDR_LO(sc->jme_txmap->dm_segs[0].ds_addr)); + + /* Configure TxMAC parameters. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC, + TXMAC_IFG1_DEFAULT | TXMAC_IFG2_DEFAULT | TXMAC_IFG_ENB | + TXMAC_THRESH_1_PKT | TXMAC_CRC_ENB | TXMAC_PAD_ENB); + + /* + * Configure Rx queue. + * FIFO full threshold for transmitting Tx pause packet : 128T + * FIFO threshold for processing next packet : 128QW + * Rx queue 0 select + * Max Rx DMA length : 128 + * Rx descriptor retry : 32 + * Rx descriptor retry time gap : 256ns + * Don't receive runt/bad frame. + */ + sc->jme_rxcsr = RXCSR_FIFO_FTHRESH_128T; + /* + * Since Rx FIFO size is 4K bytes, receiving frames larger + * than 4K bytes will suffer from Rx FIFO overruns. So + * decrease FIFO threshold to reduce the FIFO overruns for + * frames larger than 4000 bytes. + * For best performance of standard MTU sized frames use + * maximum allowable FIFO threshold, 128QW. + */ + if ((ifp->if_mtu + ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + + ETHER_CRC_LEN) > JME_RX_FIFO_SIZE) + sc->jme_rxcsr |= RXCSR_FIFO_THRESH_16QW; + else + sc->jme_rxcsr |= RXCSR_FIFO_THRESH_128QW; + sc->jme_rxcsr |= RXCSR_DMA_SIZE_128 | RXCSR_RXQ_N_SEL(RXCSR_RXQ0); + sc->jme_rxcsr |= RXCSR_DESC_RT_CNT(RXCSR_DESC_RT_CNT_DEFAULT); + sc->jme_rxcsr |= RXCSR_DESC_RT_GAP_256 & RXCSR_DESC_RT_GAP_MASK; + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_RXCSR, sc->jme_rxcsr); + + /* Set Rx descriptor counter. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_RXQDC, JME_NBUFS); + + /* Set Rx ring address to the hardware. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_HI, + JME_ADDR_HI(sc->jme_rxmap->dm_segs[0].ds_addr)); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_LO, + JME_ADDR_LO(sc->jme_rxmap->dm_segs[0].ds_addr)); + + /* Clear receive filter. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, 0); + /* Set up the receive filter. */ + jme_set_filter(sc); + + /* + * Disable all WOL bits as WOL can interfere normal Rx + * operation. Also clear WOL detection status bits. + */ + reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PMCS); + reg &= ~PMCS_WOL_ENB_MASK; + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PMCS, reg); + + reg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC); + /* + * Pad 10bytes right before received frame. This will greatly + * help Rx performance on strict-alignment architectures as + * it does not need to copy the frame to align the payload. + */ + reg |= RXMAC_PAD_10BYTES; + if ((ifp->if_capenable & + (IFCAP_CSUM_IPv4_Rx|IFCAP_CSUM_TCPv4_Rx|IFCAP_CSUM_UDPv4_Rx| + IFCAP_CSUM_TCPv6_Rx|IFCAP_CSUM_UDPv6_Rx)) != 0) + reg |= RXMAC_CSUM_ENB; + reg |= RXMAC_VLAN_ENB; /* enable hardware vlan */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, reg); + + /* Configure general purpose reg0 */ + reg = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_GPREG0); + reg &= ~GPREG0_PCC_UNIT_MASK; + /* Set PCC timer resolution to micro-seconds unit. */ + reg |= GPREG0_PCC_UNIT_US; + /* + * Disable all shadow register posting as we have to read + * JME_INTR_STATUS register in jme_int_task. Also it seems + * that it's hard to synchronize interrupt status between + * hardware and software with shadow posting due to + * requirements of bus_dmamap_sync(9). + */ + reg |= GPREG0_SH_POST_DW7_DIS | GPREG0_SH_POST_DW6_DIS | + GPREG0_SH_POST_DW5_DIS | GPREG0_SH_POST_DW4_DIS | + GPREG0_SH_POST_DW3_DIS | GPREG0_SH_POST_DW2_DIS | + GPREG0_SH_POST_DW1_DIS | GPREG0_SH_POST_DW0_DIS; + /* Disable posting of DW0. */ + reg &= ~GPREG0_POST_DW0_ENB; + /* Clear PME message. */ + reg &= ~GPREG0_PME_ENB; + /* Set PHY address. */ + reg &= ~GPREG0_PHY_ADDR_MASK; + reg |= sc->jme_phyaddr; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_GPREG0, reg); + + /* Configure Tx queue 0 packet completion coalescing. */ + reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK; + reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK; + reg |= PCCTX_COAL_TXQ0; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg); + + /* Configure Rx queue 0 packet completion coalescing. */ + reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK; + reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg); + + /* Disable Timer 1 and Timer 2. */ + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_TIMER1, 0); + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_TIMER2, 0); + + /* Configure retry transmit period, retry limit value. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD, + ((TXTRHD_RT_PERIOD_DEFAULT << TXTRHD_RT_PERIOD_SHIFT) & + TXTRHD_RT_PERIOD_MASK) | + ((TXTRHD_RT_LIMIT_DEFAULT << TXTRHD_RT_LIMIT_SHIFT) & + TXTRHD_RT_LIMIT_SHIFT)); + + /* Disable RSS. */ + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, + JME_RSSC, RSSC_DIS_RSS); + + /* Initialize the interrupt mask. */ + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, + JME_INTR_MASK_SET, JME_INTRS); + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, + JME_INTR_STATUS, 0xFFFFFFFF); + + /* set media, if not already handling a media change */ + if (do_ifinit) { + int error; + if ((error = mii_mediachg(&sc->jme_mii)) == ENXIO) + error = 0; + else if (error != 0) { + aprint_error_dev(sc->jme_dev, "could not set media\n"); + return error; + } + } + + /* Program MAC with resolved speed/duplex/flow-control. */ + jme_mac_config(sc); + + /* Start receiver/transmitter. */ + sc->jme_rx_cons = 0; + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR, + sc->jme_rxcsr | RXCSR_RX_ENB | RXCSR_RXQ_START); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR, + sc->jme_txcsr | TXCSR_TX_ENB); + + /* start ticks calls */ + callout_reset(&sc->jme_tick_ch, hz, jme_ticks, sc); + sc->jme_if.if_flags |= IFF_RUNNING; + sc->jme_if.if_flags &= ~IFF_OACTIVE; + splx(s); + return 0; +} + + +int +jme_mii_read(device_t self, int phy, int reg) +{ + struct jme_softc *sc = device_private(self); + int val, i; + + /* For FPGA version, PHY address 0 should be ignored. */ + if ((sc->jme_flags & JME_FLAG_FPGA) != 0) { + if (phy == 0) + return (0); + } else { + if (sc->jme_phyaddr != phy) + return (0); + } + + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_SMI, + SMI_OP_READ | SMI_OP_EXECUTE | + SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg)); + for (i = JME_PHY_TIMEOUT / 10; i > 0; i--) { + delay(10); + if (((val = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_SMI)) & SMI_OP_EXECUTE) == 0) + break; + } + + if (i == 0) { + aprint_error_dev(sc->jme_dev, "phy read timeout : %d\n", reg); + return (0); + } + + return ((val & SMI_DATA_MASK) >> SMI_DATA_SHIFT); +} + +void +jme_mii_write(device_t self, int phy, int reg, int val) +{ + struct jme_softc *sc = device_private(self); + int i; + + /* For FPGA version, PHY address 0 should be ignored. */ + if ((sc->jme_flags & JME_FLAG_FPGA) != 0) { + if (phy == 0) + return; + } else { + if (sc->jme_phyaddr != phy) + return; + } + + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_SMI, + SMI_OP_WRITE | SMI_OP_EXECUTE | + ((val << SMI_DATA_SHIFT) & SMI_DATA_MASK) | + SMI_PHY_ADDR(phy) | SMI_REG_ADDR(reg)); + for (i = JME_PHY_TIMEOUT / 10; i > 0; i--) { + delay(10); + if (((val = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_SMI)) & SMI_OP_EXECUTE) == 0) + break; + } + + if (i == 0) + aprint_error_dev(sc->jme_dev, "phy write timeout : %d\n", reg); + + return; +} + +void +jme_statchg(device_t self) +{ + jme_softc_t *sc = device_private(self); + struct ifnet *ifp = &sc->jme_if; + if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == (IFF_UP|IFF_RUNNING)) + jme_init(ifp, 0); +} + +static int +jme_intr(void *v) +{ + jme_softc_t *sc = v; + struct ifnet *ifp = &sc->jme_if; + struct mbuf *m; + uint32_t istatus; + uint32_t flags, buflen; + int i, ipackets; + struct jme_desc *desc; + +again: + istatus = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, + JME_INTR_STATUS); + + if ((istatus & JME_INTRS) == 0 || istatus == 0xFFFFFFFF) + goto done; + /* Reset PCC counter/timer and Ack interrupts. */ + istatus &= ~(INTR_TXQ_COMP | INTR_RXQ_COMP); + if ((istatus & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) != 0) + istatus |= INTR_TXQ_COAL | INTR_TXQ_COAL_TO | INTR_TXQ_COMP; + if ((istatus & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO)) != 0) + istatus |= INTR_RXQ_COAL | INTR_RXQ_COAL_TO | INTR_RXQ_COMP; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, + JME_INTR_STATUS, istatus); + + if ((sc->jme_if.if_flags & IFF_RUNNING) == 0) + goto done; +#ifdef JMEDEBUG_RX + printf("jme_intr 0x%x RXCS 0x%x RXDBA 0x%x 0x%x RXQDC 0x%x RXNDA 0x%x RXMCS 0x%x\n", istatus, + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXCSR), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_LO), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXDBA_HI), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXQDC), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXNDA), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC)); + printf("jme_intr RXUMA 0x%x 0x%x RXMCHT 0x%x 0x%x GHC 0x%x\n", + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR0), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_PAR1), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR0), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR1), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC)); +#endif + if ((istatus & (INTR_RXQ_COAL | INTR_RXQ_COAL_TO)) != 0) { + bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmap, 0, + sizeof(struct jme_desc) * JME_NBUFS, + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); +#ifdef JMEDEBUG_RX + printf("rxintr sc->jme_rx_cons %d flags 0x%x\n", + sc->jme_rx_cons, le32toh(sc->jme_rxring[sc->jme_rx_cons].flags)); +#endif + ipackets = 0; + while((le32toh(sc->jme_rxring[ sc->jme_rx_cons].flags) & + JME_RD_OWN) == 0) { + i = sc->jme_rx_cons; + desc = &sc->jme_rxring[i]; +#ifdef JMEDEBUG_RX + printf("rxintr i %d flags 0x%x buflen 0x%x\n", + i, le32toh(desc->flags), le32toh(desc->buflen)); +#endif + if ((le32toh(desc->buflen) & JME_RD_VALID) == 0) + break; + bus_dmamap_sync(sc->jme_dmatag, sc->jme_rxmbufm[i], 0, + sc->jme_rxmbufm[i]->dm_mapsize, + BUS_DMASYNC_POSTREAD); + bus_dmamap_unload(sc->jme_dmatag, sc->jme_rxmbufm[i]); + m = sc->jme_rxmbuf[i]; + buflen = le32toh(desc->buflen); + flags = le32toh(desc->flags); + if ((buflen & JME_RX_ERR_STAT) != 0) { + ifp->if_ierrors++; + /* reuse this buf */ + if (jme_add_rxbuf(sc, m, i) != 0) + aprint_error_dev(sc->jme_dev, + "can't reuse mbuf\n"); + sc->jme_rx_cons++; + sc->jme_rx_cons %= JME_NBUFS; + continue; + } + /* add a new buffer to chain */ + if (jme_add_rxbuf(sc, NULL, i) == ENOBUFS) { + /* reuse this mbuf (data is lost) */ + if (jme_add_rxbuf(sc, m, i) != 0) + aprint_error_dev(sc->jme_dev, + "can't reuse mbuf\n"); + ifp->if_ierrors++; + sc->jme_rx_cons++; + sc->jme_rx_cons %= JME_NBUFS; + continue; + } + + if (JME_RX_BYTES(buflen) < sizeof(struct ether_header)){ + sc->jme_rx_cons++; + sc->jme_rx_cons %= JME_NBUFS; + m_freem(m); + continue; + } + m->m_pkthdr.rcvif = ifp; + m->m_pkthdr.len = m->m_len = + JME_RX_BYTES(buflen) - JME_RX_PAD_BYTES; + m->m_data = m->m_ext.ext_buf + JME_RX_PAD_BYTES; + ifp->if_ipackets++; + ipackets++; + JME_DESC_INC(sc->jme_rx_cons, JME_NBUFS); +#if NBPFILTER > 0 + if (ifp->if_bpf) + bpf_mtap(ifp->if_bpf, m); +#endif /* NBPFILTER > 0 */ + + if ((ifp->if_capenable & IFCAP_CSUM_IPv4_Rx) && + (flags & JME_RD_IPV4)) { + m->m_pkthdr.csum_flags |= M_CSUM_IPv4; + if (!(flags & JME_RD_IPCSUM)) + m->m_pkthdr.csum_flags |= + M_CSUM_IPv4_BAD; + } + if ((ifp->if_capenable & IFCAP_CSUM_TCPv4_Rx) && + (flags & JME_RD_TCPV4) == JME_RD_TCPV4) { + m->m_pkthdr.csum_flags |= M_CSUM_TCPv4; + if (!(flags & JME_RD_TCPCSUM)) + m->m_pkthdr.csum_flags |= + M_CSUM_TCP_UDP_BAD; + } + if ((ifp->if_capenable & IFCAP_CSUM_UDPv4_Rx) && + (flags & JME_RD_UDPV4) == JME_RD_UDPV4) { + m->m_pkthdr.csum_flags |= M_CSUM_UDPv4; + if (!(flags & JME_RD_UDPCSUM)) + m->m_pkthdr.csum_flags |= + M_CSUM_TCP_UDP_BAD; + } + if ((ifp->if_capenable & IFCAP_CSUM_TCPv6_Rx) && + (flags & JME_RD_TCPV6) == JME_RD_TCPV6) { + m->m_pkthdr.csum_flags |= M_CSUM_TCPv6; + if (!(flags & JME_RD_TCPCSUM)) + m->m_pkthdr.csum_flags |= + M_CSUM_TCP_UDP_BAD; + } + if ((ifp->if_capenable & IFCAP_CSUM_UDPv6_Rx) && + (flags & JME_RD_UDPV6) == JME_RD_UDPV6) { + m->m_pkthdr.csum_flags |= M_CSUM_UDPv6; + if (!(flags & JME_RD_UDPCSUM)) + m->m_pkthdr.csum_flags |= + M_CSUM_TCP_UDP_BAD; + } + if (flags & JME_RD_VLAN_TAG) { + /* pass to vlan_input() */ + VLAN_INPUT_TAG(ifp, m, + (flags & JME_RD_VLAN_MASK), continue); + } + (*ifp->if_input)(ifp, m); + } +#if NRND > 0 + if (ipackets && RND_ENABLED(&sc->rnd_source)) + rnd_add_uint32(&sc->rnd_source, ipackets); +#endif /* NRND > 0 */ + + } + if ((istatus & INTR_RXQ_DESC_EMPTY) != 0) { + /* + * Notify hardware availability of new Rx + * buffers. + * Reading RXCSR takes very long time under + * heavy load so cache RXCSR value and writes + * the ORed value with the kick command to + * the RXCSR. This saves one register access + * cycle. + */ + sc->jme_rx_cons = 0; + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_RXCSR, + sc->jme_rxcsr | RXCSR_RX_ENB | RXCSR_RXQ_START); + } + if ((istatus & (INTR_TXQ_COAL | INTR_TXQ_COAL_TO)) != 0) { + jme_ifstart(&sc->jme_if); + } + + goto again; + +done: + return 1; +} + + +static int +jme_ifioctl(struct ifnet *ifp, unsigned long cmd, void *data) +{ + struct jme_softc *sc = ifp->if_softc; + int s, error; + + s = splnet(); + error = ether_ioctl(ifp, cmd, data); + if (error == ENETRESET && (ifp->if_flags & IFF_RUNNING)) { + if (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI) { + jme_set_filter(sc); + error = 0; + } else { + error = jme_init(ifp, 0); + } + } + splx(s); + return error; +} + +static int +jme_encap(struct jme_softc *sc, struct mbuf **m_head) +{ + struct jme_desc *txd; + struct jme_desc *desc; + struct mbuf *m; + struct m_tag *mtag; + int error, i, prod, headdsc, nsegs; + uint32_t cflags, tso_segsz; + + if (((*m_head)->m_pkthdr.csum_flags & (M_CSUM_TSOv4|M_CSUM_TSOv6)) != 0){ + /* + * Due to the adherence to NDIS specification JMC250 + * assumes upper stack computed TCP pseudo checksum + * without including payload length. This breaks + * checksum offload for TSO case so recompute TCP + * pseudo checksum for JMC250. Hopefully this wouldn't + * be much burden on modern CPUs. + */ + bool v4 = ((*m_head)->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0; + int iphl = v4 ? + M_CSUM_DATA_IPv4_IPHL((*m_head)->m_pkthdr.csum_data) : + M_CSUM_DATA_IPv6_HL((*m_head)->m_pkthdr.csum_data); + /* + * note: we support vlan offloading, so we should never have + * a ETHERTYPE_VLAN packet here - so ETHER_HDR_LEN is always + * right. + */ + int hlen = ETHER_HDR_LEN + iphl; + + if (__predict_false((*m_head)->m_len < + (hlen + sizeof(struct tcphdr)))) { + /* + * TCP/IP headers are not in the first mbuf; we need + * to do this the slow and painful way. Let's just + * hope this doesn't happen very often. + */ + struct tcphdr th; + + m_copydata((*m_head), hlen, sizeof(th), &th); + if (v4) { + struct ip ip; + + m_copydata((*m_head), ETHER_HDR_LEN, + sizeof(ip), &ip); + ip.ip_len = 0; + m_copyback((*m_head), + ETHER_HDR_LEN + offsetof(struct ip, ip_len), + sizeof(ip.ip_len), &ip.ip_len); + th.th_sum = in_cksum_phdr(ip.ip_src.s_addr, + ip.ip_dst.s_addr, htons(IPPROTO_TCP)); + } else { + struct ip6_hdr ip6; + + m_copydata((*m_head), ETHER_HDR_LEN, + sizeof(ip6), &ip6); + ip6.ip6_plen = 0; + m_copyback((*m_head), ETHER_HDR_LEN + + offsetof(struct ip6_hdr, ip6_plen), + sizeof(ip6.ip6_plen), &ip6.ip6_plen); + th.th_sum = in6_cksum_phdr(&ip6.ip6_src, + &ip6.ip6_dst, 0, htonl(IPPROTO_TCP)); + } + m_copyback((*m_head), + hlen + offsetof(struct tcphdr, th_sum), + sizeof(th.th_sum), &th.th_sum); + + hlen += th.th_off << 2; + } else { + /* + * TCP/IP headers are in the first mbuf; we can do + * this the easy way. + */ + struct tcphdr *th; + + if (v4) { + struct ip *ip = + (void *)(mtod((*m_head), char *) + + ETHER_HDR_LEN); + th = (void *)(mtod((*m_head), char *) + hlen); + + ip->ip_len = 0; + th->th_sum = in_cksum_phdr(ip->ip_src.s_addr, + ip->ip_dst.s_addr, htons(IPPROTO_TCP)); + } else { + struct ip6_hdr *ip6 = + (void *)(mtod((*m_head), char *) + + ETHER_HDR_LEN); + th = (void *)(mtod((*m_head), char *) + hlen); + + ip6->ip6_plen = 0; + th->th_sum = in6_cksum_phdr(&ip6->ip6_src, + &ip6->ip6_dst, 0, htonl(IPPROTO_TCP)); + } + hlen += th->th_off << 2; + } + + } + + prod = sc->jme_tx_prod; + txd = &sc->jme_txring[prod]; + + error = bus_dmamap_load_mbuf(sc->jme_dmatag, sc->jme_txmbufm[prod], + *m_head, BUS_DMA_WRITE); + if (error) { + if (error == EFBIG) { + log(LOG_ERR, "%s: Tx packet consumes too many " + "DMA segments, dropping...\n", + device_xname(sc->jme_dev)); + m_freem(*m_head); + m_head = NULL; + } + return (error); + } + /* + * Check descriptor overrun. Leave one free descriptor. + * Since we always use 64bit address mode for transmitting, + * each Tx request requires one more dummy descriptor. + */ + nsegs = sc->jme_txmbufm[prod]->dm_nsegs; +#ifdef JMEDEBUG_TX + printf("jme_encap prod %d nsegs %d jme_tx_cnt %d\n", prod, nsegs, sc->jme_tx_cnt); +#endif + if (sc->jme_tx_cnt + nsegs + 1 > JME_NBUFS - 1) { + bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[prod]); + return (ENOBUFS); + } + bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmbufm[prod], + 0, sc->jme_txmbufm[prod]->dm_mapsize, BUS_DMASYNC_PREWRITE); + + m = *m_head; + cflags = 0; + tso_segsz = 0; + /* Configure checksum offload and TSO. */ + if ((m->m_pkthdr.csum_flags & (M_CSUM_TSOv4|M_CSUM_TSOv6)) != 0) { + tso_segsz = (uint32_t)m->m_pkthdr.segsz << JME_TD_MSS_SHIFT; + cflags |= JME_TD_TSO; + } else { + if ((m->m_pkthdr.csum_flags & M_CSUM_IPv4) != 0) + cflags |= JME_TD_IPCSUM; + if ((m->m_pkthdr.csum_flags & (M_CSUM_TCPv4|M_CSUM_TCPv6)) != 0) + cflags |= JME_TD_TCPCSUM; + if ((m->m_pkthdr.csum_flags & (M_CSUM_UDPv4|M_CSUM_UDPv6)) != 0) + cflags |= JME_TD_UDPCSUM; + } + /* Configure VLAN. */ + if ((mtag = VLAN_OUTPUT_TAG(&sc->jme_ec, m)) != NULL) { + cflags |= (VLAN_TAG_VALUE(mtag) & JME_TD_VLAN_MASK); + cflags |= JME_TD_VLAN_TAG; + } + + desc = &sc->jme_txring[prod]; + desc->flags = htole32(cflags); + desc->buflen = htole32(tso_segsz); + desc->addr_hi = htole32(m->m_pkthdr.len); + desc->addr_lo = 0; + headdsc = prod; + sc->jme_tx_cnt++; + JME_DESC_INC(prod, JME_NBUFS); + for (i = 0; i < nsegs; i++) { + desc = &sc->jme_txring[prod]; + desc->flags = htole32(JME_TD_OWN | JME_TD_64BIT); + desc->buflen = + htole32(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_len); + desc->addr_hi = htole32( + JME_ADDR_HI(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_addr)); + desc->addr_lo = htole32( + JME_ADDR_LO(sc->jme_txmbufm[headdsc]->dm_segs[i].ds_addr)); + bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, + prod * sizeof(struct jme_desc), sizeof(struct jme_desc), + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); + sc->jme_txmbuf[prod] = NULL; + sc->jme_tx_cnt++; + JME_DESC_INC(prod, JME_NBUFS); + } + + /* Update producer index. */ + sc->jme_tx_prod = prod; +#ifdef JMEDEBUG_TX + printf("jme_encap prod now %d\n", sc->jme_tx_prod); +#endif + /* + * Finally request interrupt and give the first descriptor + * owenership to hardware. + */ + desc = &sc->jme_txring[headdsc]; + desc->flags |= htole32(JME_TD_OWN | JME_TD_INTR); + bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, + headdsc * sizeof(struct jme_desc), sizeof(struct jme_desc), + BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE); + + sc->jme_txmbuf[headdsc] = m; + return (0); +} + +static void +jme_txeof(struct jme_softc *sc) +{ + struct ifnet *ifp; + struct jme_desc *desc; + uint32_t status; + int cons, cons0, nsegs, seg; + + ifp = &sc->jme_if; + +#ifdef JMEDEBUG_TX + printf("jme_txeof cons %d prod %d\n", + sc->jme_tx_cons, sc->jme_tx_prod); + printf("jme_txeof JME_TXCSR 0x%x JME_TXDBA_LO 0x%x JME_TXDBA_HI 0x%x " + "JME_TXQDC 0x%x JME_TXNDA 0x%x JME_TXMAC 0x%x JME_TXPFC 0x%x " + "JME_TXTRHD 0x%x\n", + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXQDC), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXNDA), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD)); + for (cons = sc->jme_tx_cons; cons != sc->jme_tx_prod; ) { + desc = &sc->jme_txring[cons]; + printf("ring[%d] 0x%x 0x%x 0x%x 0x%x\n", cons, + desc->flags, desc->buflen, desc->addr_hi, desc->addr_lo); + JME_DESC_INC(cons, JME_NBUFS); + } +#endif + + cons = sc->jme_tx_cons; + if (cons == sc->jme_tx_prod) + return; + + /* + * Go through our Tx list and free mbufs for those + * frames which have been transmitted. + */ + for (; cons != sc->jme_tx_prod;) { + bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, + cons * sizeof(struct jme_desc), sizeof(struct jme_desc), + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); + + desc = &sc->jme_txring[cons]; + status = le32toh(desc->flags); +#ifdef JMEDEBUG_TX + printf("jme_txeof %i status 0x%x nsegs %d\n", cons, status, + sc->jme_txmbufm[cons]->dm_nsegs); +#endif + if (status & JME_TD_OWN) + break; + + if ((status & (JME_TD_TMOUT | JME_TD_RETRY_EXP)) != 0) + ifp->if_oerrors++; + else { + ifp->if_opackets++; + if ((status & JME_TD_COLLISION) != 0) + ifp->if_collisions += + le32toh(desc->buflen) & + JME_TD_BUF_LEN_MASK; + } + /* + * Only the first descriptor of multi-descriptor + * transmission is updated so driver have to skip entire + * chained buffers for the transmiited frame. In other + * words, JME_TD_OWN bit is valid only at the first + * descriptor of a multi-descriptor transmission. + */ + nsegs = sc->jme_txmbufm[cons]->dm_nsegs; + cons0 = cons; + JME_DESC_INC(cons, JME_NBUFS); + for (seg = 1; seg < nsegs + 1; seg++) { + bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmap, + cons * sizeof(struct jme_desc), + sizeof(struct jme_desc), + BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); + sc->jme_txring[cons].flags = 0; + JME_DESC_INC(cons, JME_NBUFS); + } + /* Reclaim transferred mbufs. */ + bus_dmamap_sync(sc->jme_dmatag, sc->jme_txmbufm[cons0], + 0, sc->jme_txmbufm[cons0]->dm_mapsize, + BUS_DMASYNC_POSTWRITE); + bus_dmamap_unload(sc->jme_dmatag, sc->jme_txmbufm[cons0]); + + KASSERT(sc->jme_txmbuf[cons0] != NULL); + m_freem(sc->jme_txmbuf[cons0]); + sc->jme_txmbuf[cons0] = NULL; + sc->jme_tx_cnt -= nsegs + 1; + KASSERT(sc->jme_tx_cnt >= 0); + sc->jme_if.if_flags &= ~IFF_OACTIVE; + } + sc->jme_tx_cons = cons; + /* Unarm watchog timer when there is no pending descriptors in queue. */ + if (sc->jme_tx_cnt == 0) + ifp->if_timer = 0; +#ifdef JMEDEBUG_TX + printf("jme_txeof jme_tx_cnt %d\n", sc->jme_tx_cnt); +#endif +} + +static void +jme_ifstart(struct ifnet *ifp) +{ + jme_softc_t *sc = ifp->if_softc; + struct mbuf *mb_head; + int enq; + + /* check if we can free some desc */ + jme_txeof(sc); + + if ((sc->jme_if.if_flags & (IFF_RUNNING|IFF_OACTIVE)) != IFF_RUNNING) + return; + for (enq = 0;; enq++) { +nexttx: + /* Grab a paquet for output */ + IFQ_DEQUEUE(&ifp->if_snd, mb_head); + if (mb_head == NULL) { +#ifdef JMEDEBUG_TX + printf("%s: nothing to send\n", __func__); +#endif + break; + } + /* try to add this mbuf to the TX ring */ + if (jme_encap(sc, &mb_head)) { + if (mb_head == NULL) { + ifp->if_oerrors++; + /* packet dropped, try next one */ + goto nexttx; + } + /* resource shortage, try again later */ + IF_PREPEND(&ifp->if_snd, mb_head); + ifp->if_flags |= IFF_OACTIVE; + break; + } +#if NBPFILTER > 0 + /* Pass packet to bpf if there is a listener */ + if (ifp->if_bpf) + bpf_mtap(ifp->if_bpf, mb_head); +#endif + } +#ifdef JMEDEBUG_TX + printf("jme_ifstart enq %d\n", enq); +#endif + if (enq) { + /* + * Set a 5 second timer just in case we don't hear from + * the card again. + */ + ifp->if_timer = 5; + /* + * Reading TXCSR takes very long time under heavy load + * so cache TXCSR value and writes the ORed value with + * the kick command to the TXCSR. This saves one register + * access cycle. + */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR, + sc->jme_txcsr | TXCSR_TX_ENB | TXCSR_TXQ_N_START(TXCSR_TXQ0)); +#ifdef JMEDEBUG_TX + printf("jme_ifstart JME_TXCSR 0x%x JME_TXDBA_LO 0x%x JME_TXDBA_HI 0x%x " + "JME_TXQDC 0x%x JME_TXNDA 0x%x JME_TXMAC 0x%x JME_TXPFC 0x%x " + "JME_TXTRHD 0x%x\n", + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXCSR), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_LO), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXDBA_HI), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXQDC), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXNDA), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC), + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD)); +#endif + } +} + +static void +jme_ifwatchdog(struct ifnet *ifp) +{ + jme_softc_t *sc = ifp->if_softc; + + if ((ifp->if_flags & IFF_RUNNING) == 0) + return; + printf("%s: device timeout\n", device_xname(sc->jme_dev)); + ifp->if_oerrors++; + jme_init(ifp, 0); +} + +static int +jme_mediachange(struct ifnet *ifp) +{ + int error; + jme_softc_t *sc = ifp->if_softc; + + if ((error = mii_mediachg(&sc->jme_mii)) == ENXIO) + error = 0; + else if (error != 0) { + aprint_error_dev(sc->jme_dev, "could not set media\n"); + return error; + } + return 0; +} + +static void +jme_ticks(void *v) +{ + jme_softc_t *sc = v; + int s = splnet(); + + /* Tick the MII. */ + mii_tick(&sc->jme_mii); + + /* every seconds */ + callout_reset(&sc->jme_tick_ch, hz, jme_ticks, sc); + splx(s); +} + +static void +jme_mac_config(jme_softc_t *sc) +{ + uint32_t ghc, gpreg, rxmac, txmac, txpause; + struct mii_data *mii = &sc->jme_mii; + + ghc = 0; + rxmac = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC); + rxmac &= ~RXMAC_FC_ENB; + txmac = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC); + txmac &= ~(TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST); + txpause = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC); + txpause &= ~TXPFC_PAUSE_ENB; + + if (mii->mii_media_active & IFM_FDX) { + ghc |= GHC_FULL_DUPLEX; + rxmac &= ~RXMAC_COLL_DET_ENB; + txmac &= ~(TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | + TXMAC_BACKOFF | TXMAC_CARRIER_EXT | + TXMAC_FRAME_BURST); + /* Disable retry transmit timer/retry limit. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD, + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD) + & ~(TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB)); + } else { + rxmac |= RXMAC_COLL_DET_ENB; + txmac |= TXMAC_COLL_ENB | TXMAC_CARRIER_SENSE | TXMAC_BACKOFF; + /* Enable retry transmit timer/retry limit. */ + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD, + bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXTRHD) | TXTRHD_RT_PERIOD_ENB | TXTRHD_RT_LIMIT_ENB); + } + /* Reprogram Tx/Rx MACs with resolved speed/duplex. */ + switch (IFM_SUBTYPE(mii->mii_media_active)) { + case IFM_10_T: + ghc |= GHC_SPEED_10; + break; + case IFM_100_TX: + ghc |= GHC_SPEED_100; + break; + case IFM_1000_T: + ghc |= GHC_SPEED_1000; + if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0) + txmac |= TXMAC_CARRIER_EXT | TXMAC_FRAME_BURST; + break; + default: + break; + } + if ((sc->jme_flags & JME_FLAG_GIGA) && + sc->jme_chip_rev == DEVICEREVID_JMC250_A2) { + /* + * Workaround occasional packet loss issue of JMC250 A2 + * when it runs on half-duplex media. + */ +#ifdef JMEDEBUG + printf("JME250 A2 workaround\n"); +#endif + gpreg = bus_space_read_4(sc->jme_bt_misc, sc->jme_bh_misc, + JME_GPREG1); + if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) + gpreg &= ~GPREG1_HDPX_FIX; + else + gpreg |= GPREG1_HDPX_FIX; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, + JME_GPREG1, gpreg); + /* Workaround CRC errors at 100Mbps on JMC250 A2. */ + if (IFM_SUBTYPE(mii->mii_media_active) == IFM_100_TX) { + /* Extend interface FIFO depth. */ + jme_mii_write(sc->jme_dev, sc->jme_phyaddr, + 0x1B, 0x0000); + } else { + /* Select default interface FIFO depth. */ + jme_mii_write(sc->jme_dev, sc->jme_phyaddr, + 0x1B, 0x0004); + } + } + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_GHC, ghc); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, rxmac); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXMAC, txmac); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_TXPFC, txpause); +} + +static void +jme_set_filter(jme_softc_t *sc) +{ + struct ifnet *ifp = &sc->jme_if; + struct ether_multistep step; + struct ether_multi *enm; + uint32_t hash[2] = {0, 0}; + int i; + uint32_t rxcfg; + + rxcfg = bus_space_read_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC); + rxcfg &= ~ (RXMAC_BROADCAST | RXMAC_PROMISC | RXMAC_MULTICAST | + RXMAC_ALLMULTI); + /* Always accept frames destined to our station address. */ + rxcfg |= RXMAC_UNICAST; + if ((ifp->if_flags & IFF_BROADCAST) != 0) + rxcfg |= RXMAC_BROADCAST; + if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) { + if ((ifp->if_flags & IFF_PROMISC) != 0) + rxcfg |= RXMAC_PROMISC; + if ((ifp->if_flags & IFF_ALLMULTI) != 0) + rxcfg |= RXMAC_ALLMULTI; + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_MAR0, 0xFFFFFFFF); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_MAR1, 0xFFFFFFFF); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, + JME_RXMAC, rxcfg); + return; + } + /* + * Set up the multicast address filter by passing all multicast + * addresses through a CRC generator, and then using the low-order + * 6 bits as an index into the 64 bit multicast hash table. The + * high order bits select the register, while the rest of the bits + * select the bit within the register. + */ + rxcfg |= RXMAC_MULTICAST; + bzero(hash, sizeof(hash)); + + ETHER_FIRST_MULTI(step, &sc->jme_ec, enm); + while (enm != NULL) { +#ifdef JEMDBUG + printf("%s: addrs %s %s\n", __func__, + ether_sprintf(enm->enm_addrlo), + ether_sprintf(enm->enm_addrhi)); +#endif + if (memcmp(enm->enm_addrlo, enm->enm_addrhi, 6) == 0) { + i = ether_crc32_be(enm->enm_addrlo, 6); + /* Just want the 6 least significant bits. */ + i &= 0x3f; + hash[i / 32] |= 1 << (i%32); + } else { + hash[0] = hash[1] = 0xffffffff; + sc->jme_if.if_flags |= IFF_ALLMULTI; + break; + } + ETHER_NEXT_MULTI(step, enm); + } +#ifdef JMEDEBUG + printf("%s: hash1 %x has2 %x\n", __func__, hash[0], hash[1]); +#endif + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR0, hash[0]); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_MAR1, hash[1]); + bus_space_write_4(sc->jme_bt_mac, sc->jme_bh_mac, JME_RXMAC, rxcfg); +} + +#if 0 +static int +jme_multicast_hash(uint8_t *a) +{ + int hash; + +#define DA(addr,bit) (addr[5 - (bit / 8)] & (1 << (bit % 8))) +#define xor8(a,b,c,d,e,f,g,h) \ + (((a != 0) + (b != 0) + (c != 0) + (d != 0) + \ + (e != 0) + (f != 0) + (g != 0) + (h != 0)) & 1) + + hash = xor8(DA(a,0), DA(a, 6), DA(a,12), DA(a,18), DA(a,24), DA(a,30), + DA(a,36), DA(a,42)); + hash |= xor8(DA(a,1), DA(a, 7), DA(a,13), DA(a,19), DA(a,25), DA(a,31), + DA(a,37), DA(a,43)) << 1; + hash |= xor8(DA(a,2), DA(a, 8), DA(a,14), DA(a,20), DA(a,26), DA(a,32), + DA(a,38), DA(a,44)) << 2; + hash |= xor8(DA(a,3), DA(a, 9), DA(a,15), DA(a,21), DA(a,27), DA(a,33), + DA(a,39), DA(a,45)) << 3; + hash |= xor8(DA(a,4), DA(a,10), DA(a,16), DA(a,22), DA(a,28), DA(a,34), + DA(a,40), DA(a,46)) << 4; + hash |= xor8(DA(a,5), DA(a,11), DA(a,17), DA(a,23), DA(a,29), DA(a,35), + DA(a,41), DA(a,47)) << 5; + + return hash; +} +#endif + +static int +jme_eeprom_read_byte(struct jme_softc *sc, uint8_t addr, uint8_t *val) +{ + uint32_t reg; + int i; + + *val = 0; + for (i = JME_EEPROM_TIMEOUT / 10; i > 0; i--) { + reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy, + JME_SMBCSR); + if ((reg & SMBCSR_HW_BUSY_MASK) == SMBCSR_HW_IDLE) + break; + delay(10); + } + + if (i == 0) { + aprint_error_dev(sc->jme_dev, "EEPROM idle timeout!\n"); + return (ETIMEDOUT); + } + + reg = ((uint32_t)addr << SMBINTF_ADDR_SHIFT) & SMBINTF_ADDR_MASK; + bus_space_write_4(sc->jme_bt_phy, sc->jme_bh_phy, + JME_SMBINTF, reg | SMBINTF_RD | SMBINTF_CMD_TRIGGER); + for (i = JME_EEPROM_TIMEOUT / 10; i > 0; i--) { + delay(10); + reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy, + JME_SMBINTF); + if ((reg & SMBINTF_CMD_TRIGGER) == 0) + break; + } + + if (i == 0) { + aprint_error_dev(sc->jme_dev, "EEPROM read timeout!\n"); + return (ETIMEDOUT); + } + + reg = bus_space_read_4(sc->jme_bt_phy, sc->jme_bh_phy, JME_SMBINTF); + *val = (reg & SMBINTF_RD_DATA_MASK) >> SMBINTF_RD_DATA_SHIFT; + + return (0); +} + + +static int +jme_eeprom_macaddr(struct jme_softc *sc) +{ + uint8_t eaddr[ETHER_ADDR_LEN]; + uint8_t fup, reg, val; + uint32_t offset; + int match; + + offset = 0; + if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 || + fup != JME_EEPROM_SIG0) + return (ENOENT); + if (jme_eeprom_read_byte(sc, offset++, &fup) != 0 || + fup != JME_EEPROM_SIG1) + return (ENOENT); + match = 0; + do { + if (jme_eeprom_read_byte(sc, offset, &fup) != 0) + break; + /* Check for the end of EEPROM descriptor. */ + if ((fup & JME_EEPROM_DESC_END) == JME_EEPROM_DESC_END) + break; + if ((uint8_t)JME_EEPROM_MKDESC(JME_EEPROM_FUNC0, + JME_EEPROM_PAGE_BAR1) == fup) { + if (jme_eeprom_read_byte(sc, offset + 1, ®) != 0) + break; + if (reg >= JME_PAR0 && + reg < JME_PAR0 + ETHER_ADDR_LEN) { + if (jme_eeprom_read_byte(sc, offset + 2, + &val) != 0) + break; + eaddr[reg - JME_PAR0] = val; + match++; + } + } + /* Try next eeprom descriptor. */ + offset += JME_EEPROM_DESC_BYTES; + } while (match != ETHER_ADDR_LEN && offset < JME_EEPROM_END); + + if (match == ETHER_ADDR_LEN) { + bcopy(eaddr, sc->jme_enaddr, ETHER_ADDR_LEN); + return (0); + } + + return (ENOENT); +} + +/* + * Set up sysctl(3) MIB, hw.jme.* - Individual controllers will be + * set up in jme_pci_attach() + */ +SYSCTL_SETUP(sysctl_jme, "sysctl jme subtree setup") +{ + int rc; + const struct sysctlnode *node; + + if ((rc = sysctl_createv(clog, 0, NULL, NULL, + 0, CTLTYPE_NODE, "hw", NULL, + NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) { + goto err; + } + + if ((rc = sysctl_createv(clog, 0, NULL, &node, + 0, CTLTYPE_NODE, "jme", + SYSCTL_DESCR("jme interface controls"), + NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL)) != 0) { + goto err; + } + + jme_root_num = node->sysctl_num; + return; + +err: + aprint_error("%s: syctl_createv failed (rc = %d)\n", __func__, rc); +} + +static int +jme_sysctl_intrxto(SYSCTLFN_ARGS) +{ + int error, t; + struct sysctlnode node; + struct jme_softc *sc; + uint32_t reg; + + node = *rnode; + sc = node.sysctl_data; + t = sc->jme_intrxto; + node.sysctl_data = &t; + error = sysctl_lookup(SYSCTLFN_CALL(&node)); + if (error || newp == NULL) + return error; + + if (t < PCCRX_COAL_TO_MIN || t > PCCRX_COAL_TO_MAX) + return EINVAL; + + /* + * update the softc with sysctl-changed value, and mark + * for hardware update + */ + sc->jme_intrxto = t; + /* Configure Rx queue 0 packet completion coalescing. */ + reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK; + reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg); + return 0; +} + +static int +jme_sysctl_intrxct(SYSCTLFN_ARGS) +{ + int error, t; + struct sysctlnode node; + struct jme_softc *sc; + uint32_t reg; + + node = *rnode; + sc = node.sysctl_data; + t = sc->jme_intrxct; + node.sysctl_data = &t; + error = sysctl_lookup(SYSCTLFN_CALL(&node)); + if (error || newp == NULL) + return error; + + if (t < PCCRX_COAL_PKT_MIN || t > PCCRX_COAL_PKT_MAX) + return EINVAL; + + /* + * update the softc with sysctl-changed value, and mark + * for hardware update + */ + sc->jme_intrxct = t; + /* Configure Rx queue 0 packet completion coalescing. */ + reg = (sc->jme_intrxto << PCCRX_COAL_TO_SHIFT) & PCCRX_COAL_TO_MASK; + reg |= (sc->jme_intrxct << PCCRX_COAL_PKT_SHIFT) & PCCRX_COAL_PKT_MASK; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCRX0, reg); + return 0; +} + +static int +jme_sysctl_inttxto(SYSCTLFN_ARGS) +{ + int error, t; + struct sysctlnode node; + struct jme_softc *sc; + uint32_t reg; + + node = *rnode; + sc = node.sysctl_data; + t = sc->jme_inttxto; + node.sysctl_data = &t; + error = sysctl_lookup(SYSCTLFN_CALL(&node)); + if (error || newp == NULL) + return error; + + if (t < PCCTX_COAL_TO_MIN || t > PCCTX_COAL_TO_MAX) + return EINVAL; + + /* + * update the softc with sysctl-changed value, and mark + * for hardware update + */ + sc->jme_inttxto = t; + /* Configure Tx queue 0 packet completion coalescing. */ + reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK; + reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK; + reg |= PCCTX_COAL_TXQ0; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg); + return 0; +} + +static int +jme_sysctl_inttxct(SYSCTLFN_ARGS) +{ + int error, t; + struct sysctlnode node; + struct jme_softc *sc; + uint32_t reg; + + node = *rnode; + sc = node.sysctl_data; + t = sc->jme_inttxct; + node.sysctl_data = &t; + error = sysctl_lookup(SYSCTLFN_CALL(&node)); + if (error || newp == NULL) + return error; + + if (t < PCCTX_COAL_PKT_MIN || t > PCCTX_COAL_PKT_MAX) + return EINVAL; + + /* + * update the softc with sysctl-changed value, and mark + * for hardware update + */ + sc->jme_inttxct = t; + /* Configure Tx queue 0 packet completion coalescing. */ + reg = (sc->jme_inttxto << PCCTX_COAL_TO_SHIFT) & PCCTX_COAL_TO_MASK; + reg |= (sc->jme_inttxct << PCCTX_COAL_PKT_SHIFT) & PCCTX_COAL_PKT_MASK; + reg |= PCCTX_COAL_TXQ0; + bus_space_write_4(sc->jme_bt_misc, sc->jme_bh_misc, JME_PCCTX, reg); + return 0; +} diff --git a/sys/dev/pci/if_jmereg.h b/sys/dev/pci/if_jmereg.h new file mode 100644 index 00000000000..0fa9cceb502 --- /dev/null +++ b/sys/dev/pci/if_jmereg.h @@ -0,0 +1,1043 @@ +/*- + * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org> + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice unmodified, this list of conditions, and the following + * disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD: src/sys/dev/jme/if_jmereg.h,v 1.3 2008/09/22 06:17:21 yongari Exp $ + */ + +#ifndef _IF_JMEREG_H +#define _IF_JMEREG_H + +/* + * JMC250 PCI revisions + */ +#define DEVICEREVID_JMC250_A0 0x00 +#define DEVICEREVID_JMC250_A2 0x11 + +/* + * JMC260 PCI revisions + */ +#define DEVICEREVID_JMC260_A0 0x00 + +/* JMC250 PCI configuration register. */ +#define JME_PCI_BAR0 0x10 /* 16KB memory window. */ + +#define JME_PCI_BAR1 0x18 /* 128bytes I/O window. */ + +#define JME_PCI_BAR2 0x1C /* 256bytes I/O window. */ + +#define JME_PCI_BAR3 0x20 /* 64KB memory window. */ + +#define JME_PCI_EROM 0x30 + +#define JME_PCI_DBG 0x9C + +#define JME_PCI_SPI 0xB0 + +#define SPI_ENB 0x00000010 +#define SPI_SO_STATUS 0x00000008 +#define SPI_SI_CTRL 0x00000004 +#define SPI_SCK_CTRL 0x00000002 +#define SPI_CS_N_CTRL 0x00000001 + +#define JME_PCI_PHYCFG0 0xC0 + +#define JME_PCI_PHYCFG1 0xC4 + +#define JME_PCI_PHYCFG2 0xC8 + +#define JME_PCI_PHYCFG3 0xCC + +#define JME_PCI_PIPECTL1 0xD0 + +#define JME_PCI_PIPECTL2 0xD4 + +/* PCIe link error/status. */ +#define JME_PCI_LES 0xD8 + +/* propeietary register 0. */ +#define JME_PCI_PE0 0xE0 +#define PE0_SPI_EXIST 0x00200000 +#define PE0_PME_D0 0x00100000 +#define PE0_PME_D3H 0x00080000 +#define PE0_PME_SPI_PAD 0x00040000 +#define PE0_MASK_ASPM 0x00020000 +#define PE0_EEPROM_RW_DIS 0x00008000 +#define PE0_PCI_INTA 0x00001000 +#define PE0_PCI_INTB 0x00002000 +#define PE0_PCI_INTC 0x00003000 +#define PE0_PCI_INTD 0x00004000 +#define PE0_PCI_SVSSID_WR_ENB 0x00000800 +#define PE0_MSIX_SIZE_8 0x00000700 +#define PE0_MSIX_SIZE_7 0x00000600 +#define PE0_MSIX_SIZE_6 0x00000500 +#define PE0_MSIX_SIZE_5 0x00000400 +#define PE0_MSIX_SIZE_4 0x00000300 +#define PE0_MSIX_SIZE_3 0x00000200 +#define PE0_MSIX_SIZE_2 0x00000100 +#define PE0_MSIX_SIZE_1 0x00000000 +#define PE0_MSIX_SIZE_DEF 0x00000700 +#define PE0_MSIX_CAP_DIS 0x00000080 +#define PE0_MSI_PVMC_ENB 0x00000040 +#define PE0_LCAP_EXIT_LAT_MASK 0x00000038 +#define PE0_LCAP_EXIT_LAT_DEF 0x00000038 +#define PE0_PM_AUXC_MASK 0x00000007 +#define PE0_PM_AUXC_DEF 0x00000007 + +#define JME_PCI_PE1 0xE4 + +#define JME_PCI_PHYTEST 0xF8 + +#define JME_PCI_GPR 0xFC + +/* + * JMC Register Map. + * ----------------------------------------------------------------------- + * Register Size IO space Memory space + * ----------------------------------------------------------------------- + * Tx/Rx MAC registers 128 bytes BAR1 + 0x00 ~ BAR0 + 0x00 ~ + * BAR1 + 0x7F BAR0 + 0x7F + * ----------------------------------------------------------------------- + * PHY registers 128 bytes BAR2 + 0x00 ~ BAR0 + 0x400 ~ + * BAR2 + 0x7F BAR0 + 0x47F + * ----------------------------------------------------------------------- + * Misc registers 128 bytes BAR2 + 0x80 ~ BAR0 + 0x800 ~ + * BAR2 + 0xfF BAR0 + 0x87F + * ----------------------------------------------------------------------- + * We use bus_space_subregion() to get handle for the 3 different + * register space. Register address are relative to the base of each + * region. + */ + +/* Tx control and status. */ +#define JME_TXCSR 0x0000 +#define TXCSR_QWEIGHT_MASK 0x0F000000 +#define TXCSR_QWEIGHT_SHIFT 24 +#define TXCSR_TXQ_SEL_MASK 0x00070000 +#define TXCSR_TXQ_SEL_SHIFT 16 +#define TXCSR_TXQ_START 0x00000001 +#define TXCSR_TXQ_START_SHIFT 8 +#define TXCSR_FIFO_THRESH_4QW 0x00000000 +#define TXCSR_FIFO_THRESH_8QW 0x00000040 +#define TXCSR_FIFO_THRESH_12QW 0x00000080 +#define TXCSR_FIFO_THRESH_16QW 0x000000C0 +#define TXCSR_DMA_SIZE_64 0x00000000 +#define TXCSR_DMA_SIZE_128 0x00000010 +#define TXCSR_DMA_SIZE_256 0x00000020 +#define TXCSR_DMA_SIZE_512 0x00000030 +#define TXCSR_DMA_BURST 0x00000004 +#define TXCSR_TX_SUSPEND 0x00000002 +#define TXCSR_TX_ENB 0x00000001 +#define TXCSR_TXQ0 0 +#define TXCSR_TXQ1 1 +#define TXCSR_TXQ2 2 +#define TXCSR_TXQ3 3 +#define TXCSR_TXQ4 4 +#define TXCSR_TXQ5 5 +#define TXCSR_TXQ6 6 +#define TXCSR_TXQ7 7 +#define TXCSR_TXQ_WEIGHT(x) \ + (((x) << TXCSR_QWEIGHT_SHIFT) & TXCSR_QWEIGHT_MASK) +#define TXCSR_TXQ_WEIGHT_MIN 0 +#define TXCSR_TXQ_WEIGHT_MAX 15 +#define TXCSR_TXQ_N_SEL(x) \ + (((x) << TXCSR_TXQ_SEL_SHIFT) & TXCSR_TXQ_SEL_MASK) +#define TXCSR_TXQ_N_START(x) \ + (TXCSR_TXQ_START << (TXCSR_TXQ_START_SHIFT + (x))) + +/* Tx queue descriptor base address. 16bytes alignment required. */ +#define JME_TXDBA_LO 0x0004 +#define JME_TXDBA_HI 0x0008 + +/* Tx queue descriptor count. multiple of 16(max = 1024). */ +#define JME_TXQDC 0x000C +#define TXQDC_MASK 0x0000007F0 + +/* Tx queue next descriptor address. */ +#define JME_TXNDA 0x0010 +#define TXNDA_ADDR_MASK 0xFFFFFFF0 +#define TXNDA_DESC_EMPTY 0x00000008 +#define TXNDA_DESC_VALID 0x00000004 +#define TXNDA_DESC_WAIT 0x00000002 +#define TXNDA_DESC_FETCH 0x00000001 + +/* Tx MAC control ans status. */ +#define JME_TXMAC 0x0014 +#define TXMAC_IFG2_MASK 0xC0000000 +#define TXMAC_IFG2_DEFAULT 0x40000000 +#define TXMAC_IFG1_MASK 0x30000000 +#define TXMAC_IFG1_DEFAULT 0x20000000 +#define TXMAC_THRESH_1_PKT 0x00000300 +#define TXMAC_THRESH_1_2_PKT 0x00000200 +#define TXMAC_THRESH_1_4_PKT 0x00000100 +#define TXMAC_THRESH_1_8_PKT 0x00000000 +#define TXMAC_FRAME_BURST 0x00000080 +#define TXMAC_CARRIER_EXT 0x00000040 +#define TXMAC_IFG_ENB 0x00000020 +#define TXMAC_BACKOFF 0x00000010 +#define TXMAC_CARRIER_SENSE 0x00000008 +#define TXMAC_COLL_ENB 0x00000004 +#define TXMAC_CRC_ENB 0x00000002 +#define TXMAC_PAD_ENB 0x00000001 + +/* Tx pause frame control. */ +#define JME_TXPFC 0x0018 +#define TXPFC_VLAN_TAG_MASK 0xFFFF0000 +#define TXPFC_VLAN_TAG_SHIFT 16 +#define TXPFC_VLAN_ENB 0x00008000 +#define TXPFC_PAUSE_ENB 0x00000001 + +/* Tx timer/retry at half duplex. */ +#define JME_TXTRHD 0x001C +#define TXTRHD_RT_PERIOD_ENB 0x80000000 +#define TXTRHD_RT_PERIOD_MASK 0x7FFFFF00 +#define TXTRHD_RT_PERIOD_SHIFT 8 +#define TXTRHD_RT_LIMIT_ENB 0x00000080 +#define TXTRHD_RT_LIMIT_MASK 0x0000007F +#define TXTRHD_RT_LIMIT_SHIFT 0 +#define TXTRHD_RT_PERIOD_DEFAULT 8192 +#define TXTRHD_RT_LIMIT_DEFAULT 8 + +/* Rx control & status. */ +#define JME_RXCSR 0x0020 +#define RXCSR_FIFO_FTHRESH_16T 0x00000000 +#define RXCSR_FIFO_FTHRESH_32T 0x10000000 +#define RXCSR_FIFO_FTHRESH_64T 0x20000000 +#define RXCSR_FIFO_FTHRESH_128T 0x30000000 +#define RXCSR_FIFO_FTHRESH_MASK 0x30000000 +#define RXCSR_FIFO_THRESH_16QW 0x00000000 +#define RXCSR_FIFO_THRESH_32QW 0x04000000 +#define RXCSR_FIFO_THRESH_64QW 0x08000000 +#define RXCSR_FIFO_THRESH_128QW 0x0C000000 +#define RXCSR_FIFO_THRESH_MASK 0x0C000000 +#define RXCSR_DMA_SIZE_16 0x00000000 +#define RXCSR_DMA_SIZE_32 0x01000000 +#define RXCSR_DMA_SIZE_64 0x02000000 +#define RXCSR_DMA_SIZE_128 0x03000000 +#define RXCSR_RXQ_SEL_MASK 0x00030000 +#define RXCSR_RXQ_SEL_SHIFT 16 +#define RXCSR_DESC_RT_GAP_MASK 0x0000F000 +#define RXCSR_DESC_RT_GAP_SHIFT 12 +#define RXCSR_DESC_RT_GAP_256 0x00000000 +#define RXCSR_DESC_RT_GAP_512 0x00001000 +#define RXCSR_DESC_RT_GAP_1024 0x00002000 +#define RXCSR_DESC_RT_GAP_2048 0x00003000 +#define RXCSR_DESC_RT_GAP_4096 0x00004000 +#define RXCSR_DESC_RT_GAP_8192 0x00005000 +#define RXCSR_DESC_RT_GAP_16384 0x00006000 +#define RXCSR_DESC_RT_GAP_32768 0x00007000 +#define RXCSR_DESC_RT_CNT_MASK 0x00000F00 +#define RXCSR_DESC_RT_CNT_SHIFT 8 +#define RXCSR_PASS_WAKEUP_PKT 0x00000040 +#define RXCSR_PASS_MAGIC_PKT 0x00000020 +#define RXCSR_PASS_RUNT_PKT 0x00000010 +#define RXCSR_PASS_BAD_PKT 0x00000008 +#define RXCSR_RXQ_START 0x00000004 +#define RXCSR_RX_SUSPEND 0x00000002 +#define RXCSR_RX_ENB 0x00000001 + +#define RXCSR_RXQ_N_SEL(x) ((x) << RXCSR_RXQ_SEL_SHIFT) +#define RXCSR_RXQ0 0 +#define RXCSR_RXQ1 1 +#define RXCSR_RXQ2 2 +#define RXCSR_RXQ3 3 +#define RXCSR_DESC_RT_CNT(x) \ + ((((x) / 4) << RXCSR_DESC_RT_CNT_SHIFT) & RXCSR_DESC_RT_CNT_MASK) +#define RXCSR_DESC_RT_CNT_DEFAULT 32 + +/* Rx queue descriptor base address. 16bytes alignment needed. */ +#define JME_RXDBA_LO 0x0024 +#define JME_RXDBA_HI 0x0028 + +/* Rx queue descriptor count. multiple of 16(max = 1024). */ +#define JME_RXQDC 0x002C +#define RXQDC_MASK 0x0000007F0 + +/* Rx queue next descriptor address. */ +#define JME_RXNDA 0x0030 +#define RXNDA_ADDR_MASK 0xFFFFFFF0 +#define RXNDA_DESC_EMPTY 0x00000008 +#define RXNDA_DESC_VALID 0x00000004 +#define RXNDA_DESC_WAIT 0x00000002 +#define RXNDA_DESC_FETCH 0x00000001 + +/* Rx MAC control and status. */ +#define JME_RXMAC 0x0034 +#define RXMAC_RSS_UNICAST 0x00000000 +#define RXMAC_RSS_UNI_MULTICAST 0x00010000 +#define RXMAC_RSS_UNI_MULTI_BROADCAST 0x00020000 +#define RXMAC_RSS_ALLFRAME 0x00030000 +#define RXMAC_PROMISC 0x00000800 +#define RXMAC_BROADCAST 0x00000400 +#define RXMAC_MULTICAST 0x00000200 +#define RXMAC_UNICAST 0x00000100 +#define RXMAC_ALLMULTI 0x00000080 +#define RXMAC_MULTICAST_FILTER 0x00000040 +#define RXMAC_COLL_DET_ENB 0x00000020 +#define RXMAC_FC_ENB 0x00000008 +#define RXMAC_VLAN_ENB 0x00000004 +#define RXMAC_PAD_10BYTES 0x00000002 +#define RXMAC_CSUM_ENB 0x00000001 + +/* Rx unicast MAC address. */ +#define JME_PAR0 0x0038 +#define JME_PAR1 0x003C + +/* Rx multicast address hash table. */ +#define JME_MAR0 0x0040 +#define JME_MAR1 0x0044 + +/* Wakeup frame output data port. */ +#define JME_WFODP 0x0048 + +/* Wakeup frame output interface. */ +#define JME_WFOI 0x004C +#define WFOI_MASK_0_31 0x00000000 +#define WFOI_MASK_31_63 0x00000010 +#define WFOI_MASK_64_95 0x00000020 +#define WFOI_MASK_96_127 0x00000030 +#define WFOI_MASK_SEL 0x00000008 +#define WFOI_CRC_SEL 0x00000000 +#define WFOI_WAKEUP_FRAME_MASK 0x00000007 +#define WFOI_WAKEUP_FRAME_SEL(x) ((x) & WFOI_WAKEUP_FRAME_MASK) + +/* Station management interface. */ +#define JME_SMI 0x0050 +#define SMI_DATA_MASK 0xFFFF0000 +#define SMI_DATA_SHIFT 16 +#define SMI_REG_ADDR_MASK 0x0000F800 +#define SMI_REG_ADDR_SHIFT 11 +#define SMI_PHY_ADDR_MASK 0x000007C0 +#define SMI_PHY_ADDR_SHIFT 6 +#define SMI_OP_WRITE 0x00000020 +#define SMI_OP_READ 0x00000000 +#define SMI_OP_EXECUTE 0x00000010 +#define SMI_MDIO 0x00000008 +#define SMI_MDOE 0x00000004 +#define SMI_MDC 0x00000002 +#define SMI_MDEN 0x00000001 +#define SMI_REG_ADDR(x) \ + (((x) << SMI_REG_ADDR_SHIFT) & SMI_REG_ADDR_MASK) +#define SMI_PHY_ADDR(x) \ + (((x) << SMI_PHY_ADDR_SHIFT) & SMI_PHY_ADDR_MASK) + +/* Global host control. */ +#define JME_GHC 0x0054 +#define GHC_LOOPBACK 0x80000000 +#define GHC_RESET 0x40000000 +#define GHC_FULL_DUPLEX 0x00000040 +#define GHC_SPEED_UNKNOWN 0x00000000 +#define GHC_SPEED_10 0x00000010 +#define GHC_SPEED_100 0x00000020 +#define GHC_SPEED_1000 0x00000030 +#define GHC_SPEED_MASK 0x00000030 +#define GHC_LINK_OFF 0x00000004 +#define GHC_LINK_ON 0x00000002 +#define GHC_LINK_STAT_POLLING 0x00000001 + +/* Power management control and status. */ +#define JME_PMCS 0x0060 +#define PMCS_WAKEUP_FRAME_7 0x80000000 +#define PMCS_WAKEUP_FRAME_6 0x40000000 +#define PMCS_WAKEUP_FRAME_5 0x20000000 +#define PMCS_WAKEUP_FRAME_4 0x10000000 +#define PMCS_WAKEUP_FRAME_3 0x08000000 +#define PMCS_WAKEUP_FRAME_2 0x04000000 +#define PMCS_WAKEUP_FRAME_1 0x02000000 +#define PMCS_WAKEUP_FRAME_0 0x01000000 +#define PMCS_LINK_FAIL 0x00040000 +#define PMCS_LINK_RISING 0x00020000 +#define PMCS_MAGIC_FRAME 0x00010000 +#define PMCS_WAKEUP_FRAME_7_ENB 0x00008000 +#define PMCS_WAKEUP_FRAME_6_ENB 0x00004000 +#define PMCS_WAKEUP_FRAME_5_ENB 0x00002000 +#define PMCS_WAKEUP_FRAME_4_ENB 0x00001000 +#define PMCS_WAKEUP_FRAME_3_ENB 0x00000800 +#define PMCS_WAKEUP_FRAME_2_ENB 0x00000400 +#define PMCS_WAKEUP_FRAME_1_ENB 0x00000200 +#define PMCS_WAKEUP_FRAME_0_ENB 0x00000100 +#define PMCS_LINK_FAIL_ENB 0x00000004 +#define PMCS_LINK_RISING_ENB 0x00000002 +#define PMCS_MAGIC_FRAME_ENB 0x00000001 +#define PMCS_WOL_ENB_MASK 0x0000FFFF + + +#define JME_PHY_EEPROM_BASE_MEMOFF 0x0400 +#define JME_PHY_EEPROM_BASE_IOOFF 0x0000 +#define JME_PHY_EEPROM_SIZE 0x0080 +/* Giga PHY & EEPROM registers. */ +#define JME_PHY_EEPROM_BASE_ADDR 0x00 + +#define JME_GIGAR0LO 0x00 +#define JME_GIGAR0HI 0x04 +#define JME_GIGARALO 0x08 +#define JME_GIGARAHI 0x0C +#define JME_GIGARBLO 0x10 +#define JME_GIGARBHI 0x14 +#define JME_GIGARCLO 0x18 +#define JME_GIGARCHI 0x1C +#define JME_GIGARDLO 0x20 +#define JME_GIGARDHI 0x24 + +/* BIST status and control. */ +#define JME_GIGACSR 0x28 +#define GIGACSR_STATUS 0x40000000 +#define GIGACSR_CTRL_MASK 0x30000000 +#define GIGACSR_CTRL_DEFAULT 0x30000000 +#define GIGACSR_TX_CLK_MASK 0x0F000000 +#define GIGACSR_RX_CLK_MASK 0x00F00000 +#define GIGACSR_TX_CLK_INV 0x00080000 +#define GIGACSR_RX_CLK_INV 0x00040000 +#define GIGACSR_PHY_RST 0x00010000 +#define GIGACSR_IRQ_N_O 0x00001000 +#define GIGACSR_BIST_OK 0x00000200 +#define GIGACSR_BIST_DONE 0x00000100 +#define GIGACSR_BIST_LED_ENB 0x00000010 +#define GIGACSR_BIST_MASK 0x00000003 + +/* PHY Link Status. */ +#define JME_LNKSTS 0x30 +#define LINKSTS_SPEED_10 0x00000000 +#define LINKSTS_SPEED_100 0x00004000 +#define LINKSTS_SPEED_1000 0x00008000 +#define LINKSTS_FULL_DUPLEX 0x00002000 +#define LINKSTS_PAGE_RCVD 0x00001000 +#define LINKSTS_SPDDPX_RESOLVED 0x00000800 +#define LINKSTS_UP 0x00000400 +#define LINKSTS_ANEG_COMP 0x00000200 +#define LINKSTS_MDI_CROSSOVR 0x00000040 +#define LINKSTS_LPAR_PAUSE_ASYM 0x00000002 +#define LINKSTS_LPAR_PAUSE 0x00000001 + +/* SMB control and status. */ +#define JME_SMBCSR 0x40 +#define SMBCSR_SLAVE_ADDR_MASK 0x7F000000 +#define SMBCSR_WR_DATA_NACK 0x00040000 +#define SMBCSR_CMD_NACK 0x00020000 +#define SMBCSR_RELOAD 0x00010000 +#define SMBCSR_CMD_ADDR_MASK 0x0000FF00 +#define SMBCSR_SCL_STAT 0x00000080 +#define SMBCSR_SDA_STAT 0x00000040 +#define SMBCSR_EEPROM_PRESENT 0x00000020 +#define SMBCSR_INIT_LD_DONE 0x00000010 +#define SMBCSR_HW_BUSY_MASK 0x0000000F +#define SMBCSR_HW_IDLE 0x00000000 + +/* SMB interface. */ +#define JME_SMBINTF 0x44 +#define SMBINTF_RD_DATA_MASK 0xFF000000 +#define SMBINTF_RD_DATA_SHIFT 24 +#define SMBINTF_WR_DATA_MASK 0x00FF0000 +#define SMBINTF_WR_DATA_SHIFT 16 +#define SMBINTF_ADDR_MASK 0x0000FF00 +#define SMBINTF_ADDR_SHIFT 8 +#define SMBINTF_RD 0x00000020 +#define SMBINTF_WR 0x00000000 +#define SMBINTF_CMD_TRIGGER 0x00000010 +#define SMBINTF_BUSY 0x00000010 +#define SMBINTF_FAST_MODE 0x00000008 +#define SMBINTF_GPIO_SCL 0x00000004 +#define SMBINTF_GPIO_SDA 0x00000002 +#define SMBINTF_GPIO_ENB 0x00000001 + +#define JME_EEPROM_SIG0 0x55 +#define JME_EEPROM_SIG1 0xAA +#define JME_EEPROM_DESC_BYTES 3 +#define JME_EEPROM_DESC_END 0x80 +#define JME_EEPROM_FUNC_MASK 0x70 +#define JME_EEPROM_FUNC_SHIFT 4 +#define JME_EEPROM_PAGE_MASK 0x0F +#define JME_EEPROM_PAGE_SHIFT 0 + +#define JME_EEPROM_FUNC0 0 +/* PCI configuration space. */ +#define JME_EEPROM_PAGE_BAR0 0 +/* 128 bytes I/O window. */ +#define JME_EEPROM_PAGE_BAR1 1 +/* 256 bytes I/O window. */ +#define JME_EEPROM_PAGE_BAR2 2 + +#define JME_EEPROM_END 0xFF + +#define JME_EEPROM_MKDESC(f, p) \ + ((((f) & JME_EEPROM_FUNC_MASK) << JME_EEPROM_FUNC_SHIFT) | \ + (((p) & JME_EEPROM_PAGE_MASK) << JME_EEPROM_PAGE_SHIFT)) + +/* 3-wire EEPROM interface. Obsolete interface, use SMBCSR. */ +#define JME_EEPINTF 0x48 +#define EEPINTF_DATA_MASK 0xFFFF0000 +#define EEPINTF_DATA_SHIFT 16 +#define EEPINTF_ADDR_MASK 0x0000FC00 +#define EEPINTF_ADDR_SHIFT 10 +#define EEPRINTF_OP_MASK 0x00000300 +#define EEPINTF_OP_EXECUTE 0x00000080 +#define EEPINTF_DATA_OUT 0x00000008 +#define EEPINTF_DATA_IN 0x00000004 +#define EEPINTF_CLK 0x00000002 +#define EEPINTF_SEL 0x00000001 + +/* 3-wire EEPROM control and status. Obsolete interface, use SMBCSR. */ +#define JME_EEPCSR 0x4C +#define EEPCSR_EEPROM_RELOAD 0x00000002 +#define EEPCSR_EEPROM_PRESENT 0x00000001 + +/* Misc registers. */ +#define JME_MISC_BASE_MEMOFF 0x800 +#define JME_MISC_BASE_IOOFF 0x080 +#define JME_MISC_SIZE 0x080 + +/* Timer control and status. */ +#define JME_TMCSR 0x00 +#define TMCSR_SW_INTR 0x80000000 +#define TMCSR_TIMER_INTR 0x10000000 +#define TMCSR_TIMER_ENB 0x01000000 +#define TMCSR_TIMER_COUNT_MASK 0x00FFFFFF + +/* GPIO control and status. */ +#define JME_GPIO 0x04 +#define GPIO_4_SPI_IN 0x80000000 +#define GPIO_3_SPI_IN 0x40000000 +#define GPIO_4_SPI_OUT 0x20000000 +#define GPIO_4_SPI_OUT_ENB 0x10000000 +#define GPIO_3_SPI_OUT 0x08000000 +#define GPIO_3_SPI_OUT_ENB 0x04000000 +#define GPIO_3_4_LED 0x00000000 +#define GPIO_3_4_GPIO 0x02000000 +#define GPIO_2_CLKREQN_IN 0x00100000 +#define GPIO_2_CLKREQN_OUT 0x00040000 +#define GPIO_2_CLKREQN_OUT_ENB 0x00020000 +#define GPIO_1_LED42_IN 0x00001000 +#define GPIO_1_LED42_OUT 0x00000400 +#define GPIO_1_LED42_OUT_ENB 0x00000200 +#define GPIO_1_LED42_ENB 0x00000100 +#define GPIO_0_SDA_IN 0x00000010 +#define GPIO_0_SDA_OUT 0x00000004 +#define GPIO_0_SDA_OUT_ENB 0x00000002 +#define GPIO_0_SDA_ENB 0x00000001 + +/* General purpose register 0. */ +#define JME_GPREG0 0x08 +#define GPREG0_SH_POST_DW7_DIS 0x80000000 +#define GPREG0_SH_POST_DW6_DIS 0x40000000 +#define GPREG0_SH_POST_DW5_DIS 0x20000000 +#define GPREG0_SH_POST_DW4_DIS 0x10000000 +#define GPREG0_SH_POST_DW3_DIS 0x08000000 +#define GPREG0_SH_POST_DW2_DIS 0x04000000 +#define GPREG0_SH_POST_DW1_DIS 0x02000000 +#define GPREG0_SH_POST_DW0_DIS 0x01000000 +#define GPREG0_DMA_RD_REQ_8 0x00000000 +#define GPREG0_DMA_RD_REQ_6 0x00100000 +#define GPREG0_DMA_RD_REQ_5 0x00200000 +#define GPREG0_DMA_RD_REQ_4 0x00300000 +#define GPREG0_POST_DW0_ENB 0x00040000 +#define GPREG0_PCC_CLR_DIS 0x00020000 +#define GPREG0_FORCE_SCL_OUT 0x00010000 +#define GPREG0_DL_RSTB_DIS 0x00008000 +#define GPREG0_STICKY_RESET 0x00004000 +#define GPREG0_DL_RSTB_CFG_DIS 0x00002000 +#define GPREG0_LINK_CHG_POLL 0x00001000 +#define GPREG0_LINK_CHG_DIRECT 0x00000000 +#define GPREG0_MSI_GEN_SEL 0x00000800 +#define GPREG0_SMB_PAD_PU_DIS 0x00000400 +#define GPREG0_PCC_UNIT_16US 0x00000000 +#define GPREG0_PCC_UNIT_256US 0x00000100 +#define GPREG0_PCC_UNIT_US 0x00000200 +#define GPREG0_PCC_UNIT_MS 0x00000300 +#define GPREG0_PCC_UNIT_MASK 0x00000300 +#define GPREG0_INTR_EVENT_ENB 0x00000080 +#define GPREG0_PME_ENB 0x00000020 +#define GPREG0_PHY_ADDR_MASK 0x0000001F +#define GPREG0_PHY_ADDR_SHIFT 0 +#define GPREG0_PHY_ADDR 1 + +/* General purpose register 1. */ +#define JME_GPREG1 0x0C +#define GPREG1_RSS_IPV6_10_100 0x00000040 /* JMC250 A2 */ +#define GPREG1_HDPX_FIX 0x00000020 /* JMC250 A2 */ +#define GPREG1_INTDLY_UNIT_16US 0x00000018 /* JMC250 A1, A2 */ +#define GPREG1_INTDLY_UNIT_1US 0x00000010 /* JMC250 A1, A2 */ +#define GPREG1_INTDLY_UNIT_256NS 0x00000008 /* JMC250 A1, A2 */ +#define GPREG1_INTDLY_UNIT_16NS 0x00000000 /* JMC250 A1, A2 */ +#define GPREG1_INTDLY_MASK 0x00000007 + +/* MSIX entry number of interrupt source. */ +#define JME_MSINUM_BASE 0x10 +#define JME_MSINUM_END 0x1F +#define MSINUM_MASK 0x7FFFFFFF +#define MSINUM_ENTRY_MASK 7 +#define MSINUM_REG_INDEX(x) ((x) / 8) +#define MSINUM_INTR_SOURCE(x, y) \ + (((x) & MSINUM_ENTRY_MASK) << (((y) & 7) * 4)) +#define MSINUM_NUM_INTR_SOURCE 32 + +/* Interrupt event status. */ +#define JME_INTR_STATUS 0x20 +#define INTR_SW 0x80000000 +#define INTR_TIMER 0x40000000 +#define INTR_LINKCHG 0x20000000 +#define INTR_PAUSE 0x10000000 +#define INTR_MAGIC_PKT 0x08000000 +#define INTR_WAKEUP_PKT 0x04000000 +#define INTR_RXQ0_COAL_TO 0x02000000 +#define INTR_RXQ1_COAL_TO 0x01000000 +#define INTR_RXQ2_COAL_TO 0x00800000 +#define INTR_RXQ3_COAL_TO 0x00400000 +#define INTR_TXQ_COAL_TO 0x00200000 +#define INTR_RXQ0_COAL 0x00100000 +#define INTR_RXQ1_COAL 0x00080000 +#define INTR_RXQ2_COAL 0x00040000 +#define INTR_RXQ3_COAL 0x00020000 +#define INTR_TXQ_COAL 0x00010000 +#define INTR_RXQ3_DESC_EMPTY 0x00008000 +#define INTR_RXQ2_DESC_EMPTY 0x00004000 +#define INTR_RXQ1_DESC_EMPTY 0x00002000 +#define INTR_RXQ0_DESC_EMPTY 0x00001000 +#define INTR_RXQ3_COMP 0x00000800 +#define INTR_RXQ2_COMP 0x00000400 +#define INTR_RXQ1_COMP 0x00000200 +#define INTR_RXQ0_COMP 0x00000100 +#define INTR_TXQ7_COMP 0x00000080 +#define INTR_TXQ6_COMP 0x00000040 +#define INTR_TXQ5_COMP 0x00000020 +#define INTR_TXQ4_COMP 0x00000010 +#define INTR_TXQ3_COMP 0x00000008 +#define INTR_TXQ2_COMP 0x00000004 +#define INTR_TXQ1_COMP 0x00000002 +#define INTR_TXQ0_COMP 0x00000001 + +#define INTR_RXQ_COAL_TO \ + (INTR_RXQ0_COAL_TO | INTR_RXQ1_COAL_TO | \ + INTR_RXQ2_COAL_TO | INTR_RXQ3_COAL_TO) + +#define INTR_RXQ_COAL \ + (INTR_RXQ0_COAL | INTR_RXQ1_COAL | INTR_RXQ2_COAL | \ + INTR_RXQ3_COAL) + +#define INTR_RXQ_COMP \ + (INTR_RXQ0_COMP | INTR_RXQ1_COMP | INTR_RXQ2_COMP | \ + INTR_RXQ3_COMP) + +#define INTR_RXQ_DESC_EMPTY \ + (INTR_RXQ0_DESC_EMPTY | INTR_RXQ1_DESC_EMPTY | \ + INTR_RXQ2_DESC_EMPTY | INTR_RXQ3_DESC_EMPTY) + +#define INTR_RXQ_COMP \ + (INTR_RXQ0_COMP | INTR_RXQ1_COMP | INTR_RXQ2_COMP | \ + INTR_RXQ3_COMP) + +#define INTR_TXQ_COMP \ + (INTR_TXQ0_COMP | INTR_TXQ1_COMP | INTR_TXQ2_COMP | \ + INTR_TXQ3_COMP | INTR_TXQ4_COMP | INTR_TXQ5_COMP | \ + INTR_TXQ6_COMP | INTR_TXQ7_COMP) + +#define JME_INTRS \ + (INTR_RXQ_COAL_TO | INTR_TXQ_COAL_TO | INTR_RXQ_COAL | \ + INTR_TXQ_COAL | INTR_RXQ_DESC_EMPTY) + +#define N_INTR_SW 31 +#define N_INTR_TIMER 30 +#define N_INTR_LINKCHG 29 +#define N_INTR_PAUSE 28 +#define N_INTR_MAGIC_PKT 27 +#define N_INTR_WAKEUP_PKT 26 +#define N_INTR_RXQ0_COAL_TO 25 +#define N_INTR_RXQ1_COAL_TO 24 +#define N_INTR_RXQ2_COAL_TO 23 +#define N_INTR_RXQ3_COAL_TO 22 +#define N_INTR_TXQ_COAL_TO 21 +#define N_INTR_RXQ0_COAL 20 +#define N_INTR_RXQ1_COAL 19 +#define N_INTR_RXQ2_COAL 18 +#define N_INTR_RXQ3_COAL 17 +#define N_INTR_TXQ_COAL 16 +#define N_INTR_RXQ3_DESC_EMPTY 15 +#define N_INTR_RXQ2_DESC_EMPTY 14 +#define N_INTR_RXQ1_DESC_EMPTY 13 +#define N_INTR_RXQ0_DESC_EMPTY 12 +#define N_INTR_RXQ3_COMP 11 +#define N_INTR_RXQ2_COMP 10 +#define N_INTR_RXQ1_COMP 9 +#define N_INTR_RXQ0_COMP 8 +#define N_INTR_TXQ7_COMP 7 +#define N_INTR_TXQ6_COMP 6 +#define N_INTR_TXQ5_COMP 5 +#define N_INTR_TXQ4_COMP 4 +#define N_INTR_TXQ3_COMP 3 +#define N_INTR_TXQ2_COMP 2 +#define N_INTR_TXQ1_COMP 1 +#define N_INTR_TXQ0_COMP 0 + +/* Interrupt request status. */ +#define JME_INTR_REQ_STATUS 0x24 + +/* Interrupt enable - setting port. */ +#define JME_INTR_MASK_SET 0x28 + +/* Interrupt enable - clearing port. */ +#define JME_INTR_MASK_CLR 0x2C + +/* Packet completion coalescing control of Rx queue 0, 1, 2 and 3. */ +#define JME_PCCRX0 0x30 +#define JME_PCCRX1 0x34 +#define JME_PCCRX2 0x38 +#define JME_PCCRX3 0x3C +#define PCCRX_COAL_TO_MASK 0xFFFF0000 +#define PCCRX_COAL_TO_SHIFT 16 +#define PCCRX_COAL_PKT_MASK 0x0000FF00 +#define PCCRX_COAL_PKT_SHIFT 8 + +#define PCCRX_COAL_TO_MIN 1 +#define PCCRX_COAL_TO_DEFAULT 100 +#define PCCRX_COAL_TO_MAX 65535 + +#define PCCRX_COAL_PKT_MIN 1 +#define PCCRX_COAL_PKT_DEFAULT 2 +#define PCCRX_COAL_PKT_MAX 255 + +/* Packet completion coalescing control of Tx queue. */ +#define JME_PCCTX 0x40 +#define PCCTX_COAL_TO_MASK 0xFFFF0000 +#define PCCTX_COAL_TO_SHIFT 16 +#define PCCTX_COAL_PKT_MASK 0x0000FF00 +#define PCCTX_COAL_PKT_SHIFT 8 +#define PCCTX_COAL_TXQ7 0x00000080 +#define PCCTX_COAL_TXQ6 0x00000040 +#define PCCTX_COAL_TXQ5 0x00000020 +#define PCCTX_COAL_TXQ4 0x00000010 +#define PCCTX_COAL_TXQ3 0x00000008 +#define PCCTX_COAL_TXQ2 0x00000004 +#define PCCTX_COAL_TXQ1 0x00000002 +#define PCCTX_COAL_TXQ0 0x00000001 + +#define PCCTX_COAL_TO_MIN 1 +#define PCCTX_COAL_TO_DEFAULT 100 +#define PCCTX_COAL_TO_MAX 65535 + +#define PCCTX_COAL_PKT_MIN 1 +#define PCCTX_COAL_PKT_DEFAULT 8 +#define PCCTX_COAL_PKT_MAX 255 + +/* Chip mode and FPGA version. */ +#define JME_CHIPMODE 0x44 +#define CHIPMODE_FPGA_REV_MASK 0xFFFF0000 +#define CHIPMODE_FPGA_REV_SHIFT 16 +#define CHIPMODE_NOT_FPGA 0 +#define CHIPMODE_REV_MASK 0x0000FF00 +#define CHIPMODE_REV_SHIFT 8 +#define CHIPMODE_MODE_48P 0x0000000C +#define CHIPMODE_MODE_64P 0x00000004 +#define CHIPMODE_MODE_128P_MAC 0x00000003 +#define CHIPMODE_MODE_128P_DBG 0x00000002 +#define CHIPMODE_MODE_128P_PHY 0x00000000 + +/* Shadow status base address high/low. */ +#define JME_SHBASE_ADDR_HI 0x48 +#define JME_SHBASE_ADDR_LO 0x4C +#define SHBASE_ADDR_LO_MASK 0xFFFFFFE0 +#define SHBASE_POST_FORCE 0x00000002 +#define SHBASE_POST_ENB 0x00000001 + +/* Timer 1 and 2. */ +#define JME_TIMER1 0x70 +#define JME_TIMER2 0x74 +#define TIMER_ENB 0x01000000 +#define TIMER_CNT_MASK 0x00FFFFFF +#define TIMER_CNT_SHIFT 0 +#define TIMER_UNIT 1024 /* 1024us */ + +/* Aggresive power mode control. */ +#define JME_APMC 0x7C +#define APMC_PCIE_SDOWN_STAT 0x80000000 +#define APMC_PCIE_SDOWN_ENB 0x40000000 +#define APMC_PSEUDO_HOT_PLUG 0x20000000 +#define APMC_EXT_PLUGIN_ENB 0x04000000 +#define APMC_EXT_PLUGIN_CTL_MSK 0x03000000 +#define APMC_DIS_SRAM 0x00000004 +#define APMC_DIS_CLKPM 0x00000002 +#define APMC_DIS_CLKTX 0x00000001 + +/* Packet completion coalesing status of Rx queue 0, 1, 2 and 3. */ +#define JME_PCCSRX_BASE 0x80 +#define JME_PCCSRX_END 0x8F +#define PCCSRX_REG(x) (JME_PCCSRX_BASE + ((x) * 4)) +#define PCCSRX_TO_MASK 0xFFFF0000 +#define PCCSRX_TO_SHIFT 16 +#define PCCSRX_PKT_CNT_MASK 0x0000FF00 +#define PCCSRX_PKT_CNT_SHIFT 8 + +/* Packet completion coalesing status of Tx queue. */ +#define JME_PCCSTX 0x90 +#define PCCSTX_TO_MASK 0xFFFF0000 +#define PCCSTX_TO_SHIFT 16 +#define PCCSTX_PKT_CNT_MASK 0x0000FF00 +#define PCCSTX_PKT_CNT_SHIFT 8 + +/* Tx queues empty indicator. */ +#define JME_TXQEMPTY 0x94 +#define TXQEMPTY_TXQ7 0x00000080 +#define TXQEMPTY_TXQ6 0x00000040 +#define TXQEMPTY_TXQ5 0x00000020 +#define TXQEMPTY_TXQ4 0x00000010 +#define TXQEMPTY_TXQ3 0x00000008 +#define TXQEMPTY_TXQ2 0x00000004 +#define TXQEMPTY_TXQ1 0x00000002 +#define TXQEMPTY_TXQ0 0x00000001 +#define TXQEMPTY_N_TXQ(x, y) ((x) & (0x01 << (y))) + +/* RSS control registers. */ +#define JME_RSS_BASE 0x0C00 + +#define JME_RSSC 0x0C00 +#define RSSC_HASH_LEN_MASK 0x0000E000 +#define RSSC_HASH_64_ENTRY 0x0000A000 +#define RSSC_HASH_128_ENTRY 0x0000E000 +#define RSSC_HASH_NONE 0x00001000 +#define RSSC_HASH_IPV6 0x00000800 +#define RSSC_HASH_IPV4 0x00000400 +#define RSSC_HASH_IPV6_TCP 0x00000200 +#define RSSC_HASH_IPV4_TCP 0x00000100 +#define RSSC_NCPU_MASK 0x000000F8 +#define RSSC_NCPU_SHIFT 3 +#define RSSC_DIS_RSS 0x00000000 +#define RSSC_2RXQ_ENB 0x00000001 +#define RSSS_4RXQ_ENB 0x00000002 + +/* CPU vector. */ +#define JME_RSSCPU 0x0C04 +#define RSSCPU_N_SEL(x) ((1 << (x)) + +/* RSS Hash value. */ +#define JME_RSSHASH 0x0C10 + +#define JME_RSSHASH_STAT 0x0C14 + +#define JME_RSS_RDATA0 0x0C18 + +#define JME_RSS_RDATA1 0x0C1C + +/* RSS secret key. */ +#define JME_RSSKEY_BASE 0x0C40 +#define JME_RSSKEY_LAST 0x0C64 +#define JME_RSSKEY_END 0x0C67 +#define HASHKEY_NBYTES 40 +#define RSSKEY_REG(x) (JME_RSSKEY_LAST - (4 * ((x) / 4))) +#define RSSKEY_VALUE(x, y) ((x) << (24 - 8 * ((y) % 4))) + +/* RSS indirection table entries. */ +#define JME_RSSTBL_BASE 0x0C80 +#define JME_RSSTBL_END 0x0CFF +#define RSSTBL_NENTRY 128 +#define RSSTBL_REG(x) (JME_RSSTBL_BASE + ((x) / 4)) +#define RSSTBL_VALUE(x, y) ((x) << (8 * ((y) % 4))) + +/* MSI-X table. */ +#define JME_MSIX_BASE_ADDR 0x2000 + +#define JME_MSIX_BASE 0x2000 +#define JME_MSIX_END 0x207F +#define JME_MSIX_NENTRY 8 +#define MSIX_REG(x) (JME_MSIX_BASE + ((x) * 0x10)) +#define MSIX_ADDR_HI_OFF 0x00 +#define MSIX_ADDR_LO_OFF 0x04 +#define MSIX_ADDR_LO_MASK 0xFFFFFFFC +#define MSIX_DATA_OFF 0x08 +#define MSIX_VECTOR_OFF 0x0C +#define MSIX_VECTOR_RSVD 0x80000000 +#define MSIX_VECTOR_DIS 0x00000001 + +/* MSI-X PBA. */ +#define JME_MSIX_PBA_BASE_ADDR 0x3000 + +#define JME_MSIX_PBA 0x3000 +#define MSIX_PBA_RSVD_MASK 0xFFFFFF00 +#define MSIX_PBA_RSVD_SHIFT 8 +#define MSIX_PBA_PEND_MASK 0x000000FF +#define MSIX_PBA_PEND_SHIFT 0 +#define MSIX_PBA_PEND_ENTRY7 0x00000080 +#define MSIX_PBA_PEND_ENTRY6 0x00000040 +#define MSIX_PBA_PEND_ENTRY5 0x00000020 +#define MSIX_PBA_PEND_ENTRY4 0x00000010 +#define MSIX_PBA_PEND_ENTRY3 0x00000008 +#define MSIX_PBA_PEND_ENTRY2 0x00000004 +#define MSIX_PBA_PEND_ENTRY1 0x00000002 +#define MSIX_PBA_PEND_ENTRY0 0x00000001 + +#define JME_PHY_OUI 0x001B8C +#define JME_PHY_MODEL 0x21 +#define JME_PHY_REV 0x01 +#define JME_PHY_ADDR 1 + +/* JMC250 shadow status block. */ +struct jme_ssb { + uint32_t dw0; + uint32_t dw1; + uint32_t dw2; + uint32_t dw3; + uint32_t dw4; + uint32_t dw5; + uint32_t dw6; + uint32_t dw7; +}; + +/* JMC250 descriptor structures. */ +struct jme_desc { + uint32_t flags; + uint32_t buflen; + uint32_t addr_hi; + uint32_t addr_lo; +}; + +#define JME_TD_OWN 0x80000000 +#define JME_TD_INTR 0x40000000 +#define JME_TD_64BIT 0x20000000 +#define JME_TD_TCPCSUM 0x10000000 +#define JME_TD_UDPCSUM 0x08000000 +#define JME_TD_IPCSUM 0x04000000 +#define JME_TD_TSO 0x02000000 +#define JME_TD_VLAN_TAG 0x01000000 +#define JME_TD_VLAN_MASK 0x0000FFFF + +#define JME_TD_MSS_MASK 0xFFFC0000 +#define JME_TD_MSS_SHIFT 18 +#define JME_TD_BUF_LEN_MASK 0x0000FFFF +#define JME_TD_BUF_LEN_SHIFT 0 + +#define JME_TD_FRAME_LEN_MASK 0x0000FFFF +#define JME_TD_FRAME_LEN_SHIFT 0 + +/* + * Only the first Tx descriptor of a packet is updated + * after packet transmission. + */ +#define JME_TD_TMOUT 0x20000000 +#define JME_TD_RETRY_EXP 0x10000000 +#define JME_TD_COLLISION 0x08000000 +#define JME_TD_UNDERRUN 0x04000000 +#define JME_TD_EHDR_SIZE_MASK 0x000000FF +#define JME_TD_EHDR_SIZE_SHIFT 0 + +#define JME_TD_SEG_CNT_MASK 0xFFFF0000 +#define JME_TD_SEG_CNT_SHIFT 16 +#define JME_TD_RETRY_CNT_MASK 0x0000FFFF +#define JME_TD_RETRY_CNT_SHIFT 0 + +#define JME_RD_OWN 0x80000000 +#define JME_RD_INTR 0x40000000 +#define JME_RD_64BIT 0x20000000 + +#define JME_RD_BUF_LEN_MASK 0x0000FFFF +#define JME_RD_BUF_LEN_SHIFT 0 + +/* + * Only the first Rx descriptor of a packet is updated + * after packet reception. + */ +#define JME_RD_MORE_FRAG 0x20000000 +#define JME_RD_TCP 0x10000000 +#define JME_RD_UDP 0x08000000 +#define JME_RD_IPCSUM 0x04000000 +#define JME_RD_TCPCSUM 0x02000000 +#define JME_RD_UDPCSUM 0x01000000 +#define JME_RD_VLAN_TAG 0x00800000 +#define JME_RD_IPV4 0x00400000 +#define JME_RD_IPV6 0x00200000 +#define JME_RD_PAUSE 0x00100000 +#define JME_RD_MAGIC 0x00080000 +#define JME_RD_WAKEUP 0x00040000 +#define JME_RD_BCAST 0x00030000 +#define JME_RD_MCAST 0x00020000 +#define JME_RD_UCAST 0x00010000 +#define JME_RD_VLAN_MASK 0x0000FFFF +#define JME_RD_VLAN_SHIFT 0 +#define JME_RD_TCPV4 (JME_RD_IPV4|JME_RD_TCP) +#define JME_RD_UDPV4 (JME_RD_IPV4|JME_RD_UDP) +#define JME_RD_TCPV6 (JME_RD_IPV6|JME_RD_TCP) +#define JME_RD_UDPV6 (JME_RD_IPV6|JME_RD_UDP) + +#define JME_RD_VALID 0x80000000 +#define JME_RD_CNT_MASK 0x7F000000 +#define JME_RD_CNT_SHIFT 24 +#define JME_RD_GIANT 0x00800000 +#define JME_RD_GMII_ERR 0x00400000 +#define JME_RD_NBL_RCVD 0x00200000 +#define JME_RD_COLL 0x00100000 +#define JME_RD_ABORT 0x00080000 +#define JME_RD_RUNT 0x00040000 +#define JME_RD_FIFO_OVRN 0x00020000 +#define JME_RD_CRC_ERR 0x00010000 +#define JME_RD_FRAME_LEN_MASK 0x0000FFFF + +#define JME_RX_ERR_STAT \ + (JME_RD_GIANT | JME_RD_GMII_ERR | JME_RD_NBL_RCVD | \ + JME_RD_COLL | JME_RD_ABORT | JME_RD_RUNT | \ + JME_RD_FIFO_OVRN | JME_RD_CRC_ERR) + +#define JME_RD_ERR_MASK 0x00FF0000 +#define JME_RD_ERR_SHIFT 16 +#define JME_RX_ERR(x) (((x) & JME_RD_ERR_MASK) >> JME_RD_ERR_SHIFT) +#define JME_RX_ERR_BITS "\20" \ + "\1CRCERR\2FIFOOVRN\3RUNT\4ABORT" \ + "\5COLL\6NBLRCVD\7GMIIERR\10" + +#define JME_RX_NSEGS(x) (((x) & JME_RD_CNT_MASK) >> JME_RD_CNT_SHIFT) +#define JME_RX_BYTES(x) ((x) & JME_RD_FRAME_LEN_MASK) +#define JME_RX_PAD_BYTES 10 + +#define JME_RD_RSS_HASH_VALUE 0xFFFFFFFF + +#define JME_RD_RSS_HASH_MASK 0x00003F00 +#define JME_RD_RSS_HASH_SHIFT 8 +#define JME_RD_RSS_HASH_NONE 0x00000000 +#define JME_RD_RSS_HASH_IPV4 0x00000100 +#define JME_RD_RSS_HASH_IPV4TCP 0x00000200 +#define JME_RD_RSS_HASH_IPV6 0x00000400 +#define JME_RD_RSS_HASH_IPV6TCP 0x00001000 +#define JME_RD_HASH_FN_NONE 0x00000000 +#define JME_RD_HASH_FN_TOEPLITZ 0x00000001 + +#define JME_MAX_TX_LEN 65535 +#define JME_MAX_RX_LEN 65535 + +#define JME_ADDR_LO(x) ((uint64_t) (x) & 0xFFFFFFFF) +#define JME_ADDR_HI(x) ((uint64_t) (x) >> 32) + +/* + * JMC250 can't handle Tx checksum offload/TSO if frame length + * is larger than its FIFO size(2K). It's also good idea to not + * use jumbo frame if hardware is running at half-duplex media. + * Because the jumbo frame may not fit into the Tx FIFO, + * collisions make hardware fetch frame from host memory with + * DMA again which in turn slows down Tx performance + * significantly. + */ +#define JME_TX_FIFO_SIZE 2000 +/* + * JMC250 has just 4K Rx FIFO. To support jumbo frame that is + * larger than 4K bytes in length, Rx FIFO threshold should be + * adjusted to minimize Rx FIFO overrun. + */ +#define JME_RX_FIFO_SIZE 4000 + +#endif |
