/* $NetBSD: if_il.c,v 1.37 2021/08/01 15:29:30 andvar Exp $ */
/*
* Copyright (c) 1982, 1986 Regents of the University of California.
* 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. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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.
*
* @(#)if_il.c 7.8 (Berkeley) 12/16/90
*/
/*
* Ethernet software status per interface.
*
* Each interface is referenced by a network interface structure,
* is_if, which the routing code uses to locate the interface.
* This structure contains the output queue for the interface, its address, ...
* We also have, for each interface, a UBA interface structure, which
* contains information about the UNIBUS resources held by the interface:
* map registers, buffered data paths, etc. Information is cached in this
* structure for use by the if_uba.c routines in running the interface
* efficiently.
*/
/*
* Interface exists: make available by filling in network interface
* record. System will initialize the interface when it is ready
* to accept packets. A STATUS command is done to get the ethernet
* address and other interesting data.
*/
void
ilattach(device_t parent, device_t self, void *aux)
{
struct uba_attach_args *ua = aux;
struct il_softc *sc = device_private(self);
struct ifnet *ifp = &sc->sc_if;
int error;
/*
* Reset of interface after UNIBUS reset.
* If interface is on specified uba, reset its state.
*/
void
ilreset(device_t dev)
{
struct il_softc *sc = (void *)dev;
/*
* Turn off source address insertion (it's faster this way),
* and set board online. Former doesn't work if board is
* already online (happens on ubareset), so we put it offline
* first.
*/
s = splnet();
IL_WCSR(IL_CSR, ILC_RESET);
if (ilwait(sc, "hardware diag")) {
sc->sc_if.if_flags &= ~IFF_UP;
goto out;
}
IL_WCSR(IL_CSR, ILC_CISA);
while ((IL_RCSR(IL_CSR) & IL_CDONE) == 0)
;
/*
* If we must reprogram this board's physical ethernet
* address (as for secondary XNS interfaces), we do so
* before putting it on line, and starting receive requests.
* If you try this on an older 1010 board, it will total
* wedge the board.
*/
if (sc->sc_flags & ILF_SETADDR) {
memcpy(&sc->sc_isu, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
IL_WCSR(IL_BAR, LOWORD(sc->sc_ui.ui_baddr));
IL_WCSR(IL_BCR, ETHER_ADDR_LEN);
IL_WCSR(IL_CSR,
((sc->sc_ui.ui_baddr >> 2) & IL_EUA) | ILC_LDPA);
if (ilwait(sc, "setaddr"))
goto out;
IL_WCSR(IL_BAR, LOWORD(sc->sc_ui.ui_baddr));
IL_WCSR(IL_BCR, sizeof (struct il_stats));
IL_WCSR(IL_CSR,
((sc->sc_ui.ui_baddr >> 2) & IL_EUA) | ILC_STAT);
if (ilwait(sc, "verifying setaddr"))
goto out;
if (memcmp(sc->sc_stats.ils_addr,
CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN) != 0) {
aprint_error_dev(sc->sc_dev, "setaddr didn't work\n");
goto out;
}
}
if (sc->sc_if.if_flags & IFF_PROMISC) {
IL_WCSR(IL_CSR, ILC_PRMSC);
if (ilwait(sc, "all multi"))
goto out;
} else if (sc->sc_if.if_flags & IFF_ALLMULTI) {
too_many_multis:
IL_WCSR(IL_CSR, ILC_ALLMC);
if (ilwait(sc, "all multi"))
goto out;
} else {
int i;
struct ethercom *ec = &sc->sc_ec;
register struct ether_addr *ep = sc->sc_maddrs;
struct ether_multi *enm;
struct ether_multistep step;
/*
* Step through our list of multicast addresses. If we have
* too many multicast addresses, or if we have to listen to
* a range of multicast addresses, turn on reception of all
* multicasts.
*/
i = 0;
ETHER_LOCK(ec);
ETHER_FIRST_MULTI(step, ec, enm);
while (enm != NULL) {
if (++i > 63 /* && k != 0 */) {
break;
}
*ep++ = *(struct ether_addr *)enm->enm_addrlo;
ETHER_NEXT_MULTI(step, enm);
}
ETHER_UNLOCK(ec);
if (i == 0) {
/* no multicasts! */
} else if (i <= 63) {
IL_WCSR(IL_BAR, sc->sc_ubaddr & 0xffff);
IL_WCSR(IL_BCR, i * sizeof(struct ether_addr));
IL_WCSR(IL_CSR,
((sc->sc_ubaddr >> 2) & IL_EUA) | ILC_LDGRPS);
if (ilwait(sc, "load multi"))
goto out;
} else {
sc->sc_if.if_flags |= IFF_ALLMULTI;
goto too_many_multis;
}
}
/*
* Set board online.
* Hang receive buffer and start any pending
* writes by faking a transmit complete.
* Receive bcr is not a multiple of 8 so buffer
* chaining can't happen.
*/
IL_WCSR(IL_CSR, ILC_ONLINE);
while ((IL_RCSR(IL_CSR) & IL_CDONE) == 0)
;
/*
* Start output on interface.
* Get another datagram to send off of the interface queue,
* and map it to the interface before starting the output.
*/
void
ilstart(struct ifnet *ifp)
{
struct il_softc *sc = ifp->if_softc;
int len;
struct mbuf *m;
short csr;
case ILC_XMIT:
sc->sc_if.if_opackets++;
if (csr > ILERR_RETRIES)
sc->sc_if.if_oerrors++;
break;
case ILC_STAT:
if (csr == ILERR_SUCCESS)
iltotal(sc);
break;
}
if_wubaend(&sc->sc_ifuba);
ilstart(&sc->sc_if);
}
/*
* Ethernet interface receiver interrupt.
* If input error just drop packet.
* Otherwise purge input buffered data path and examine
* packet to determine type. If can't determine length
* from type, then have to drop packet. Otherwise decapsulate
* packet based on type and pass to type specific higher-level
* input routine.
*/
void
ilrint(void *arg)
{
struct il_softc *sc = arg;
struct il_rheader *il;
struct mbuf *m;
int len, s;
#ifdef notyet
if (sc->sc_ifuba.ifu_flags & UBA_NEEDBDP)
UBAPURGE(is->is_ifuba.ifu_uba, is->is_ifuba.ifu_r.ifrw_bdp);
#endif
il = (struct il_rheader *)(sc->sc_ifuba.ifu_r.ifrw_addr);
len = il->ilr_length - sizeof(struct il_rheader);
if ((il->ilr_status&(ILFSTAT_A | ILFSTAT_C)) || len < 46 ||
len > ETHERMTU) {
sc->sc_if.if_ierrors++;
#ifdef notdef
if (sc->sc_if.if_ierrors % 100 == 0)
printf("il%d: += 100 input errors\n", unit);
#endif
goto setup;
}
if (len == 0)
goto setup;
/*
* Pull packet off interface.
*/
m = if_rubaget(&sc->sc_ifuba, &sc->sc_if, len);
if (m == NULL)
goto setup;
/* Shave off status hdr */
m_adj(m, 4);
if_percpuq_enqueue((&sc->sc_if)->if_percpuq, m);
setup:
/*
* Reset for next packet if possible.
* If waiting for transmit command completion, set flag
* and wait until command completes.
*/
if (sc->sc_if.if_flags & IFF_OACTIVE) {
sc->sc_flags |= ILF_RCVPENDING;
return;
}
IL_WCSR(IL_BAR, LOWORD(sc->sc_ifuba.ifu_r.ifrw_info));
IL_WCSR(IL_BCR, sizeof(struct il_rheader) + ETHERMTU + 6);
IL_WCSR(IL_CSR,
((sc->sc_ifuba.ifu_r.ifrw_info >> 2) & IL_EUA) | ILC_RCV | IL_RIE);
s = splhigh();
while ((IL_RCSR(IL_CSR) & IL_CDONE) == 0)
;
splx(s);
}
/*
* Watchdog routine, request statistics from board.
*/
void
ilwatch(struct ifnet *ifp)
{
struct il_softc *sc = ifp->if_softc;
int s;
if (sc->sc_flags & ILF_STATPENDING) {
ifp->if_timer = sc->sc_scaninterval;
return;
}
s = splnet();
sc->sc_flags |= ILF_STATPENDING;
if ((sc->sc_if.if_flags & IFF_OACTIVE) == 0)
ilstart(ifp);
splx(s);
ifp->if_timer = sc->sc_scaninterval;
}
/*
* Total up the on-board statistics.
*/
void
iltotal(struct il_softc *sc)
{
struct ifnet *ifp = &sc->sc_if;
u_short *interval, *sum, *end;