#include <sys/cdefs.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/rman.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <machine/bus.h>
#include <net/bpf.h>
#include <net/if.h>
#include <net/ethernet.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <net/if_var.h>
#include <net/if_vlan_var.h>
#include <dev/fdt/fdt_common.h>
#include <dev/ffec/if_ffecreg.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/mii/mii.h>
#include <dev/mii/miivar.h>
#include <dev/mii/mii_fdt.h>
#include "miibus_if.h"
enum {
FECTYPE_NONE,
FECTYPE_GENERIC,
FECTYPE_IMX53,
FECTYPE_IMX6,
FECTYPE_MVF,
};
#define FECTYPE_MASK 0x000000ff
#define FECFLAG_GBE (1 << 8)
#define FECFLAG_AVB (1 << 9)
#define FECFLAG_RACC (1 << 10)
static struct ofw_compat_data compat_data[] = {
{"fsl,imx51-fec", FECTYPE_GENERIC},
{"fsl,imx53-fec", FECTYPE_IMX53},
{"fsl,imx6q-fec", FECTYPE_IMX6 | FECFLAG_RACC | FECFLAG_GBE },
{"fsl,imx6ul-fec", FECTYPE_IMX6 | FECFLAG_RACC },
{"fsl,imx6sx-fec", FECTYPE_IMX6 | FECFLAG_RACC | FECFLAG_GBE |
FECFLAG_AVB },
{"fsl,imx7d-fec", FECTYPE_IMX6 | FECFLAG_RACC | FECFLAG_GBE |
FECFLAG_AVB },
{"fsl,mvf600-fec", FECTYPE_MVF | FECFLAG_RACC },
{"fsl,mvf-fec", FECTYPE_MVF},
{NULL, FECTYPE_NONE},
};
#define RX_DESC_COUNT 64
#define RX_DESC_SIZE (sizeof(struct ffec_hwdesc) * RX_DESC_COUNT)
#define TX_DESC_COUNT 64
#define TX_DESC_SIZE (sizeof(struct ffec_hwdesc) * TX_DESC_COUNT)
#define WATCHDOG_TIMEOUT_SECS 5
#define MAX_IRQ_COUNT 3
struct ffec_bufmap {
struct mbuf *mbuf;
bus_dmamap_t map;
};
struct ffec_softc {
device_t dev;
device_t miibus;
struct mii_data * mii_softc;
if_t ifp;
int if_flags;
struct mtx mtx;
struct resource *irq_res[MAX_IRQ_COUNT];
struct resource *mem_res;
void * intr_cookie[MAX_IRQ_COUNT];
struct callout ffec_callout;
mii_contype_t phy_conn_type;
uint32_t fecflags;
uint8_t fectype;
boolean_t link_is_up;
boolean_t is_attached;
boolean_t is_detaching;
int tx_watchdog_count;
int rxbuf_align;
int txbuf_align;
bus_dma_tag_t rxdesc_tag;
bus_dmamap_t rxdesc_map;
struct ffec_hwdesc *rxdesc_ring;
bus_addr_t rxdesc_ring_paddr;
bus_dma_tag_t rxbuf_tag;
struct ffec_bufmap rxbuf_map[RX_DESC_COUNT];
uint32_t rx_idx;
bus_dma_tag_t txdesc_tag;
bus_dmamap_t txdesc_map;
struct ffec_hwdesc *txdesc_ring;
bus_addr_t txdesc_ring_paddr;
bus_dma_tag_t txbuf_tag;
struct ffec_bufmap txbuf_map[TX_DESC_COUNT];
uint32_t tx_idx_head;
uint32_t tx_idx_tail;
int txcount;
};
static struct resource_spec irq_res_spec[MAX_IRQ_COUNT + 1] = {
{ SYS_RES_IRQ, 0, RF_ACTIVE },
{ SYS_RES_IRQ, 1, RF_ACTIVE | RF_OPTIONAL },
{ SYS_RES_IRQ, 2, RF_ACTIVE | RF_OPTIONAL },
RESOURCE_SPEC_END
};
#define FFEC_LOCK(sc) mtx_lock(&(sc)->mtx)
#define FFEC_UNLOCK(sc) mtx_unlock(&(sc)->mtx)
#define FFEC_LOCK_INIT(sc) mtx_init(&(sc)->mtx, \
device_get_nameunit((sc)->dev), MTX_NETWORK_LOCK, MTX_DEF)
#define FFEC_LOCK_DESTROY(sc) mtx_destroy(&(sc)->mtx);
#define FFEC_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED);
#define FFEC_ASSERT_UNLOCKED(sc) mtx_assert(&(sc)->mtx, MA_NOTOWNED);
static void ffec_init_locked(struct ffec_softc *sc);
static void ffec_stop_locked(struct ffec_softc *sc);
static void ffec_txstart_locked(struct ffec_softc *sc);
static void ffec_txfinish_locked(struct ffec_softc *sc);
static inline uint16_t
RD2(struct ffec_softc *sc, bus_size_t off)
{
return (bus_read_2(sc->mem_res, off));
}
static inline void
WR2(struct ffec_softc *sc, bus_size_t off, uint16_t val)
{
bus_write_2(sc->mem_res, off, val);
}
static inline uint32_t
RD4(struct ffec_softc *sc, bus_size_t off)
{
return (bus_read_4(sc->mem_res, off));
}
static inline void
WR4(struct ffec_softc *sc, bus_size_t off, uint32_t val)
{
bus_write_4(sc->mem_res, off, val);
}
static inline uint32_t
next_rxidx(struct ffec_softc *sc, uint32_t curidx)
{
return ((curidx == RX_DESC_COUNT - 1) ? 0 : curidx + 1);
}
static inline uint32_t
next_txidx(struct ffec_softc *sc, uint32_t curidx)
{
return ((curidx == TX_DESC_COUNT - 1) ? 0 : curidx + 1);
}
static void
ffec_get1paddr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
{
if (error != 0)
return;
*(bus_addr_t *)arg = segs[0].ds_addr;
}
static void
ffec_miigasket_setup(struct ffec_softc *sc)
{
uint32_t ifmode;
switch (sc->fectype)
{
case FECTYPE_IMX53:
break;
default:
return;
}
switch (sc->phy_conn_type)
{
case MII_CONTYPE_MII:
ifmode = 0;
break;
case MII_CONTYPE_RMII:
ifmode = FEC_MIIGSK_CFGR_IF_MODE_RMII;
break;
default:
return;
}
WR2(sc, FEC_MIIGSK_ENR, 0);
while (RD2(sc, FEC_MIIGSK_ENR) & FEC_MIIGSK_ENR_READY)
continue;
WR2(sc, FEC_MIIGSK_CFGR, ifmode);
WR2(sc, FEC_MIIGSK_ENR, FEC_MIIGSK_ENR_EN);
while (!(RD2(sc, FEC_MIIGSK_ENR) & FEC_MIIGSK_ENR_READY))
continue;
}
static boolean_t
ffec_miibus_iowait(struct ffec_softc *sc)
{
uint32_t timeout;
for (timeout = 10000; timeout != 0; --timeout)
if (RD4(sc, FEC_IER_REG) & FEC_IER_MII)
return (true);
return (false);
}
static int
ffec_miibus_readreg(device_t dev, int phy, int reg)
{
struct ffec_softc *sc;
int val;
sc = device_get_softc(dev);
WR4(sc, FEC_IER_REG, FEC_IER_MII);
WR4(sc, FEC_MMFR_REG, FEC_MMFR_OP_READ |
FEC_MMFR_ST_VALUE | FEC_MMFR_TA_VALUE |
((phy << FEC_MMFR_PA_SHIFT) & FEC_MMFR_PA_MASK) |
((reg << FEC_MMFR_RA_SHIFT) & FEC_MMFR_RA_MASK));
if (!ffec_miibus_iowait(sc)) {
device_printf(dev, "timeout waiting for mii read\n");
return (-1);
}
val = RD4(sc, FEC_MMFR_REG) & FEC_MMFR_DATA_MASK;
return (val);
}
static int
ffec_miibus_writereg(device_t dev, int phy, int reg, int val)
{
struct ffec_softc *sc;
sc = device_get_softc(dev);
WR4(sc, FEC_IER_REG, FEC_IER_MII);
WR4(sc, FEC_MMFR_REG, FEC_MMFR_OP_WRITE |
FEC_MMFR_ST_VALUE | FEC_MMFR_TA_VALUE |
((phy << FEC_MMFR_PA_SHIFT) & FEC_MMFR_PA_MASK) |
((reg << FEC_MMFR_RA_SHIFT) & FEC_MMFR_RA_MASK) |
(val & FEC_MMFR_DATA_MASK));
if (!ffec_miibus_iowait(sc)) {
device_printf(dev, "timeout waiting for mii write\n");
return (-1);
}
return (0);
}
static void
ffec_miibus_statchg(device_t dev)
{
struct ffec_softc *sc;
struct mii_data *mii;
uint32_t ecr, rcr, tcr;
sc = device_get_softc(dev);
FFEC_ASSERT_LOCKED(sc);
mii = sc->mii_softc;
if (mii->mii_media_status & IFM_ACTIVE)
sc->link_is_up = true;
else
sc->link_is_up = false;
ecr = RD4(sc, FEC_ECR_REG) & ~FEC_ECR_SPEED;
rcr = RD4(sc, FEC_RCR_REG) & ~(FEC_RCR_RMII_10T | FEC_RCR_RMII_MODE |
FEC_RCR_RGMII_EN | FEC_RCR_DRT | FEC_RCR_FCE);
tcr = RD4(sc, FEC_TCR_REG) & ~FEC_TCR_FDEN;
rcr |= FEC_RCR_MII_MODE;
switch (sc->phy_conn_type) {
case MII_CONTYPE_RMII:
rcr |= FEC_RCR_RMII_MODE;
break;
case MII_CONTYPE_RGMII:
case MII_CONTYPE_RGMII_ID:
case MII_CONTYPE_RGMII_RXID:
case MII_CONTYPE_RGMII_TXID:
rcr |= FEC_RCR_RGMII_EN;
break;
default:
break;
}
switch (IFM_SUBTYPE(mii->mii_media_active)) {
case IFM_1000_T:
case IFM_1000_SX:
ecr |= FEC_ECR_SPEED;
break;
case IFM_100_TX:
break;
case IFM_10_T:
rcr |= FEC_RCR_RMII_10T;
break;
case IFM_NONE:
sc->link_is_up = false;
return;
default:
sc->link_is_up = false;
device_printf(dev, "Unsupported media %u\n",
IFM_SUBTYPE(mii->mii_media_active));
return;
}
if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0)
tcr |= FEC_TCR_FDEN;
else
rcr |= FEC_RCR_DRT;
if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FLOW) != 0)
rcr |= FEC_RCR_FCE;
WR4(sc, FEC_RCR_REG, rcr);
WR4(sc, FEC_TCR_REG, tcr);
WR4(sc, FEC_ECR_REG, ecr);
}
static void
ffec_media_status(if_t ifp, struct ifmediareq *ifmr)
{
struct ffec_softc *sc;
struct mii_data *mii;
sc = if_getsoftc(ifp);
mii = sc->mii_softc;
FFEC_LOCK(sc);
mii_pollstat(mii);
ifmr->ifm_active = mii->mii_media_active;
ifmr->ifm_status = mii->mii_media_status;
FFEC_UNLOCK(sc);
}
static int
ffec_media_change_locked(struct ffec_softc *sc)
{
return (mii_mediachg(sc->mii_softc));
}
static int
ffec_media_change(if_t ifp)
{
struct ffec_softc *sc;
int error;
sc = if_getsoftc(ifp);
FFEC_LOCK(sc);
error = ffec_media_change_locked(sc);
FFEC_UNLOCK(sc);
return (error);
}
static void ffec_clear_stats(struct ffec_softc *sc)
{
uint32_t mibc;
mibc = RD4(sc, FEC_MIBC_REG);
if (sc->fectype == FECTYPE_IMX6 || sc->fectype == FECTYPE_MVF) {
WR4(sc, FEC_MIBC_REG, mibc | FEC_MIBC_CLEAR);
WR4(sc, FEC_MIBC_REG, mibc & ~FEC_MIBC_CLEAR);
} else {
WR4(sc, FEC_MIBC_REG, mibc | FEC_MIBC_DIS);
WR4(sc, FEC_IEEE_R_DROP, 0);
WR4(sc, FEC_IEEE_R_MACERR, 0);
WR4(sc, FEC_RMON_R_CRC_ALIGN, 0);
WR4(sc, FEC_RMON_R_FRAG, 0);
WR4(sc, FEC_RMON_R_JAB, 0);
WR4(sc, FEC_RMON_R_MC_PKT, 0);
WR4(sc, FEC_RMON_R_OVERSIZE, 0);
WR4(sc, FEC_RMON_R_PACKETS, 0);
WR4(sc, FEC_RMON_R_UNDERSIZE, 0);
WR4(sc, FEC_RMON_T_COL, 0);
WR4(sc, FEC_RMON_T_CRC_ALIGN, 0);
WR4(sc, FEC_RMON_T_FRAG, 0);
WR4(sc, FEC_RMON_T_JAB, 0);
WR4(sc, FEC_RMON_T_MC_PKT, 0);
WR4(sc, FEC_RMON_T_OVERSIZE , 0);
WR4(sc, FEC_RMON_T_PACKETS, 0);
WR4(sc, FEC_RMON_T_UNDERSIZE, 0);
WR4(sc, FEC_MIBC_REG, mibc);
}
}
static void
ffec_harvest_stats(struct ffec_softc *sc)
{
if_t ifp;
ifp = sc->ifp;
if_inc_counter(ifp, IFCOUNTER_IPACKETS, RD4(sc, FEC_RMON_R_PACKETS));
if_inc_counter(ifp, IFCOUNTER_IMCASTS, RD4(sc, FEC_RMON_R_MC_PKT));
if_inc_counter(ifp, IFCOUNTER_IERRORS,
RD4(sc, FEC_RMON_R_CRC_ALIGN) + RD4(sc, FEC_RMON_R_UNDERSIZE) +
RD4(sc, FEC_RMON_R_OVERSIZE) + RD4(sc, FEC_RMON_R_FRAG) +
RD4(sc, FEC_RMON_R_JAB) + RD4(sc, FEC_IEEE_R_DROP));
if_inc_counter(ifp, IFCOUNTER_IQDROPS, RD4(sc, FEC_IEEE_R_MACERR));
if_inc_counter(ifp, IFCOUNTER_OPACKETS, RD4(sc, FEC_RMON_T_PACKETS));
if_inc_counter(ifp, IFCOUNTER_OMCASTS, RD4(sc, FEC_RMON_T_MC_PKT));
if_inc_counter(ifp, IFCOUNTER_OERRORS,
RD4(sc, FEC_RMON_T_CRC_ALIGN) + RD4(sc, FEC_RMON_T_UNDERSIZE) +
RD4(sc, FEC_RMON_T_OVERSIZE) + RD4(sc, FEC_RMON_T_FRAG) +
RD4(sc, FEC_RMON_T_JAB));
if_inc_counter(ifp, IFCOUNTER_COLLISIONS, RD4(sc, FEC_RMON_T_COL));
ffec_clear_stats(sc);
}
static void
ffec_tick(void *arg)
{
struct ffec_softc *sc;
if_t ifp;
int link_was_up;
sc = arg;
FFEC_ASSERT_LOCKED(sc);
ifp = sc->ifp;
if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING))
return;
if (sc->tx_watchdog_count > 0) {
if (--sc->tx_watchdog_count == 0) {
ffec_txfinish_locked(sc);
}
}
ffec_harvest_stats(sc);
link_was_up = sc->link_is_up;
mii_tick(sc->mii_softc);
if (sc->link_is_up && !link_was_up)
ffec_txstart_locked(sc);
callout_reset(&sc->ffec_callout, hz, ffec_tick, sc);
}
inline static uint32_t
ffec_setup_txdesc(struct ffec_softc *sc, int idx, bus_addr_t paddr,
uint32_t len)
{
uint32_t nidx;
uint32_t flags;
nidx = next_txidx(sc, idx);
if (paddr == 0 || len == 0) {
flags = 0;
--sc->txcount;
} else {
flags = FEC_TXDESC_READY | FEC_TXDESC_L | FEC_TXDESC_TC;
++sc->txcount;
}
if (nidx == 0)
flags |= FEC_TXDESC_WRAP;
sc->txdesc_ring[idx].buf_paddr = (uint32_t)paddr;
sc->txdesc_ring[idx].flags_len = flags | len;
return (nidx);
}
static int
ffec_setup_txbuf(struct ffec_softc *sc, int idx, struct mbuf **mp)
{
struct mbuf * m;
int error, nsegs;
struct bus_dma_segment seg;
if ((m = m_defrag(*mp, M_NOWAIT)) == NULL)
return (ENOMEM);
*mp = m;
error = bus_dmamap_load_mbuf_sg(sc->txbuf_tag, sc->txbuf_map[idx].map,
m, &seg, &nsegs, 0);
if (error != 0) {
return (ENOMEM);
}
bus_dmamap_sync(sc->txbuf_tag, sc->txbuf_map[idx].map,
BUS_DMASYNC_PREWRITE);
sc->txbuf_map[idx].mbuf = m;
ffec_setup_txdesc(sc, idx, seg.ds_addr, seg.ds_len);
return (0);
}
static void
ffec_txstart_locked(struct ffec_softc *sc)
{
if_t ifp;
struct mbuf *m;
int enqueued;
FFEC_ASSERT_LOCKED(sc);
if (!sc->link_is_up)
return;
ifp = sc->ifp;
if (if_getdrvflags(ifp) & IFF_DRV_OACTIVE)
return;
enqueued = 0;
for (;;) {
if (sc->txcount == (TX_DESC_COUNT-1)) {
if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
break;
}
m = if_dequeue(ifp);
if (m == NULL)
break;
if (ffec_setup_txbuf(sc, sc->tx_idx_head, &m) != 0) {
if_sendq_prepend(ifp, m);
break;
}
BPF_MTAP(ifp, m);
sc->tx_idx_head = next_txidx(sc, sc->tx_idx_head);
++enqueued;
}
if (enqueued != 0) {
bus_dmamap_sync(sc->txdesc_tag, sc->txdesc_map, BUS_DMASYNC_PREWRITE);
WR4(sc, FEC_TDAR_REG, FEC_TDAR_TDAR);
bus_dmamap_sync(sc->txdesc_tag, sc->txdesc_map, BUS_DMASYNC_POSTWRITE);
sc->tx_watchdog_count = WATCHDOG_TIMEOUT_SECS;
}
}
static void
ffec_txstart(if_t ifp)
{
struct ffec_softc *sc = if_getsoftc(ifp);
FFEC_LOCK(sc);
ffec_txstart_locked(sc);
FFEC_UNLOCK(sc);
}
static void
ffec_txfinish_locked(struct ffec_softc *sc)
{
if_t ifp;
struct ffec_hwdesc *desc;
struct ffec_bufmap *bmap;
boolean_t retired_buffer;
FFEC_ASSERT_LOCKED(sc);
bus_dmamap_sync(sc->txdesc_tag, sc->txdesc_map, BUS_DMASYNC_PREREAD);
bus_dmamap_sync(sc->txdesc_tag, sc->txdesc_map, BUS_DMASYNC_POSTREAD);
ifp = sc->ifp;
retired_buffer = false;
while (sc->tx_idx_tail != sc->tx_idx_head) {
desc = &sc->txdesc_ring[sc->tx_idx_tail];
if (desc->flags_len & FEC_TXDESC_READY)
break;
retired_buffer = true;
bmap = &sc->txbuf_map[sc->tx_idx_tail];
bus_dmamap_sync(sc->txbuf_tag, bmap->map,
BUS_DMASYNC_POSTWRITE);
bus_dmamap_unload(sc->txbuf_tag, bmap->map);
m_freem(bmap->mbuf);
bmap->mbuf = NULL;
ffec_setup_txdesc(sc, sc->tx_idx_tail, 0, 0);
sc->tx_idx_tail = next_txidx(sc, sc->tx_idx_tail);
}
if (retired_buffer) {
if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
ffec_txstart_locked(sc);
}
if (sc->tx_idx_tail == sc->tx_idx_head) {
sc->tx_watchdog_count = 0;
}
}
inline static uint32_t
ffec_setup_rxdesc(struct ffec_softc *sc, int idx, bus_addr_t paddr)
{
uint32_t nidx;
nidx = next_rxidx(sc, idx);
sc->rxdesc_ring[idx].buf_paddr = (uint32_t)paddr;
sc->rxdesc_ring[idx].flags_len = FEC_RXDESC_EMPTY |
((nidx == 0) ? FEC_RXDESC_WRAP : 0);
return (nidx);
}
static int
ffec_setup_rxbuf(struct ffec_softc *sc, int idx, struct mbuf * m)
{
int error, nsegs;
struct bus_dma_segment seg;
if (!(sc->fecflags & FECFLAG_RACC)) {
m_adj(m, roundup(ETHER_ALIGN, sc->rxbuf_align));
}
error = bus_dmamap_load_mbuf_sg(sc->rxbuf_tag, sc->rxbuf_map[idx].map,
m, &seg, &nsegs, 0);
if (error != 0) {
return (error);
}
bus_dmamap_sync(sc->rxbuf_tag, sc->rxbuf_map[idx].map,
BUS_DMASYNC_PREREAD);
sc->rxbuf_map[idx].mbuf = m;
ffec_setup_rxdesc(sc, idx, seg.ds_addr);
return (0);
}
static struct mbuf *
ffec_alloc_mbufcl(struct ffec_softc *sc)
{
struct mbuf *m;
m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
if (m != NULL)
m->m_pkthdr.len = m->m_len = m->m_ext.ext_size;
return (m);
}
static void
ffec_rxfinish_onebuf(struct ffec_softc *sc, int len)
{
struct mbuf *m, *newmbuf;
struct ffec_bufmap *bmap;
uint8_t *dst, *src;
int error;
if ((newmbuf = ffec_alloc_mbufcl(sc)) == NULL) {
if_inc_counter(sc->ifp, IFCOUNTER_IQDROPS, 1);
ffec_setup_rxdesc(sc, sc->rx_idx,
sc->rxdesc_ring[sc->rx_idx].buf_paddr);
return;
}
FFEC_UNLOCK(sc);
bmap = &sc->rxbuf_map[sc->rx_idx];
len -= ETHER_CRC_LEN;
bus_dmamap_sync(sc->rxbuf_tag, bmap->map, BUS_DMASYNC_POSTREAD);
bus_dmamap_unload(sc->rxbuf_tag, bmap->map);
m = bmap->mbuf;
bmap->mbuf = NULL;
m->m_len = len;
m->m_pkthdr.len = len;
m->m_pkthdr.rcvif = sc->ifp;
if (sc->fecflags & FECFLAG_RACC) {
m->m_data = mtod(m, uint8_t *) + 2;
} else {
src = mtod(m, uint8_t*);
dst = src - ETHER_ALIGN;
bcopy(src, dst, len);
m->m_data = dst;
}
if_input(sc->ifp, m);
FFEC_LOCK(sc);
if ((error = ffec_setup_rxbuf(sc, sc->rx_idx, newmbuf)) != 0) {
device_printf(sc->dev, "ffec_setup_rxbuf error %d\n", error);
}
}
static void
ffec_rxfinish_locked(struct ffec_softc *sc)
{
struct ffec_hwdesc *desc;
int len;
boolean_t produced_empty_buffer;
FFEC_ASSERT_LOCKED(sc);
bus_dmamap_sync(sc->rxdesc_tag, sc->rxdesc_map, BUS_DMASYNC_PREREAD);
bus_dmamap_sync(sc->rxdesc_tag, sc->rxdesc_map, BUS_DMASYNC_POSTREAD);
produced_empty_buffer = false;
for (;;) {
desc = &sc->rxdesc_ring[sc->rx_idx];
if (desc->flags_len & FEC_RXDESC_EMPTY)
break;
produced_empty_buffer = true;
len = (desc->flags_len & FEC_RXDESC_LEN_MASK);
if (len < 64) {
ffec_setup_rxdesc(sc, sc->rx_idx,
sc->rxdesc_ring[sc->rx_idx].buf_paddr);
} else if ((desc->flags_len & FEC_RXDESC_L) == 0) {
device_printf(sc->dev,
"fec_rxfinish: received frame without LAST bit set");
ffec_setup_rxdesc(sc, sc->rx_idx,
sc->rxdesc_ring[sc->rx_idx].buf_paddr);
} else if (desc->flags_len & FEC_RXDESC_ERROR_BITS) {
ffec_setup_rxdesc(sc, sc->rx_idx,
sc->rxdesc_ring[sc->rx_idx].buf_paddr);
} else {
ffec_rxfinish_onebuf(sc, len);
}
sc->rx_idx = next_rxidx(sc, sc->rx_idx);
}
if (produced_empty_buffer) {
bus_dmamap_sync(sc->rxdesc_tag, sc->rxdesc_map, BUS_DMASYNC_PREWRITE);
WR4(sc, FEC_RDAR_REG, FEC_RDAR_RDAR);
bus_dmamap_sync(sc->rxdesc_tag, sc->rxdesc_map, BUS_DMASYNC_POSTWRITE);
}
}
static void
ffec_get_hwaddr(struct ffec_softc *sc, uint8_t *hwaddr)
{
uint32_t palr, paur, rnd;
palr = RD4(sc, FEC_PALR_REG);
paur = RD4(sc, FEC_PAUR_REG) & FEC_PAUR_PADDR2_MASK;
if ((palr | paur) != 0) {
hwaddr[0] = palr >> 24;
hwaddr[1] = palr >> 16;
hwaddr[2] = palr >> 8;
hwaddr[3] = palr >> 0;
hwaddr[4] = paur >> 24;
hwaddr[5] = paur >> 16;
} else {
rnd = arc4random() & 0x00ffffff;
hwaddr[0] = 'b';
hwaddr[1] = 's';
hwaddr[2] = 'd';
hwaddr[3] = rnd >> 16;
hwaddr[4] = rnd >> 8;
hwaddr[5] = rnd >> 0;
}
if (bootverbose) {
device_printf(sc->dev,
"MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
hwaddr[0], hwaddr[1], hwaddr[2],
hwaddr[3], hwaddr[4], hwaddr[5]);
}
}
static u_int
ffec_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
{
uint64_t *ghash = arg;
uint32_t crc;
crc = ether_crc32_le(LLADDR(sdl), ETHER_ADDR_LEN);
*ghash |= 1LLU << (((uint8_t *)&crc)[3] >> 2);
return (1);
}
static void
ffec_setup_rxfilter(struct ffec_softc *sc)
{
if_t ifp;
uint8_t *eaddr;
uint64_t ghash, ihash;
FFEC_ASSERT_LOCKED(sc);
ifp = sc->ifp;
if ((if_getflags(ifp) & IFF_ALLMULTI))
ghash = 0xffffffffffffffffLLU;
else {
ghash = 0;
if_foreach_llmaddr(ifp, ffec_hash_maddr, &ghash);
}
WR4(sc, FEC_GAUR_REG, (uint32_t)(ghash >> 32));
WR4(sc, FEC_GALR_REG, (uint32_t)ghash);
if ((if_getflags(ifp) & IFF_PROMISC))
ihash = 0xffffffffffffffffLLU;
else {
ihash = 0;
}
WR4(sc, FEC_IAUR_REG, (uint32_t)(ihash >> 32));
WR4(sc, FEC_IALR_REG, (uint32_t)ihash);
eaddr = if_getlladdr(ifp);
WR4(sc, FEC_PALR_REG, (eaddr[0] << 24) | (eaddr[1] << 16) |
(eaddr[2] << 8) | eaddr[3]);
WR4(sc, FEC_PAUR_REG, (eaddr[4] << 24) | (eaddr[5] << 16));
}
static void
ffec_stop_locked(struct ffec_softc *sc)
{
if_t ifp;
struct ffec_hwdesc *desc;
struct ffec_bufmap *bmap;
int idx;
FFEC_ASSERT_LOCKED(sc);
ifp = sc->ifp;
if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
sc->tx_watchdog_count = 0;
WR4(sc, FEC_ECR_REG, RD4(sc, FEC_ECR_REG) & ~FEC_ECR_ETHEREN);
WR4(sc, FEC_IEM_REG, 0x00000000);
WR4(sc, FEC_IER_REG, 0xffffffff);
callout_stop(&sc->ffec_callout);
idx = sc->tx_idx_tail;
while (idx != sc->tx_idx_head) {
desc = &sc->txdesc_ring[idx];
bmap = &sc->txbuf_map[idx];
if (desc->buf_paddr != 0) {
bus_dmamap_unload(sc->txbuf_tag, bmap->map);
m_freem(bmap->mbuf);
bmap->mbuf = NULL;
ffec_setup_txdesc(sc, idx, 0, 0);
}
idx = next_txidx(sc, idx);
}
for (idx = 0; idx < RX_DESC_COUNT; ++idx) {
desc = &sc->rxdesc_ring[idx];
ffec_setup_rxdesc(sc, idx, desc->buf_paddr);
}
}
static void
ffec_init_locked(struct ffec_softc *sc)
{
if_t ifp = sc->ifp;
uint32_t maxbuf, maxfl, regval;
FFEC_ASSERT_LOCKED(sc);
maxbuf = MCLBYTES - roundup(ETHER_ALIGN, sc->rxbuf_align);
maxfl = min(maxbuf, 0x7ff);
if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
return;
WR4(sc, FEC_IEM_REG, 0x00000000);
WR4(sc, FEC_IER_REG, 0xffffffff);
ffec_setup_rxfilter(sc);
WR4(sc, FEC_TFWR_REG, FEC_TFWR_STRFWD | FEC_TFWR_TWFR_128BYTE);
WR4(sc, FEC_RCR_REG, (maxfl << FEC_RCR_MAX_FL_SHIFT));
WR4(sc, FEC_TCR_REG, 0);
WR4(sc, FEC_OPD_REG, 0x00010020);
WR4(sc, FEC_MRBR_REG, maxfl << FEC_MRBR_R_BUF_SIZE_SHIFT);
WR4(sc, FEC_FTRL_REG, maxfl);
sc->rx_idx = 0;
sc->tx_idx_head = sc->tx_idx_tail = 0;
sc->txcount = 0;
WR4(sc, FEC_RDSR_REG, sc->rxdesc_ring_paddr);
WR4(sc, FEC_TDSR_REG, sc->txdesc_ring_paddr);
WR4(sc, FEC_IEM_REG, FEC_IER_TXF | FEC_IER_RXF | FEC_IER_EBERR);
regval = RD4(sc, FEC_MIBC_REG);
WR4(sc, FEC_MIBC_REG, regval | FEC_MIBC_DIS);
ffec_clear_stats(sc);
WR4(sc, FEC_MIBC_REG, regval & ~FEC_MIBC_DIS);
if (sc->fecflags & FECFLAG_RACC) {
regval = RD4(sc, FEC_RACC_REG);
WR4(sc, FEC_RACC_REG, regval | FEC_RACC_SHIFT16);
}
regval = RD4(sc, FEC_ECR_REG);
#if _BYTE_ORDER == _LITTLE_ENDIAN
regval |= FEC_ECR_DBSWP;
#endif
regval |= FEC_ECR_ETHEREN;
WR4(sc, FEC_ECR_REG, regval);
if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
mii_mediachg(sc->mii_softc);
callout_reset(&sc->ffec_callout, hz, ffec_tick, sc);
WR4(sc, FEC_RDAR_REG, FEC_RDAR_RDAR);
}
static void
ffec_init(void *if_softc)
{
struct ffec_softc *sc = if_softc;
FFEC_LOCK(sc);
ffec_init_locked(sc);
FFEC_UNLOCK(sc);
}
static void
ffec_intr(void *arg)
{
struct ffec_softc *sc;
uint32_t ier;
sc = arg;
FFEC_LOCK(sc);
ier = RD4(sc, FEC_IER_REG);
if (ier & FEC_IER_TXF) {
WR4(sc, FEC_IER_REG, FEC_IER_TXF);
ffec_txfinish_locked(sc);
}
if (ier & FEC_IER_RXF) {
WR4(sc, FEC_IER_REG, FEC_IER_RXF);
ffec_rxfinish_locked(sc);
}
if (ier & FEC_IER_EBERR) {
WR4(sc, FEC_IER_REG, FEC_IER_EBERR);
device_printf(sc->dev,
"Ethernet DMA error, restarting controller.\n");
ffec_stop_locked(sc);
ffec_init_locked(sc);
}
FFEC_UNLOCK(sc);
}
static int
ffec_ioctl(if_t ifp, u_long cmd, caddr_t data)
{
struct ffec_softc *sc;
struct mii_data *mii;
struct ifreq *ifr;
int mask, error;
sc = if_getsoftc(ifp);
ifr = (struct ifreq *)data;
error = 0;
switch (cmd) {
case SIOCSIFFLAGS:
FFEC_LOCK(sc);
if (if_getflags(ifp) & IFF_UP) {
if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
if ((if_getflags(ifp) ^ sc->if_flags) &
(IFF_PROMISC | IFF_ALLMULTI))
ffec_setup_rxfilter(sc);
} else {
if (!sc->is_detaching)
ffec_init_locked(sc);
}
} else {
if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
ffec_stop_locked(sc);
}
sc->if_flags = if_getflags(ifp);
FFEC_UNLOCK(sc);
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
FFEC_LOCK(sc);
ffec_setup_rxfilter(sc);
FFEC_UNLOCK(sc);
}
break;
case SIOCSIFMEDIA:
case SIOCGIFMEDIA:
mii = sc->mii_softc;
error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
break;
case SIOCSIFCAP:
mask = if_getcapenable(ifp) ^ ifr->ifr_reqcap;
if (mask & IFCAP_VLAN_MTU) {
if_togglecapenable(ifp, IFCAP_VLAN_MTU);
}
break;
default:
error = ether_ioctl(ifp, cmd, data);
break;
}
return (error);
}
static int
ffec_detach(device_t dev)
{
struct ffec_softc *sc;
bus_dmamap_t map;
int idx, irq;
sc = device_get_softc(dev);
if (sc->is_attached) {
FFEC_LOCK(sc);
sc->is_detaching = true;
ffec_stop_locked(sc);
FFEC_UNLOCK(sc);
callout_drain(&sc->ffec_callout);
ether_ifdetach(sc->ifp);
}
for (idx = 0; idx < RX_DESC_COUNT; ++idx) {
if ((map = sc->rxbuf_map[idx].map) != NULL) {
bus_dmamap_unload(sc->rxbuf_tag, map);
bus_dmamap_destroy(sc->rxbuf_tag, map);
m_freem(sc->rxbuf_map[idx].mbuf);
}
}
if (sc->rxbuf_tag != NULL)
bus_dma_tag_destroy(sc->rxbuf_tag);
if (sc->rxdesc_map != NULL) {
bus_dmamap_unload(sc->rxdesc_tag, sc->rxdesc_map);
bus_dmamem_free(sc->rxdesc_tag, sc->rxdesc_ring,
sc->rxdesc_map);
}
if (sc->rxdesc_tag != NULL)
bus_dma_tag_destroy(sc->rxdesc_tag);
for (idx = 0; idx < TX_DESC_COUNT; ++idx) {
if ((map = sc->txbuf_map[idx].map) != NULL) {
bus_dmamap_destroy(sc->txbuf_tag, map);
}
}
if (sc->txbuf_tag != NULL)
bus_dma_tag_destroy(sc->txbuf_tag);
if (sc->txdesc_map != NULL) {
bus_dmamap_unload(sc->txdesc_tag, sc->txdesc_map);
bus_dmamem_free(sc->txdesc_tag, sc->txdesc_ring,
sc->txdesc_map);
}
if (sc->txdesc_tag != NULL)
bus_dma_tag_destroy(sc->txdesc_tag);
for (irq = 0; irq < MAX_IRQ_COUNT; ++irq) {
if (sc->intr_cookie[irq] != NULL) {
bus_teardown_intr(dev, sc->irq_res[irq],
sc->intr_cookie[irq]);
}
}
bus_release_resources(dev, irq_res_spec, sc->irq_res);
if (sc->mem_res != NULL)
bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
FFEC_LOCK_DESTROY(sc);
return (0);
}
static int
ffec_attach(device_t dev)
{
struct ffec_softc *sc;
if_t ifp = NULL;
struct mbuf *m;
void *dummy;
uintptr_t typeflags;
phandle_t ofw_node;
uint32_t idx, mscr;
int error, phynum, rid, irq;
uint8_t eaddr[ETHER_ADDR_LEN];
sc = device_get_softc(dev);
sc->dev = dev;
FFEC_LOCK_INIT(sc);
typeflags = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
sc->fectype = (uint8_t)(typeflags & FECTYPE_MASK);
sc->fecflags = (uint32_t)(typeflags & ~FECTYPE_MASK);
if (sc->fecflags & FECFLAG_AVB) {
sc->rxbuf_align = 64;
sc->txbuf_align = 1;
} else {
sc->rxbuf_align = 16;
sc->txbuf_align = 16;
}
if ((ofw_node = ofw_bus_get_node(dev)) == -1) {
device_printf(dev, "Impossible: Can't find ofw bus node\n");
error = ENXIO;
goto out;
}
sc->phy_conn_type = mii_fdt_get_contype(ofw_node);
if (sc->phy_conn_type == MII_CONTYPE_UNKNOWN) {
device_printf(sc->dev, "No valid 'phy-mode' "
"property found in FDT data for device.\n");
error = ENOATTR;
goto out;
}
callout_init_mtx(&sc->ffec_callout, &sc->mtx, 0);
rid = 0;
sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
RF_ACTIVE);
if (sc->mem_res == NULL) {
device_printf(dev, "could not allocate memory resources.\n");
error = ENOMEM;
goto out;
}
error = bus_alloc_resources(dev, irq_res_spec, sc->irq_res);
if (error != 0) {
device_printf(dev, "could not allocate interrupt resources\n");
goto out;
}
error = bus_dma_tag_create(
bus_get_dma_tag(dev),
FEC_DESC_RING_ALIGN, 0,
BUS_SPACE_MAXADDR_32BIT,
BUS_SPACE_MAXADDR,
NULL, NULL,
TX_DESC_SIZE, 1,
TX_DESC_SIZE,
0,
NULL, NULL,
&sc->txdesc_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create TX ring DMA tag.\n");
goto out;
}
error = bus_dmamem_alloc(sc->txdesc_tag, (void**)&sc->txdesc_ring,
BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->txdesc_map);
if (error != 0) {
device_printf(sc->dev,
"could not allocate TX descriptor ring.\n");
goto out;
}
error = bus_dmamap_load(sc->txdesc_tag, sc->txdesc_map, sc->txdesc_ring,
TX_DESC_SIZE, ffec_get1paddr, &sc->txdesc_ring_paddr, 0);
if (error != 0) {
device_printf(sc->dev,
"could not load TX descriptor ring map.\n");
goto out;
}
error = bus_dma_tag_create(
bus_get_dma_tag(dev),
sc->txbuf_align, 0,
BUS_SPACE_MAXADDR_32BIT,
BUS_SPACE_MAXADDR,
NULL, NULL,
MCLBYTES, 1,
MCLBYTES,
0,
NULL, NULL,
&sc->txbuf_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create TX ring DMA tag.\n");
goto out;
}
for (idx = 0; idx < TX_DESC_COUNT; ++idx) {
error = bus_dmamap_create(sc->txbuf_tag, 0,
&sc->txbuf_map[idx].map);
if (error != 0) {
device_printf(sc->dev,
"could not create TX buffer DMA map.\n");
goto out;
}
ffec_setup_txdesc(sc, idx, 0, 0);
}
error = bus_dma_tag_create(
bus_get_dma_tag(dev),
FEC_DESC_RING_ALIGN, 0,
BUS_SPACE_MAXADDR_32BIT,
BUS_SPACE_MAXADDR,
NULL, NULL,
RX_DESC_SIZE, 1,
RX_DESC_SIZE,
0,
NULL, NULL,
&sc->rxdesc_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create RX ring DMA tag.\n");
goto out;
}
error = bus_dmamem_alloc(sc->rxdesc_tag, (void **)&sc->rxdesc_ring,
BUS_DMA_COHERENT | BUS_DMA_WAITOK | BUS_DMA_ZERO, &sc->rxdesc_map);
if (error != 0) {
device_printf(sc->dev,
"could not allocate RX descriptor ring.\n");
goto out;
}
error = bus_dmamap_load(sc->rxdesc_tag, sc->rxdesc_map, sc->rxdesc_ring,
RX_DESC_SIZE, ffec_get1paddr, &sc->rxdesc_ring_paddr, 0);
if (error != 0) {
device_printf(sc->dev,
"could not load RX descriptor ring map.\n");
goto out;
}
error = bus_dma_tag_create(
bus_get_dma_tag(dev),
1, 0,
BUS_SPACE_MAXADDR_32BIT,
BUS_SPACE_MAXADDR,
NULL, NULL,
MCLBYTES, 1,
MCLBYTES,
0,
NULL, NULL,
&sc->rxbuf_tag);
if (error != 0) {
device_printf(sc->dev,
"could not create RX buf DMA tag.\n");
goto out;
}
for (idx = 0; idx < RX_DESC_COUNT; ++idx) {
error = bus_dmamap_create(sc->rxbuf_tag, 0,
&sc->rxbuf_map[idx].map);
if (error != 0) {
device_printf(sc->dev,
"could not create RX buffer DMA map.\n");
goto out;
}
if ((m = ffec_alloc_mbufcl(sc)) == NULL) {
device_printf(dev, "Could not alloc mbuf\n");
error = ENOMEM;
goto out;
}
if ((error = ffec_setup_rxbuf(sc, idx, m)) != 0) {
device_printf(sc->dev,
"could not create new RX buffer.\n");
goto out;
}
}
ffec_get_hwaddr(sc, eaddr);
if (sc->fecflags & FECFLAG_AVB)
WR4(sc, FEC_ECR_REG, 0);
else
WR4(sc, FEC_ECR_REG, FEC_ECR_RESET);
for (irq = 0; irq < MAX_IRQ_COUNT; ++irq) {
if (sc->irq_res[irq] != NULL) {
error = bus_setup_intr(dev, sc->irq_res[irq],
INTR_TYPE_NET | INTR_MPSAFE, NULL, ffec_intr, sc,
&sc->intr_cookie[irq]);
if (error != 0) {
device_printf(dev,
"could not setup interrupt handler.\n");
goto out;
}
}
}
mscr = 13 << FEC_MSCR_MII_SPEED_SHIFT;
if (OF_hasprop(ofw_node, "phy-disable-preamble")) {
mscr |= FEC_MSCR_DIS_PRE;
if (bootverbose)
device_printf(dev, "PHY preamble disabled\n");
}
WR4(sc, FEC_MSCR_REG, mscr);
sc->ifp = ifp = if_alloc(IFT_ETHER);
if_setsoftc(ifp, sc);
if_initname(ifp, device_get_name(dev), device_get_unit(dev));
if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
if_setcapabilities(ifp, IFCAP_VLAN_MTU);
if_setcapenable(ifp, if_getcapabilities(ifp));
if_setstartfn(ifp, ffec_txstart);
if_setioctlfn(ifp, ffec_ioctl);
if_setinitfn(ifp, ffec_init);
if_setsendqlen(ifp, TX_DESC_COUNT - 1);
if_setsendqready(ifp);
if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
#if 0
if_setlinkmib(ifp, &sc->mibdata);
if_setlinkmiblen(ifp, sizeof(sc->mibdata));
#endif
ffec_miigasket_setup(sc);
if (fdt_get_phyaddr(ofw_node, dev, &phynum, &dummy) != 0) {
phynum = MII_PHY_ANY;
}
error = mii_attach(dev, &sc->miibus, ifp, ffec_media_change,
ffec_media_status, BMSR_DEFCAPMASK, phynum, MII_OFFSET_ANY,
(sc->fecflags & FECTYPE_MVF) ? MIIF_FORCEANEG : 0);
if (error != 0) {
device_printf(dev, "PHY attach failed\n");
goto out;
}
sc->mii_softc = device_get_softc(sc->miibus);
ether_ifattach(ifp, eaddr);
sc->is_attached = true;
error = 0;
out:
if (error != 0)
ffec_detach(dev);
return (error);
}
static int
ffec_probe(device_t dev)
{
uintptr_t fectype;
if (!ofw_bus_status_okay(dev))
return (ENXIO);
fectype = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
if (fectype == FECTYPE_NONE)
return (ENXIO);
device_set_desc(dev, (fectype & FECFLAG_GBE) ?
"Freescale Gigabit Ethernet Controller" :
"Freescale Fast Ethernet Controller");
return (BUS_PROBE_DEFAULT);
}
static device_method_t ffec_methods[] = {
DEVMETHOD(device_probe, ffec_probe),
DEVMETHOD(device_attach, ffec_attach),
DEVMETHOD(device_detach, ffec_detach),
DEVMETHOD(miibus_readreg, ffec_miibus_readreg),
DEVMETHOD(miibus_writereg, ffec_miibus_writereg),
DEVMETHOD(miibus_statchg, ffec_miibus_statchg),
DEVMETHOD_END
};
static driver_t ffec_driver = {
"ffec",
ffec_methods,
sizeof(struct ffec_softc)
};
DRIVER_MODULE(ffec, simplebus, ffec_driver, 0, 0);
DRIVER_MODULE(miibus, ffec, miibus_driver, 0, 0);
MODULE_DEPEND(ffec, ether, 1, 1, 1);
MODULE_DEPEND(ffec, miibus, 1, 1, 1);