#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_le_ebus.c,v 1.26 2024/07/05 04:31:49 rin Exp $");
#include "opt_inet.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/syslog.h>
#include <sys/socket.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/errno.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_ether.h>
#include <net/if_media.h>
#include <net/bpf.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/if_inarp.h>
#endif
#include <sys/rndsource.h>
#include <emips/ebus/ebusvar.h>
#include <emips/emips/machdep.h>
#include <machine/emipsreg.h>
extern paddr_t kvtophys(vaddr_t);
struct bufmap {
struct mbuf *mbuf;
paddr_t phys;
};
struct enic_softc {
device_t sc_dev;
struct ethercom sc_ethercom;
struct ifmedia sc_media;
struct _Enic *sc_regs;
int sc_havecarrier;
void *sc_sh;
int inited;
int sc_no_rd;
int sc_n_recv;
int sc_recv_h;
#define SC_MAX_N_RECV 64
struct bufmap sc_recv[SC_MAX_N_RECV];
int sc_no_td;
int sc_n_xmit;
int sc_xmit_h;
#define SC_MAX_N_XMIT 16
struct bufmap sc_xmit[SC_MAX_N_XMIT];
bool sc_txbusy;
#if DEBUG
int xhit;
int xmiss;
int tfull;
int tfull2;
int brh;
int rf;
int bxh;
int it;
#endif
uint8_t sc_enaddr[ETHER_ADDR_LEN];
uint8_t sc_pad[2];
krndsource_t rnd_source;
};
void enic_reset(struct ifnet *);
int enic_init(struct ifnet *);
void enic_stop(struct ifnet *, int);
void enic_start(struct ifnet *);
void enic_shutdown(void *);
void enic_watchdog(struct ifnet *);
int enic_mediachange(struct ifnet *);
void enic_mediastatus(struct ifnet *, struct ifmediareq *);
int enic_ioctl(struct ifnet *, u_long, void *);
int enic_intr(void *, void *);
void enic_rint(struct enic_softc *, uint32_t, paddr_t);
void enic_tint(struct enic_softc *, uint32_t, paddr_t);
void enic_kill_xmit(struct enic_softc *);
void enic_post_recv(struct enic_softc *, struct mbuf *);
void enic_refill(struct enic_softc *);
static int enic_gethwinfo(struct enic_softc *);
int enic_put(struct enic_softc *, struct mbuf **);
static int enic_match(device_t, cfdata_t, void *);
static void enic_attach(device_t, device_t, void *);
CFATTACH_DECL_NEW(enic_emips, sizeof(struct enic_softc),
enic_match, enic_attach, NULL, NULL);
int
enic_match(device_t parent, cfdata_t cf, void *aux)
{
struct ebus_attach_args *d = aux;
struct _Enic *et = (struct _Enic *)d->ia_vaddr;
if (strcmp("enic", d->ia_name) != 0)
return 0;
if ((et == NULL) || (et->Tag != PMTTAG_ETHERNET))
return 0;
return 1;
}
void
enic_attach(device_t parent, device_t self, void *aux)
{
struct enic_softc *sc = device_private(self);
struct ebus_attach_args *ia = aux;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
sc->sc_regs = (struct _Enic *)(ia->ia_vaddr);
#if DEBUG
printf(" virt=%p ", (void *)sc->sc_regs);
#endif
if (!enic_gethwinfo(sc)) {
printf(" <cannot get hw info> DISABLED.\n");
return;
}
sc->sc_dev = self;
sc->sc_no_td = 0;
sc->sc_havecarrier = 1;
sc->sc_recv_h = 0;
sc->sc_xmit_h = 0;
memset(sc->sc_recv, 0, sizeof sc->sc_recv);
memset(sc->sc_xmit, 0, sizeof sc->sc_xmit);
strcpy(ifp->if_xname, device_xname(sc->sc_dev));
ifp->if_softc = sc;
ifp->if_start = enic_start;
ifp->if_ioctl = enic_ioctl;
ifp->if_watchdog = enic_watchdog;
ifp->if_init = enic_init;
ifp->if_stop = enic_stop;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
IFQ_SET_READY(&ifp->if_snd);
sc->sc_ethercom.ec_ifmedia = &sc->sc_media;
ifmedia_init(&sc->sc_media, 0, enic_mediachange, enic_mediastatus);
ifmedia_add(&sc->sc_media, IFM_ETHER | IFM_MANUAL, 0, NULL);
ifmedia_set(&sc->sc_media, IFM_ETHER | IFM_MANUAL);
enic_stop(ifp, 0);
sc->inited = 0;
printf(": eNIC [%d %d], address %s\n",
sc->sc_n_recv, sc->sc_n_xmit, ether_sprintf(sc->sc_enaddr));
#if 0
sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
#endif
if_attach(ifp);
if_deferred_start_init(ifp, NULL);
ether_ifattach(ifp, sc->sc_enaddr);
sc->sc_sh = shutdownhook_establish(enic_shutdown, ifp);
if (sc->sc_sh == NULL)
panic("enic_attach: cannot establish shutdown hook");
rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
RND_TYPE_NET, RND_FLAG_DEFAULT);
ebus_intr_establish(parent, (void *)ia->ia_cookie, IPL_NET,
enic_intr, sc);
}
static int enic_gethwinfo(struct enic_softc *sc)
{
uint8_t buffer[8];
PENIC_INFO hw = (PENIC_INFO)buffer;
paddr_t phys = kvtophys((vaddr_t)&buffer[0]), phys2;
int i;
memset(buffer, 0, sizeof buffer);
buffer[0] = ENIC_CMD_GET_ADDRESS;
buffer[3] = ENIC_CMD_GET_ADDRESS;
sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
sc->sc_regs->BufferAddressHi32 = 0;
sc->sc_regs->BufferAddressLo32 = phys;
for (i = 0; i < 100; i++) {
DELAY(100);
if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
break;
}
if (i == 100)
return 0;
phys2 = sc->sc_regs->BufferAddressLo32;
if (phys2 != phys) {
printf("enic uhu? %llx != %llx?\n",
(long long)phys, (long long)phys2);
return 0;
}
memcpy(sc->sc_enaddr, buffer, ETHER_ADDR_LEN);
memset(buffer, 0, sizeof buffer);
buffer[0] = ENIC_CMD_GET_INFO;
buffer[3] = ENIC_CMD_GET_INFO;
sc->sc_regs->SizeAndFlags = (sizeof buffer) | ES_F_CMD;
sc->sc_regs->BufferAddressHi32 = 0;
sc->sc_regs->BufferAddressLo32 = phys;
for (i = 0; i < 100; i++) {
DELAY(100);
if ((sc->sc_regs->Control & EC_OF_EMPTY) == 0)
break;
}
if (i == 100)
return 0;
phys2 = sc->sc_regs->BufferAddressLo32;
if (phys2 != phys) {
printf("enic uhu2? %llx != %llx?\n",
(long long)phys, (long long)phys2);
return 0;
}
#if 0
printf("enic: hwinfo: %x %x %x %x %x %x \n",
hw->InputFifoSize, hw->OutputFifoSize, hw->CompletionFifoSize,
hw->ErrorCount, hw->FramesDropped, hw->Reserved);
#endif
sc->sc_n_recv = hw->InputFifoSize;
if (sc->sc_n_recv > SC_MAX_N_RECV)
sc->sc_n_recv = SC_MAX_N_RECV;
if (sc->sc_n_recv == 0) {
sc->sc_n_recv = 8;
sc->sc_n_xmit = 4;
} else {
sc->sc_n_xmit = hw->OutputFifoSize;
if (sc->sc_n_xmit > SC_MAX_N_XMIT)
sc->sc_n_xmit = SC_MAX_N_XMIT;
}
return 1;
}
void
enic_reset(struct ifnet *ifp)
{
int s;
s = splnet();
enic_stop(ifp, 0);
enic_init(ifp);
splx(s);
}
void
enic_stop(struct ifnet *ifp, int suspend)
{
struct enic_softc *sc = ifp->if_softc;
#if 0
printf("enic_stop %x\n", sc->sc_regs->Control);
#endif
sc->inited = 2;
sc->sc_regs->Control = EC_RESET;
sc->sc_no_rd = 0;
sc->sc_no_td = 0;
}
void
enic_shutdown(void *arg)
{
struct ifnet *ifp = arg;
enic_stop(ifp, 0);
}
void
enic_kill_xmit(struct enic_softc *sc)
{
int i;
struct mbuf *m;
for (i = 0; i < sc->sc_n_xmit; i++) {
if ((m = sc->sc_xmit[i].mbuf) != NULL) {
sc->sc_xmit[i].mbuf = NULL;
sc->sc_xmit[i].phys = ~0;
m_freem(m);
}
}
sc->sc_no_td = 0;
sc->sc_xmit_h = 0;
}
void
enic_post_recv(struct enic_softc *sc, struct mbuf *m)
{
int i, waitmode = M_DONTWAIT;
paddr_t phys;
#define rpostone(_p_) \
sc->sc_regs->SizeAndFlags = ES_F_RECV | MCLBYTES; \
sc->sc_regs->BufferAddressHi32 = 0; \
sc->sc_regs->BufferAddressLo32 = _p_;
#define tpostone(_p_,_s_) \
sc->sc_regs->SizeAndFlags = ES_F_XMIT | (_s_); \
sc->sc_regs->BufferAddressHi32 = 0; \
sc->sc_regs->BufferAddressLo32 = _p_;
if (m != NULL) {
if (sc->sc_regs->Control & EC_IF_FULL)
goto no_room;
for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
if (sc->sc_recv[i].mbuf == NULL)
goto found;
for (i = 0; i < sc->sc_recv_h; i++)
if (sc->sc_recv[i].mbuf == NULL)
goto found;
no_room:
#if DEBUG
sc->rf++;
#endif
m_freem(m);
return;
found:
phys = kvtophys((vaddr_t)m->m_data);
sc->sc_recv[i].mbuf = m;
sc->sc_recv[i].phys = phys;
rpostone(phys);
sc->sc_no_rd++;
return;
}
if (sc->inited) {
int j = 0;
sc->sc_recv_h = 0;
for (i = 0; i < sc->sc_n_recv; i++)
if ((m = sc->sc_recv[i].mbuf) != NULL) {
phys = sc->sc_recv[i].phys;
sc->sc_recv[i].mbuf = NULL;
sc->sc_recv[i].phys = ~0;
sc->sc_recv[j].mbuf = m;
sc->sc_recv[j].phys = phys;
#if DEBUG
if (sc->sc_regs->Control & EC_IF_FULL)
printf("?uhu? postrecv full? %d\n",
sc->sc_no_rd);
#endif
sc->sc_no_rd++;
rpostone(phys);
j++;
}
sc->inited = 1;
if (j >= sc->sc_n_recv)
return;
i = j; m = NULL;
goto fillem;
}
waitmode = M_WAIT;
sc->sc_recv_h = 0;
memset(sc->sc_recv, 0, sizeof(sc->sc_recv[0]) * sc->sc_n_recv);
i = 0;
fillem:
for (; i < sc->sc_n_recv; i++) {
MGETHDR(m, waitmode, MT_DATA);
if (m == 0)
break;
m_set_rcvif(m, &sc->sc_ethercom.ec_if);
m->m_pkthdr.len = 0;
MCLGET(m, waitmode);
if ((m->m_flags & M_EXT) == 0)
break;
#if 1
#define ADJUST_MBUF_OFFSET(_m_) { \
(_m_)->m_data += 2; \
(_m_)->m_len -= 2; \
}
#else
#define ADJUST_MBUF_OFFSET(_m_)
#endif
m->m_len = MCLBYTES;
ADJUST_MBUF_OFFSET(m);
phys = kvtophys((vaddr_t)m->m_data);
sc->sc_recv[i].mbuf = m;
sc->sc_recv[i].phys = phys;
#if DEBUG
if (sc->sc_regs->Control & EC_IF_FULL)
printf("?uhu? postrecv2 full? %d\n", sc->sc_no_rd);
#endif
sc->sc_no_rd++;
rpostone(phys);
m = NULL;
}
m_freem(m);
sc->inited = 1;
}
void enic_refill(struct enic_softc *sc)
{
struct mbuf *m;
int waitmode = M_DONTWAIT;
MGETHDR(m, waitmode, MT_DATA);
if (m == NULL)
return;
m_set_rcvif(m, &sc->sc_ethercom.ec_if);
m->m_pkthdr.len = 0;
MCLGET(m, waitmode);
if ((m->m_flags & M_EXT) == 0) {
m_freem(m);
return;
}
m->m_len = MCLBYTES;
ADJUST_MBUF_OFFSET(m);
enic_post_recv(sc, m);
}
int
enic_init(struct ifnet *ifp)
{
struct enic_softc *sc = ifp->if_softc;
uint32_t ctl;
if (sc->inited != 1) {
enic_kill_xmit(sc);
enic_post_recv(sc, NULL);
}
ifp->if_flags |= IFF_RUNNING;
sc->sc_txbusy = false;
ifp->if_timer = 0;
ctl = sc->sc_regs->Control | EC_INTEN;
ctl &= ~EC_RXDIS;
sc->sc_regs->Control = ctl;
#if 0
printf("enic_init <- %x\n", ctl);
#endif
if_schedule_deferred_start(ifp);
return 0;
}
void
enic_watchdog(struct ifnet *ifp)
{
struct enic_softc *sc = ifp->if_softc;
#if 0
printf("enic_watch ctl=%x\n", sc->sc_regs->Control);
#endif
log(LOG_ERR, "%s: device timeout\n", device_xname(sc->sc_dev));
if_statinc(ifp, if_oerrors);
enic_reset(ifp);
}
int
enic_mediachange(struct ifnet *ifp)
{
#if 0
struct enic_softc *sc = ifp->if_softc;
#endif
return 0;
}
void
enic_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
{
struct enic_softc *sc = ifp->if_softc;
if ((ifp->if_flags & IFF_UP) == 0)
return;
ifmr->ifm_status = IFM_AVALID;
if (sc->sc_havecarrier)
ifmr->ifm_status |= IFM_ACTIVE;
}
int
enic_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
int s, error = 0;
s = splnet();
switch (cmd) {
default:
error = ether_ioctl(ifp, cmd, data);
if (cmd == SIOCSIFADDR) {
#if 0
printf("enic_ioctl(%lx)\n", cmd);
#endif
enic_init(ifp);
}
if (error != ENETRESET)
break;
error = 0;
if (cmd != SIOCADDMULTI && cmd != SIOCDELMULTI)
break;
if (ifp->if_flags & IFF_RUNNING) {
enic_reset(ifp);
}
break;
}
splx(s);
return error;
}
int
enic_intr(void *cookie, void *f)
{
struct enic_softc *sc = cookie;
uint32_t isr, saf, hi, lo, fl;
isr = sc->sc_regs->Control;
if ((isr & (EC_INTEN | EC_DONE)) != (EC_INTEN | EC_DONE))
return 0;
if (isr & EC_ERROR) {
printf("%s: internal error\n", device_xname(sc->sc_dev));
enic_reset(&sc->sc_ethercom.ec_if);
return 1;
}
while ((isr & EC_OF_EMPTY) == 0) {
saf = sc->sc_regs->SizeAndFlags;
hi = sc->sc_regs->BufferAddressHi32;
lo = sc->sc_regs->BufferAddressLo32;
__USE(hi);
fl = saf & (ES_F_MASK &~ ES_F_DONE);
if (fl == ES_F_RECV)
enic_rint(sc, saf, lo);
else if (fl == ES_F_XMIT)
enic_tint(sc, saf, lo);
else {
if (fl != ES_F_CMD)
printf("%s: invalid saf=x%x (lo=%x)\n",
device_xname(sc->sc_dev), saf, lo);
}
isr = sc->sc_regs->Control;
}
rnd_add_uint32(&sc->rnd_source, isr);
return 1;
}
void
enic_rint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
{
struct mbuf *m;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
int len = saf & ES_S_MASK, i;
for (i = sc->sc_recv_h; i < sc->sc_n_recv; i++)
if (sc->sc_recv[i].phys == phys)
goto found;
for (i = 0; i < sc->sc_recv_h; i++)
if (sc->sc_recv[i].phys == phys)
goto found;
printf("%s: bad recv phys %llx\n", device_xname(sc->sc_dev),
(long long)phys);
if_statinc(ifp, if_ierrors);
return;
found:
sc->sc_no_rd--;
m = sc->sc_recv[i].mbuf;
sc->sc_recv[i].mbuf = NULL;
sc->sc_recv[i].phys = ~0;
if (i == sc->sc_recv_h) {
sc->sc_recv_h = (++i == sc->sc_n_recv) ? 0 : i;
}
#if DEBUG
else
sc->brh++;
#endif
if (len <= sizeof(struct ether_header) ||
len > ((sc->sc_ethercom.ec_capenable & ETHERCAP_VLAN_MTU) ?
ETHER_VLAN_ENCAP_LEN + ETHERMTU + sizeof(struct ether_header) :
ETHERMTU + sizeof(struct ether_header))) {
if_statinc(ifp, if_ierrors);
enic_post_recv(sc, m);
return;
}
m->m_pkthdr.len = len;
m->m_len = len;
if_percpuq_enqueue(ifp->if_percpuq, m);
enic_refill(sc);
}
void enic_tint(struct enic_softc *sc, uint32_t saf, paddr_t phys)
{
struct mbuf *m;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
int i;
#if DEBUG
sc->it = 1;
#endif
for (i = sc->sc_xmit_h; i < sc->sc_n_xmit; i++)
if (sc->sc_xmit[i].phys == phys)
goto found;
for (i = 0; i < sc->sc_xmit_h; i++)
if (sc->sc_xmit[i].phys == phys)
goto found;
printf("%s: bad xmit phys %llx\n", device_xname(sc->sc_dev),
(long long)phys);
if_statinc(ifp, if_oerrors);
return;
found:
m = sc->sc_xmit[i].mbuf;
sc->sc_xmit[i].mbuf = NULL;
sc->sc_xmit[i].phys = ~0;
if (i == sc->sc_xmit_h) {
sc->sc_xmit_h = (++i == sc->sc_n_xmit) ? 0 : i;
}
#if DEBUG
else
sc->bxh++;
#endif
m_freem(m);
if_statinc(ifp, if_opackets);
if (--sc->sc_no_td == 0)
ifp->if_timer = 0;
sc->sc_txbusy = false;
if_schedule_deferred_start(ifp);
#if DEBUG
sc->it = 1;
#endif
}
void
enic_start(struct ifnet *ifp)
{
struct enic_softc *sc = ifp->if_softc;
struct mbuf *m;
int len, ix, s;
paddr_t phys;
#if DEBUG
sc->it = 0;
#endif
#if 0
printf("enic_start(%x)\n", ifp->if_flags);
#endif
if ((ifp->if_flags & IFF_RUNNING) == 0)
return;
s = splnet();
ix = sc->sc_xmit_h;
for (;;) {
IFQ_POLL(&ifp->if_snd, m);
if (m == NULL)
break;
for (; ix < sc->sc_n_xmit; ix++)
if (sc->sc_xmit[ix].mbuf == NULL)
goto found;
for (ix = 0; ix < sc->sc_xmit_h; ix++)
if (sc->sc_xmit[ix].mbuf == NULL)
goto found;
sc->sc_txbusy = true;
#if DEBUG
sc->tfull++;
#endif
break;
found:
IFQ_DEQUEUE(&ifp->if_snd, m);
if (m == NULL)
break;
bpf_mtap(ifp, m, BPF_D_OUT);
len = enic_put(sc, &m);
if (len == 0)
break;
if (len > (ETHERMTU + sizeof(struct ether_header))) {
printf("enic? tlen %d > %d\n",
len, ETHERMTU + sizeof(struct ether_header));
len = ETHERMTU + sizeof(struct ether_header);
}
ifp->if_timer = 5;
phys = kvtophys((vaddr_t)m->m_data);
sc->sc_xmit[ix].mbuf = m;
sc->sc_xmit[ix].phys = phys;
sc->sc_no_td++;
tpostone(phys, len);
if (sc->sc_regs->Control & EC_IF_FULL) {
sc->sc_txbusy = true;
#if DEBUG
sc->tfull2++;
#endif
break;
}
ix++;
}
splx(s);
}
int enic_put(struct enic_softc *sc, struct mbuf **pm)
{
struct mbuf *n, *m = *pm, *mm;
int len, tlen = 0, xlen = m->m_pkthdr.len;
uint8_t *cp;
#if 0
tlen = xlen;
for (; m; m = n) {
len = m->m_len;
if (len == 0) {
n = m_free(m);
if (m == *pm)
*pm = n;
continue;
}
tlen -= len;
KASSERT(m != m->m_next);
n = m->m_next;
if (tlen <= 0)
break;
}
if (((m = *pm) == NULL) || (tlen > 0))
xlen = 0;
#endif
if ((xlen == 0) || (xlen <= m->m_len)) {
#if DEBUG
sc->xhit++;
#endif
return xlen;
}
tlen = xlen;
MGETHDR(n, M_NOWAIT, MT_DATA);
if (n == NULL)
goto Bad;
m_set_rcvif(n, &sc->sc_ethercom.ec_if);
n->m_pkthdr.len = tlen;
MCLGET(n, M_NOWAIT);
if ((n->m_flags & M_EXT) == 0) {
m_freem(n);
goto Bad;
}
n->m_len = tlen;
cp = mtod(n, uint8_t *);
for (; m && tlen; m = mm) {
len = m->m_len;
if (len > tlen)
len = tlen;
if (len)
memcpy(cp, mtod(m, void *), len);
cp += len;
tlen -= len;
mm = m_free(m);
}
*pm = n;
#if DEBUG
sc->xmiss++;
#endif
return (xlen);
Bad:
printf("enic_put: no mem?\n");
m_freem(m);
return 0;
}