#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: if_plip.c,v 1.38 2022/09/04 15:59:08 rjs Exp $");
#include "opt_inet.h"
#include "opt_plip.h"
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/types.h>
#include <sys/device.h>
#include <sys/ioctl.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <net/if.h>
#include <net/if_types.h>
#include <sys/time.h>
#include <net/bpf.h>
#ifdef INET
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/in_var.h>
#include <netinet/ip.h>
#else
#error Cannot config lp/plip without inet
#endif
#include <dev/ppbus/ppbus_base.h>
#include <dev/ppbus/ppbus_device.h>
#include <dev/ppbus/ppbus_io.h>
#include <dev/ppbus/ppbus_var.h>
#include <machine/types.h>
#include <sys/intr.h>
#ifndef LPMTU
#define LPMTU 1500
#endif
#ifndef LPMAXSPIN1
#define LPMAXSPIN1 8000
#endif
#ifndef LPMAXSPIN2
#define LPMAXSPIN2 500
#endif
#ifndef LPMAXERRS
#define LPMAXERRS 100
#endif
#ifndef LPMAXRTRY
#define LPMAXRTRY 100
#endif
#define CLPIPHDRLEN 14
#define CLPIP_SHAKE 0x80
#define MLPIPHDRLEN CLPIPHDRLEN
#define LPIPHDRLEN 2
#define LPIP_SHAKE 0x40
#if !defined(MLPIPHDRLEN) || LPIPHDRLEN > MLPIPHDRLEN
#define MLPIPHDRLEN LPIPHDRLEN
#endif
#define LPIPTBLSIZE 256
#define LP_PRINTF if (lpflag) printf
#ifdef PLIP_DEBUG
static int volatile lpflag = 1;
#else
static int volatile lpflag = 0;
#endif
static u_char *txmith;
#define txmitl (txmith+(1*LPIPTBLSIZE))
#define trecvh (txmith+(2*LPIPTBLSIZE))
#define trecvl (txmith+(3*LPIPTBLSIZE))
static u_char *ctxmith;
#define ctxmitl (ctxmith+(1*LPIPTBLSIZE))
#define ctrecvh (ctxmith+(2*LPIPTBLSIZE))
#define ctrecvl (ctxmith+(3*LPIPTBLSIZE))
static uint16_t lp_count = 0;
static int lp_probe(device_t, cfdata_t, void *);
static void lp_attach(device_t, device_t, void *);
static int lp_detach(device_t, int);
struct lp_softc {
struct ppbus_device_softc ppbus_dev;
struct ifnet sc_if;
u_char *sc_ifbuf;
unsigned short sc_iferrs;
unsigned short sc_xmit_rtry;
u_int8_t sc_dev_ok;
};
CFATTACH_DECL_NEW(plip, sizeof(struct lp_softc), lp_probe, lp_attach, lp_detach,
NULL);
static void lpinittables(void);
static void lpfreetables(void);
static int lpioctl(struct ifnet *, u_long, void *);
static int lpoutput(struct ifnet *, struct mbuf *, const struct sockaddr *,
const struct rtentry *);
static void lpstart(struct ifnet *);
static void lp_intr(void *);
static int
lp_probe(device_t parent, cfdata_t match, void *aux)
{
struct ppbus_attach_args * args = aux;
if (args->capabilities & PPBUS_HAS_INTR)
return 1;
printf("%s(%s): not an interrupt-driven port.\n", __func__,
device_xname(parent));
return 0;
}
static void
lp_attach(device_t parent, device_t self, void *aux)
{
struct lp_softc * lp = device_private(self);
struct ifnet * ifp = &lp->sc_if;
lp->ppbus_dev.sc_dev = self;
lp->sc_dev_ok = 0;
lp->sc_ifbuf = NULL;
lp->sc_iferrs = 0;
lp->sc_xmit_rtry = 0;
ifp->if_softc = lp;
strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
ifp->if_mtu = LPMTU;
ifp->if_flags = IFF_SIMPLEX | IFF_POINTOPOINT | IFF_MULTICAST;
ifp->if_ioctl = lpioctl;
ifp->if_output = lpoutput;
ifp->if_start = lpstart;
ifp->if_type = IFT_PARA;
ifp->if_hdrlen = 0;
ifp->if_addrlen = 0;
ifp->if_dlt = DLT_NULL;
IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
IFQ_SET_READY(&ifp->if_snd);
if_attach(ifp);
if_alloc_sadl(ifp);
bpf_attach(ifp, DLT_NULL, sizeof(u_int32_t));
if (lp_count++ == 0)
lpinittables();
printf("\n");
}
static int
lp_detach(device_t self, int flags)
{
int error = 0;
struct lp_softc * lp = device_private(self);
device_t ppbus = device_parent(self);
if (lp->sc_dev_ok) {
if (!(flags & DETACH_QUIET))
LP_PRINTF("%s(%s): device not properly attached! "
"Skipping detach....\n", __func__,
device_xname(self));
return error;
}
if (lp->sc_if.if_flags & IFF_RUNNING) {
ppbus_wctr(ppbus, 0x00);
if_detach(&lp->sc_if);
error = ppbus_remove_handler(ppbus, lp_intr);
if (error) {
if (!(flags & DETACH_QUIET))
LP_PRINTF("%s(%s): unable to remove interrupt "
"callback.\n", __func__,
device_xname(self));
if (!(flags & DETACH_FORCE))
return error;
}
error = ppbus_release_bus(ppbus, self, 0, 0);
if (error) {
if (!(flags & DETACH_QUIET))
LP_PRINTF("%s(%s): error releasing bus %s.\n",
__func__, device_xname(self),
device_xname(ppbus));
if (!(flags & DETACH_FORCE))
return error;
}
}
if (lp->sc_ifbuf)
free(lp->sc_ifbuf, M_DEVBUF);
if (--lp_count == 0)
lpfreetables();
return error;
}
static void
lpinittables(void)
{
int i;
if (!txmith)
txmith = malloc(4*LPIPTBLSIZE, M_DEVBUF, M_WAITOK);
if (!ctxmith)
ctxmith = malloc(4*LPIPTBLSIZE, M_DEVBUF, M_WAITOK);
for (i = 0; i < LPIPTBLSIZE; i++) {
ctxmith[i] = (i & 0xF0) >> 4;
ctxmitl[i] = 0x10 | (i & 0x0F);
ctrecvh[i] = (i & 0x78) << 1;
ctrecvl[i] = (i & 0x78) >> 3;
}
for (i = 0; i < LPIPTBLSIZE; i++) {
txmith[i] = ((i & 0x80) >> 3) | ((i & 0x70) >> 4) | 0x08;
txmitl[i] = ((i & 0x08) << 1) | (i & 0x07);
trecvh[i] = ((~i) & 0x80) | ((i & 0x38) << 1);
trecvl[i] = (((~i) & 0x80) >> 4) | ((i & 0x38) >> 3);
}
}
static void
lpfreetables(void)
{
if (txmith)
free(txmith, M_DEVBUF);
if (ctxmith)
free(ctxmith, M_DEVBUF);
txmith = ctxmith = NULL;
}
static int
lpioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct lp_softc * sc = ifp->if_softc;
device_t dev = sc->ppbus_dev.sc_dev;
device_t ppbus = device_parent(dev);
struct ifaddr * ifa = (struct ifaddr *)data;
struct ifreq * ifr = (struct ifreq *)data;
u_char * ptr;
int error, s;
error = 0;
s = splnet();
if (sc->sc_dev_ok) {
LP_PRINTF("%s(%s): device not properly attached!", __func__,
device_xname(dev));
error = ENODEV;
goto end;
}
switch (cmd) {
case SIOCSIFDSTADDR:
if (ifa->ifa_addr->sa_family != AF_INET)
error = EAFNOSUPPORT;
break;
case SIOCINITIFADDR:
if (ifa->ifa_addr->sa_family != AF_INET) {
error = EAFNOSUPPORT;
break;
}
ifp->if_flags |= IFF_UP;
case SIOCSIFFLAGS:
if (cmd == SIOCSIFFLAGS) {
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
break;
}
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_UP) {
if ((error = ppbus_request_bus(ppbus, dev, 0, 0)))
break;
error = ppbus_set_mode(ppbus, PPBUS_COMPATIBLE, 0);
if (error)
break;
error = ppbus_add_handler(ppbus, lp_intr, dev);
if (error) {
LP_PRINTF("%s(%s): unable to register interrupt"
" callback.\n", __func__,
device_xname(dev));
ppbus_release_bus(ppbus, dev, 0, 0);
break;
}
if (sc->sc_ifbuf == NULL) {
sc->sc_ifbuf = malloc(sc->sc_if.if_mtu +
MLPIPHDRLEN, M_DEVBUF, M_WAITOK);
}
ppbus_wctr(ppbus, IRQENABLE);
ifp->if_flags |= IFF_RUNNING;
}
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) == IFF_RUNNING) {
ppbus_remove_handler(ppbus, lp_intr);
error = ppbus_release_bus(ppbus, dev, 0, 0);
ifp->if_flags &= ~IFF_RUNNING;
}
ppbus_wdtr(ppbus, 0);
break;
case SIOCSIFMTU:
if (sc->sc_if.if_mtu == ifr->ifr_mtu)
break;
ptr = sc->sc_ifbuf;
sc->sc_ifbuf = malloc(ifr->ifr_mtu + MLPIPHDRLEN, M_DEVBUF,
M_WAITOK);
if (ptr)
free(ptr,M_DEVBUF);
case SIOCGIFMTU:
if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
error = 0;
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
if (ifr == NULL) {
error = EAFNOSUPPORT;
break;
}
switch (ifreq_getaddr(cmd, ifr)->sa_family) {
case AF_INET:
break;
default:
splx(s);
return EAFNOSUPPORT;
}
break;
case SIOCGIFMEDIA:
default:
LP_PRINTF("LP:ioctl(0x%lx)\n", cmd);
error = ifioctl_common(ifp, cmd, data);
}
end:
splx(s);
return error;
}
static inline int
clpoutbyte(u_char byte, int spin, device_t ppbus)
{
int s = spin;
ppbus_wdtr(ppbus, ctxmitl[byte]);
while (ppbus_rstr(ppbus) & CLPIP_SHAKE) {
if (--s == 0) {
return 1;
}
}
s = spin;
ppbus_wdtr(ppbus, ctxmith[byte]);
while (!(ppbus_rstr(ppbus) & CLPIP_SHAKE)) {
if (--s == 0) {
return 1;
}
}
return 0;
}
static inline int
clpinbyte(int spin, device_t ppbus)
{
u_char c, cl;
int s = spin;
while (ppbus_rstr(ppbus) & CLPIP_SHAKE) {
if (!--s) {
return -1;
}
}
cl = ppbus_rstr(ppbus);
ppbus_wdtr(ppbus, 0x10);
s = spin;
while (!(ppbus_rstr(ppbus) & CLPIP_SHAKE)) {
if (!--s) {
return -1;
}
}
c = ppbus_rstr(ppbus);
ppbus_wdtr(ppbus, 0x00);
return (ctrecvl[cl] | ctrecvh[c]);
}
static void
lptap(struct ifnet *ifp, struct mbuf *m, u_int direction)
{
u_int32_t af = AF_INET;
struct mbuf m0;
m0.m_type = MT_DATA;
m0.m_next = m;
m0.m_nextpkt = NULL;
m0.m_owner = NULL;
m0.m_len = sizeof(u_int32_t);
m0.m_data = (char *)⁡
m0.m_flags = 0;
bpf_mtap(ifp, &m0, direction);
}
static void
lp_intr(void *arg)
{
device_t dev = (device_t)arg;
device_t ppbus = device_parent(dev);
struct lp_softc * sc = device_private(dev);
struct ifnet * ifp = &sc->sc_if;
struct mbuf *top;
int len, s, j;
u_char *bp;
u_char c, cl;
s = splnet();
if (sc->sc_dev_ok) {
LP_PRINTF("%s(%s): device not properly attached!", __func__,
device_xname(dev));
goto done;
}
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
goto done;
if (!(ppbus_rstr(ppbus) & LPIP_SHAKE))
goto done;
ppbus_wctr(ppbus, ~IRQENABLE);
top = NULL;
bp = sc->sc_ifbuf;
if (ifp->if_flags & IFF_LINK0) {
ppbus_wdtr(ppbus, 0x01);
j = clpinbyte(LPMAXSPIN2, ppbus);
if (j == -1)
goto err;
len = j;
j = clpinbyte(LPMAXSPIN2, ppbus);
if (j == -1)
goto err;
len = len + (j << 8);
if (len > ifp->if_mtu + MLPIPHDRLEN)
goto err;
while (len--) {
j = clpinbyte(LPMAXSPIN2, ppbus);
if (j == -1) {
goto err;
}
*bp++ = j;
}
j = clpinbyte(LPMAXSPIN2, ppbus);
if (j == -1) {
goto err;
}
ppbus_wdtr(ppbus, 0);
len = bp - sc->sc_ifbuf;
if (len <= CLPIPHDRLEN)
goto err;
len -= CLPIPHDRLEN;
top = m_devget(sc->sc_ifbuf + CLPIPHDRLEN, len, 0, ifp);
}
else {
len = ifp->if_mtu + LPIPHDRLEN;
while (len--) {
cl = ppbus_rstr(ppbus);
ppbus_wdtr(ppbus, 0x08);
j = LPMAXSPIN2;
while ((ppbus_rstr(ppbus) & LPIP_SHAKE)) {
if (!--j)
goto err;
}
c = ppbus_rstr(ppbus);
ppbus_wdtr(ppbus, 0);
*bp++= trecvh[cl] | trecvl[c];
j = LPMAXSPIN2;
while (!((cl = ppbus_rstr(ppbus)) & LPIP_SHAKE)) {
if (cl != c &&
(((cl = ppbus_rstr(ppbus)) ^ 0xb8) &
0xf8) == (c & 0xf8))
goto end;
if (!--j)
goto err;
}
}
end:
len = bp - sc->sc_ifbuf;
if (len <= LPIPHDRLEN)
goto err;
len -= LPIPHDRLEN;
top = m_devget(sc->sc_ifbuf + LPIPHDRLEN, len, 0, ifp);
}
if (top == NULL) {
if_statinc(ifp, if_iqdrops);
goto err;
}
if (ifp->if_bpf) {
lptap(ifp, top, BPF_D_IN);
}
if (__predict_false(!pktq_enqueue(ip_pktq, top, 0))) {
if_statinc(ifp, if_iqdrops);
m_freem(top);
goto err;
}
if_statinc(ifp, if_ipackets);
if_statadd(ifp, if_ibytes, len);
sc->sc_iferrs = 0;
goto done;
err:
ppbus_wdtr(ppbus, 0);
if_statinc(ifp, if_ierrors);
sc->sc_iferrs++;
LP_PRINTF("R");
if (sc->sc_iferrs > LPMAXERRS) {
aprint_error_dev(dev, "Too many consecutive errors, going off-line.\n");
ppbus_wctr(ppbus, ~IRQENABLE);
if_down(ifp);
sc->sc_iferrs = 0;
}
done:
ppbus_wctr(ppbus, IRQENABLE);
if ((ifp->if_flags & IFF_OACTIVE) == 0)
lpstart(ifp);
splx(s);
return;
}
static inline int
lpoutbyte(u_char byte, int spin, device_t ppbus)
{
int s = spin;
ppbus_wdtr(ppbus, txmith[byte]);
while (!(ppbus_rstr(ppbus) & LPIP_SHAKE)) {
if (--s == 0)
return 1;
}
s = spin;
ppbus_wdtr(ppbus, txmitl[byte]);
while (ppbus_rstr(ppbus) & LPIP_SHAKE) {
if (--s == 0)
return 1;
}
return 0;
}
static int
lpoutput(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
const struct rtentry *rt)
{
struct lp_softc * sc = ifp->if_softc;
device_t dev = sc->ppbus_dev.sc_dev;
device_t ppbus = device_parent(dev);
int err;
int s;
s = splnet();
if (sc->sc_dev_ok) {
LP_PRINTF("%s(%s): device not properly attached!", __func__,
device_xname(dev));
err = ENODEV;
goto endoutput;
}
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
err = ENETDOWN;
goto endoutput;
}
if (dst->sa_family != AF_INET) {
LP_PRINTF("%s: af%d not supported\n", ifp->if_xname,
dst->sa_family);
if_statinc(ifp, if_noproto);
err = EAFNOSUPPORT;
goto endoutput;
}
IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
IFQ_ENQUEUE(&ifp->if_snd, m, err);
if (err == 0) {
if ((ifp->if_flags & IFF_OACTIVE) == 0)
lpstart(ifp);
} else {
if_statinc(ifp, if_oerrors);
sc->sc_iferrs++;
LP_PRINTF("Q");
if (sc->sc_iferrs > LPMAXERRS) {
aprint_error_dev(dev, "Too many errors, going off-line.\n");
ppbus_wctr(ppbus, ~IRQENABLE);
if_down(ifp);
sc->sc_iferrs = 0;
}
}
endoutput:
if ((err != 0) && (err != ENOBUFS))
m_freem(m);
splx(s);
return err;
}
void
lpstart(struct ifnet * ifp)
{
struct lp_softc * lp = ifp->if_softc;
device_t dev = lp->ppbus_dev.sc_dev;
device_t ppbus = device_parent(dev);
struct mbuf * mm;
struct mbuf * m;
u_char * cp;
int err, i, len, spin, count;
u_char str, chksum;
if (lp->sc_dev_ok) {
LP_PRINTF("%s(%s): device not properly attached!", __func__,
device_xname(dev));
return;
}
if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING)) {
return;
}
ifp->if_flags |= IFF_OACTIVE;
ppbus_wdtr(ppbus, 0);
for (;;) {
if (IFQ_IS_EMPTY(&ifp->if_snd)) {
goto final;
}
IFQ_POLL(&ifp->if_snd, m);
if (m == NULL)
goto final;
str = ppbus_rstr(ppbus);
if ((str & LPIP_SHAKE) ||
((ifp->if_flags & IFF_LINK0) && !(str & CLPIP_SHAKE))) {
LP_PRINTF("&");
if (++lp->sc_xmit_rtry > LPMAXRTRY) {
aprint_error_dev(dev, "Too many retries while channel "
"busy, going off-line.\n");
ppbus_wctr(ppbus, ~IRQENABLE);
if_down(ifp);
lp->sc_xmit_rtry = 0;
}
goto final;
}
lp->sc_xmit_rtry = 0;
ppbus_wctr(ppbus, ~IRQENABLE);
err = 1;
if (ifp->if_flags & IFF_LINK0) {
count = 14;
for (mm = m; mm; mm = mm->m_next) {
count += mm->m_len;
}
spin = LPMAXSPIN1;
ppbus_wdtr(ppbus, 0x08);
while ((ppbus_rstr(ppbus) & 0x08) == 0) {
if (--spin == 0) {
goto nend;
}
}
if (clpoutbyte(count & 0xFF, LPMAXSPIN1, ppbus))
goto nend;
if (clpoutbyte((count >> 8) & 0xFF, LPMAXSPIN1, ppbus))
goto nend;
chksum = 0;
for (i = 0; i < 12; i++) {
if (clpoutbyte(i, LPMAXSPIN1, ppbus))
goto nend;
chksum += i;
}
if (clpoutbyte(0x08, LPMAXSPIN1, ppbus))
goto nend;
if (clpoutbyte(0x00, LPMAXSPIN1, ppbus))
goto nend;
chksum += 0x08 + 0x00;
mm = m;
do {
cp = mtod(mm, u_char *);
len = mm->m_len;
while (len--) {
if (clpoutbyte(*cp, LPMAXSPIN2, ppbus))
goto nend;
chksum += *cp++;
}
} while ((mm = mm->m_next));
if (clpoutbyte(chksum, LPMAXSPIN2, ppbus))
goto nend;
err = 0;
ppbus_wdtr(ppbus, 0);
}
else {
cp = NULL;
if (lpoutbyte(0x08, LPMAXSPIN1, ppbus))
goto end;
if (lpoutbyte(0x00, LPMAXSPIN2, ppbus))
goto end;
mm = m;
do {
cp = mtod(mm,u_char *);
len = mm->m_len;
while (len--)
if (lpoutbyte(*cp++, LPMAXSPIN2, ppbus))
goto end;
} while ((mm = mm->m_next));
err = 0;
end:
if (cp)
ppbus_wdtr(ppbus, txmitl[*(--cp)] ^ 0x17);
else
ppbus_wdtr(ppbus, txmitl['\0'] ^ 0x17);
}
nend:
ppbus_wctr(ppbus, IRQENABLE);
if (err) {
ppbus_wdtr(ppbus, 0);
if_statinc(ifp, if_oerrors);
lp->sc_iferrs++;
LP_PRINTF("X");
if (lp->sc_iferrs > LPMAXERRS) {
aprint_error_dev(dev, "Too many errors, going off-line.\n");
ppbus_wctr(ppbus, ~IRQENABLE);
if_down(ifp);
lp->sc_iferrs = 0;
goto final;
}
} else {
IFQ_DEQUEUE(&ifp->if_snd, m);
if(ifp->if_bpf)
lptap(ifp, m, BPF_D_OUT);
if_statinc(ifp, if_opackets);
if_statadd(ifp, if_obytes, m->m_pkthdr.len);
m_freem(m);
}
}
final:
ifp->if_flags &= ~IFF_OACTIVE;
return;
}