/*-
* Copyright (c) 1999 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Paul Kranenburg.
*
* 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.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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.
*/
/*
* HME common initialization.
*
* hme_softc fields that must be initialized by the front-end:
*
* the bus tag:
* sc_bustag
*
* the DMA bus tag:
* sc_dmatag
*
* the bus handles:
* sc_seb (Shared Ethernet Block registers)
* sc_erx (Receiver Unit registers)
* sc_etx (Transmitter Unit registers)
* sc_mac (MAC registers)
* sc_mif (Management Interface registers)
*
* the maximum bus burst size:
* sc_burst
*
* (notyet:DMA capable memory for the ring descriptors & packet buffers:
* rb_membase, rb_dmabase)
*
* the local Ethernet address:
* sc_enaddr
*
*/
/* Make sure the chip is stopped. */
hme_chipreset(sc);
/*
* Allocate descriptors and buffers
* XXX - do all this differently.. and more configurably,
* eg. use things as `dma_load_mbuf()' on transmit,
* and a pool of `EXTMEM' mbufs (with buffers DMA-mapped
* all the time) on the receiver side.
*
* Note: receive buffers must be 64-byte aligned.
* Also, apparently, the buffers must extend to a DMA burst
* boundary beyond the maximum packet size.
*/
#define _HME_NDESC 128
#define _HME_BUFSZ 1600
/* Note: the # of descriptors must be a multiple of 16 */
sc->sc_rb.rb_ntbuf = _HME_NDESC;
sc->sc_rb.rb_nrbuf = _HME_NDESC;
/*
* Allocate DMA capable memory
* Buffer descriptors must be aligned on a 2048 byte boundary;
* take this into account when calculating the size. Note that
* the maximum number of descriptors (256) occupies 2048 bytes,
* so we allocate that much regardless of _HME_NDESC.
*/
size = 2048 + /* TX descriptors */
2048 + /* RX descriptors */
sc->sc_rb.rb_ntbuf * _HME_BUFSZ + /* TX buffers */
sc->sc_rb.rb_nrbuf * _HME_BUFSZ; /* RX buffers */
child = LIST_FIRST(&mii->mii_phys);
if (child == NULL) {
/* No PHY attached */
ifmedia_add(&mii->mii_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_MANUAL);
} else {
/*
* Walk along the list of attached MII devices and
* establish an `MII instance' to `phy number'
* mapping. We'll use this mapping in media change
* requests to determine which phy to use to program
* the MIF configuration register.
*/
for (; child != NULL; child = LIST_NEXT(child, mii_list)) {
/*
* Note: we support just two PHYs: the built-in
* internal device and an external on the MII
* connector.
*/
if (child->mii_phy > 1 || child->mii_inst > 1) {
aprint_error_dev(sc->sc_dev,
"cannot accommodate MII device %s"
" at phy %d, instance %d\n",
device_xname(child->mii_dev),
child->mii_phy, child->mii_inst);
continue;
}
sc->sc_phys[child->mii_inst] = child->mii_phy;
}
/*
* Set the default media to auto negotiation if the phy has
* the auto negotiation capability.
* XXX; What to do otherwise?
*/
if (ifmedia_match(&mii->mii_media, IFM_ETHER | IFM_AUTO, 0))
ifmedia_set(&mii->mii_media, IFM_ETHER | IFM_AUTO);
/*
else
ifmedia_set(&sc->sc_mii.mii_media, sc->sc_defaultmedia);
*/
}
/*
* Initialize transmit buffer descriptors
*/
for (i = 0; i < ntbuf; i++) {
HME_XD_SETADDR(sc->sc_pci, hr->rb_txd, i, txbufdma + i * _HME_BUFSZ);
HME_XD_SETFLAGS(sc->sc_pci, hr->rb_txd, i, 0);
}
/*
* Initialize receive buffer descriptors
*/
for (i = 0; i < nrbuf; i++) {
HME_XD_SETADDR(sc->sc_pci, hr->rb_rxd, i, rxbufdma + i * _HME_BUFSZ);
HME_XD_SETFLAGS(sc->sc_pci, hr->rb_rxd, i,
HME_XD_OWN | HME_XD_ENCODE_RSIZE(_HME_BUFSZ));
}
/*
* Initialization of interface; set up initialization block
* and transmit/receive descriptor rings.
*/
int
hme_init(struct ifnet *ifp)
{
struct hme_softc *sc = ifp->if_softc;
bus_space_tag_t t = sc->sc_bustag;
bus_space_handle_t seb = sc->sc_seb;
bus_space_handle_t etx = sc->sc_etx;
bus_space_handle_t erx = sc->sc_erx;
bus_space_handle_t mac = sc->sc_mac;
uint8_t *ea;
uint32_t v;
int rc;
/*
* Initialization sequence. The numbered steps below correspond
* to the sequence outlined in section 6.3.5.1 in the Ethernet
* Channel Engine manual (part of the PCIO manual).
* See also the STP2002-STQ document from Sun Microsystems.
*/
switch (sc->sc_burst) {
default:
v = 0;
break;
case 16:
v = HME_SEB_CFG_BURST16;
break;
case 32:
v = HME_SEB_CFG_BURST32;
break;
case 64:
v = HME_SEB_CFG_BURST64;
break;
}
bus_space_write_4(t, seb, HME_SEBI_CFG, v);
/* step 9. ETX Configuration: use mostly default values */
/* Enable DMA */
v = bus_space_read_4(t, etx, HME_ETXI_CFG);
v |= HME_ETX_CFG_DMAENABLE;
bus_space_write_4(t, etx, HME_ETXI_CFG, v);
/* Transmit Descriptor ring size: in increments of 16 */
bus_space_write_4(t, etx, HME_ETXI_RSIZE, _HME_NDESC / 16 - 1);
/* Encode Receive Descriptor ring size: four possible values */
switch (_HME_NDESC /*XXX*/) {
case 32:
v |= HME_ERX_CFG_RINGSIZE32;
break;
case 64:
v |= HME_ERX_CFG_RINGSIZE64;
break;
case 128:
v |= HME_ERX_CFG_RINGSIZE128;
break;
case 256:
v |= HME_ERX_CFG_RINGSIZE256;
break;
default:
printf("hme: invalid Receive Descriptor ring size\n");
break;
}
/* Enable DMA */
v |= HME_ERX_CFG_DMAENABLE;
/* set h/w rx checksum start offset (# of half-words) */
#ifdef INET
v |= (((ETHER_HDR_LEN + sizeof(struct ip)) / sizeof(uint16_t))
<< HME_ERX_CFG_CSUMSHIFT) &
HME_ERX_CFG_CSUMSTART;
#endif
bus_space_write_4(t, erx, HME_ERXI_CFG, v);
/* step 11. XIF Configuration */
v = bus_space_read_4(t, mac, HME_MACI_XIF);
v |= HME_MAC_XIF_OE;
bus_space_write_4(t, mac, HME_MACI_XIF, v);
/*
* Routine to copy from mbuf chain to transmit buffer in
* network buffer memory.
* Returns the amount of data copied.
*/
int
hme_put(struct hme_softc *sc, int ri, struct mbuf *m)
/* ri: Ring index */
{
struct mbuf *n;
int len, tlen = 0;
char *bp;
bp = (char *)sc->sc_rb.rb_txbuf + (ri % sc->sc_rb.rb_ntbuf) * _HME_BUFSZ;
for (; m; m = n) {
len = m->m_len;
if (len == 0) {
n = m_free(m);
continue;
}
memcpy(bp, mtod(m, void *), len);
bp += len;
tlen += len;
n = m_free(m);
}
return (tlen);
}
/*
* Pull data off an interface.
* Len is length of data, with local net header stripped.
* We copy the data into mbufs. When full cluster sized units are present
* we copy into clusters.
*/
struct mbuf *
hme_get(struct hme_softc *sc, int ri, uint32_t flags)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
struct mbuf *m, *m0, *newm;
char *bp;
int len, totlen;
#ifdef INET
int csum_flags;
#endif
totlen = HME_XD_DECODE_RSIZE(flags);
MGETHDR(m0, M_DONTWAIT, MT_DATA);
if (m0 == 0)
return (0);
MCLAIM(m0, &sc->sc_ethercom.ec_rx_mowner);
m_set_rcvif(m0, ifp);
m0->m_pkthdr.len = totlen;
len = MHLEN;
m = m0;
bp = (char *)sc->sc_rb.rb_rxbuf + (ri % sc->sc_rb.rb_nrbuf) * _HME_BUFSZ;
while (totlen > 0) {
if (totlen >= MINCLSIZE) {
MCLGET(m, M_DONTWAIT);
if ((m->m_flags & M_EXT) == 0)
goto bad;
len = MCLBYTES;
}
/*
* bail if too short, has random trailing garbage, truncated,
* fragment, or has ethernet pad.
*/
if (ntohs(ip->ip_len) < hlen ||
ntohs(ip->ip_len) != pktlen ||
(ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) != 0)
goto swcsum;
switch (ip->ip_p) {
case IPPROTO_TCP:
if ((ifp->if_csum_flags_rx & M_CSUM_TCPv4) == 0)
goto swcsum;
if (pktlen < (hlen + sizeof(struct tcphdr)))
goto swcsum;
csum_flags =
M_CSUM_TCPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
break;
case IPPROTO_UDP:
if ((ifp->if_csum_flags_rx & M_CSUM_UDPv4) == 0)
goto swcsum;
if (pktlen < (hlen + sizeof(struct udphdr)))
goto swcsum;
uh = (struct udphdr *)((char *)ip + hlen);
/* no checksum */
if (uh->uh_sum == 0)
goto swcsum;
csum_flags =
M_CSUM_UDPv4 | M_CSUM_DATA | M_CSUM_NO_PSEUDOHDR;
break;
default:
goto swcsum;
}
/* w/ M_CSUM_NO_PSEUDOHDR, the uncomplemented sum is expected */
csum_data = ~flags & HME_XD_RXCKSUM;
/*
* If data offset is different from RX cksum start offset,
* we have to deduct them.
*/
hlen = ((char *)ip + hlen) -
((char *)eh + ETHER_HDR_LEN + sizeof(struct ip));
if (hlen > 1) {
uint32_t optsum;
/*
* Initialize the MII Management Interface
*/
void
hme_mifinit(struct hme_softc *sc)
{
bus_space_tag_t t = sc->sc_bustag;
bus_space_handle_t mif = sc->sc_mif;
bus_space_handle_t mac = sc->sc_mac;
int instance, phy;
uint32_t v;
if (sc->sc_mii.mii_media.ifm_cur != NULL) {
instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
phy = sc->sc_phys[instance];
} else
/* No media set yet, pick phy arbitrarily.. */
phy = HME_PHYAD_EXTERNAL;
/* Configure the MIF in frame mode, no poll, current phy select */
v = 0;
if (phy == HME_PHYAD_EXTERNAL)
v |= HME_MIF_CFG_PHY;
bus_space_write_4(t, mif, HME_MIFI_CFG, v);
/* If an external transceiver is selected, enable its MII drivers */
v = bus_space_read_4(t, mac, HME_MACI_XIF);
v &= ~HME_MAC_XIF_MIIENABLE;
if (phy == HME_PHYAD_EXTERNAL)
v |= HME_MAC_XIF_MIIENABLE;
bus_space_write_4(t, mac, HME_MACI_XIF, v);
}
/*
* MII interface
*/
static int
hme_mii_readreg(device_t self, int phy, int reg, uint16_t *val)
{
struct hme_softc *sc = device_private(self);
bus_space_tag_t t = sc->sc_bustag;
bus_space_handle_t mif = sc->sc_mif;
bus_space_handle_t mac = sc->sc_mac;
uint32_t v, xif_cfg, mifi_cfg;
int n, rv;
/* We can at most have two PHYs */
if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
return -1;
/* Select the desired PHY in the MIF configuration register */
v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
v &= ~HME_MIF_CFG_PHY;
if (phy == HME_PHYAD_EXTERNAL)
v |= HME_MIF_CFG_PHY;
bus_space_write_4(t, mif, HME_MIFI_CFG, v);
/* Enable MII drivers on external transceiver */
v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
if (phy == HME_PHYAD_EXTERNAL)
v |= HME_MAC_XIF_MIIENABLE;
else
v &= ~HME_MAC_XIF_MIIENABLE;
bus_space_write_4(t, mac, HME_MACI_XIF, v);
#if 0
/* This doesn't work reliably; the MDIO_1 bit is off most of the time */
/*
* Check whether a transceiver is connected by testing
* the MIF configuration register's MDI_X bits. Note that
* MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
*/
mif_mdi_bit = 1 << (8 + (1 - phy));
delay(100);
v = bus_space_read_4(t, mif, HME_MIFI_CFG);
if ((v & mif_mdi_bit) == 0) {
rv = -1;
goto out;
}
#endif
static int
hme_mii_writereg(device_t self, int phy, int reg, uint16_t val)
{
struct hme_softc *sc = device_private(self);
bus_space_tag_t t = sc->sc_bustag;
bus_space_handle_t mif = sc->sc_mif;
bus_space_handle_t mac = sc->sc_mac;
uint32_t v, xif_cfg, mifi_cfg;
int n, rv;
/* We can at most have two PHYs */
if (phy != HME_PHYAD_EXTERNAL && phy != HME_PHYAD_INTERNAL)
return -1;
/* Select the desired PHY in the MIF configuration register */
v = mifi_cfg = bus_space_read_4(t, mif, HME_MIFI_CFG);
v &= ~HME_MIF_CFG_PHY;
if (phy == HME_PHYAD_EXTERNAL)
v |= HME_MIF_CFG_PHY;
bus_space_write_4(t, mif, HME_MIFI_CFG, v);
/* Enable MII drivers on external transceiver */
v = xif_cfg = bus_space_read_4(t, mac, HME_MACI_XIF);
if (phy == HME_PHYAD_EXTERNAL)
v |= HME_MAC_XIF_MIIENABLE;
else
v &= ~HME_MAC_XIF_MIIENABLE;
bus_space_write_4(t, mac, HME_MACI_XIF, v);
#if 0
/* This doesn't work reliably; the MDIO_1 bit is off most of the time */
/*
* Check whether a transceiver is connected by testing
* the MIF configuration register's MDI_X bits. Note that
* MDI_0 (int) == 0x100 and MDI_1 (ext) == 0x200; see hmereg.h
*/
mif_mdi_bit = 1 << (8 + (1 - phy));
delay(100);
v = bus_space_read_4(t, mif, HME_MIFI_CFG);
if ((v & mif_mdi_bit) == 0) {
rv = -1;
goto out;
}
#endif
#ifdef HMEDEBUG
if (sc->sc_debug)
printf("hme_mii_statchg: status change\n");
#endif
/* Set the MAC Full Duplex bit appropriately */
/* Apparently the hme chip is SIMPLEX if working in full duplex mode,
but not otherwise. */
v = bus_space_read_4(t, mac, HME_MACI_TXCFG);
if ((IFM_OPTIONS(sc->sc_mii.mii_media_active) & IFM_FDX) != 0) {
v |= HME_MAC_TXCFG_FULLDPLX;
sc->sc_ethercom.ec_if.if_flags |= IFF_SIMPLEX;
} else {
v &= ~HME_MAC_TXCFG_FULLDPLX;
sc->sc_ethercom.ec_if.if_flags &= ~IFF_SIMPLEX;
}
sc->sc_if_flags = sc->sc_ethercom.ec_if.if_flags;
bus_space_write_4(t, mac, HME_MACI_TXCFG, v);
}
int
hme_mediachange(struct ifnet *ifp)
{
struct hme_softc *sc = ifp->if_softc;
bus_space_tag_t t = sc->sc_bustag;
bus_space_handle_t mif = sc->sc_mif;
bus_space_handle_t mac = sc->sc_mac;
int instance = IFM_INST(sc->sc_mii.mii_media.ifm_cur->ifm_media);
int phy = sc->sc_phys[instance];
int rc;
uint32_t v;
#ifdef HMEDEBUG
if (sc->sc_debug)
printf("hme_mediachange: phy = %d\n", phy);
#endif
/* Select the current PHY in the MIF configuration register */
v = bus_space_read_4(t, mif, HME_MIFI_CFG);
v &= ~HME_MIF_CFG_PHY;
if (phy == HME_PHYAD_EXTERNAL)
v |= HME_MIF_CFG_PHY;
bus_space_write_4(t, mif, HME_MIFI_CFG, v);
/* If an external transceiver is selected, enable its MII drivers */
v = bus_space_read_4(t, mac, HME_MACI_XIF);
v &= ~HME_MAC_XIF_MIIENABLE;
if (phy == HME_PHYAD_EXTERNAL)
v |= HME_MAC_XIF_MIIENABLE;
bus_space_write_4(t, mac, HME_MACI_XIF, v);
switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
case IFF_RUNNING:
/*
* If interface is marked down and it is running, then
* stop it.
*/
hme_stop(ifp, 0);
ifp->if_flags &= ~IFF_RUNNING;
break;
case IFF_UP:
/*
* If interface is marked up and it is stopped, then
* start it.
*/
error = hme_init(ifp);
break;
case IFF_UP | IFF_RUNNING:
/*
* If setting debug or promiscuous mode, do not reset
* the chip; for everything else, call hme_init()
* which will trigger a reset.
*/
#define RESETIGN (IFF_CANTCHANGE | IFF_DEBUG)
if (ifp->if_flags != sc->sc_if_flags) {
if ((ifp->if_flags & (~RESETIGN))
== (sc->sc_if_flags & (~RESETIGN)))
hme_setladrf(sc);
else
error = hme_init(ifp);
}
#undef RESETIGN
break;
case 0:
break;
}
if (sc->sc_ec_capenable != sc->sc_ethercom.ec_capenable)
error = hme_init(ifp);
break;
default:
if ((error = ether_ioctl(ifp, cmd, data)) != ENETRESET)
break;
error = 0;
if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
;
else if (ifp->if_flags & IFF_RUNNING) {
/*
* Multicast list has changed; set the hardware filter
* accordingly.
*/
hme_setladrf(sc);
}
break;
}
/* Get current RX configuration */
v = bus_space_read_4(t, mac, HME_MACI_RXCFG);
if ((ifp->if_flags & IFF_PROMISC) != 0) {
/* Turn on promiscuous mode; turn off the hash filter */
v |= HME_MAC_RXCFG_PMISC;
v &= ~HME_MAC_RXCFG_HENABLE;
ifp->if_flags |= IFF_ALLMULTI;
goto chipit;
}
/* Turn off promiscuous mode; turn on the hash filter */
v &= ~HME_MAC_RXCFG_PMISC;
v |= HME_MAC_RXCFG_HENABLE;
/*
* Set up multicast address filter by passing all multicast addresses
* through a crc generator, and then using the high order 6 bits as an
* index into the 64 bit logical address filter. The high order bit
* selects the word, while the rest of the bits select the bit within
* the word.
*/
ETHER_LOCK(ec);
ETHER_FIRST_MULTI(step, ec, enm);
while (enm != NULL) {
if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
/*
* We must listen to a range of multicast addresses.
* For now, just accept all multicasts, rather than
* trying to set only those filter bits needed to match
* the range. (At this time, the only use of address
* ranges is for IP multicast routing, for which the
* range is big enough to require all bits set.)
*/
hash[3] = hash[2] = hash[1] = hash[0] = 0xffff;
ifp->if_flags |= IFF_ALLMULTI;
ETHER_UNLOCK(ec);
goto chipit;
}
/* Just want the 6 most significant bits. */
crc >>= 26;
/* Set the corresponding bit in the filter. */
hash[crc >> 4] |= 1 << (crc & 0xf);
ETHER_NEXT_MULTI(step, enm);
}
ETHER_UNLOCK(ec);
ifp->if_flags &= ~IFF_ALLMULTI;
chipit:
/* Now load the hash table into the chip */
bus_space_write_4(t, mac, HME_MACI_HASHTAB0, hash[0]);
bus_space_write_4(t, mac, HME_MACI_HASHTAB1, hash[1]);
bus_space_write_4(t, mac, HME_MACI_HASHTAB2, hash[2]);
bus_space_write_4(t, mac, HME_MACI_HASHTAB3, hash[3]);
bus_space_write_4(t, mac, HME_MACI_RXCFG, v);
}
/*
* Routines for accessing the transmit and receive buffers.
* The various CPU and adapter configurations supported by this
* driver require three different access methods for buffers
* and descriptors:
* (1) contig (contiguous data; no padding),
* (2) gap2 (two bytes of data followed by two bytes of padding),
* (3) gap16 (16 bytes of data followed by 16 bytes of padding).
*/
#if 0
/*
* contig: contiguous data with no padding.
*
* Buffers may have any alignment.
*/
void
hme_copytobuf_contig(struct hme_softc *sc, void *from, int ri, int len)
{
volatile void *buf = sc->sc_rb.rb_txbuf + (ri * _HME_BUFSZ);
/*
* Just call memcpy() to do the work.
*/
memcpy(buf, from, len);
}
void
hme_copyfrombuf_contig(struct hme_softc *sc, void *to, int boff, int len)
{
volatile void *buf = sc->sc_rb.rb_rxbuf + (ri * _HME_BUFSZ);
/*
* Just call memcpy() to do the work.
*/
memcpy(to, buf, len);
}
#endif