/* $NetBSD: if_ec.c,v 1.37 2024/07/05 04:31:50 rin Exp $ */
/*
* Copyright (c) 2001 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Matthew Fredette.
*
* 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.
*/
struct ethercom sc_ethercom; /* ethernet common */
struct ifmedia sc_media; /* our supported media */
bus_space_tag_t sc_iot; /* bus space tag */
bus_space_handle_t sc_ioh; /* bus space handle */
bool sc_txbusy;
u_char sc_jammed; /* nonzero if the net is jammed */
u_char sc_colliding; /* nonzero if the net is colliding */
uint32_t sc_backoff_seed; /* seed for the backoff PRNG */
krndsource_t rnd_source;
};
/* Macros to read and write the CSR. */
#define ECREG_CSR_RD bus_space_read_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR)
#define ECREG_CSR_WR(val) bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_CSR, val)
/* After this many collisions, the packet is dropped. */
#define EC_COLLISIONS_JAMMED 16
/*
* Various constants used in the backoff pseudorandom
* number generator.
*/
#define EC_BACKOFF_PRNG_COLL_MAX 10
#define EC_BACKOFF_PRNG_MUL 1103515245
#define EC_BACKOFF_PRNG_ADD 12345
#define EC_BACKOFF_PRNG_MASK 0x7fffffff
/*
* Prototypes
*/
int ec_intr(void *);
void ec_reset(struct ifnet *);
int ec_init(struct ifnet *);
int ec_ioctl(struct ifnet *, u_long, void *);
void ec_watchdog(struct ifnet *);
void ec_start(struct ifnet *);
/* Map in the board control regs. */
sc->sc_iot = mbma->mbma_bustag;
if (bus_space_map(mbma->mbma_bustag, mbma->mbma_paddr, ECREG_BANK_SZ,
0, &sc->sc_ioh))
panic("%s: can't map regs", __func__);
/* Reset the board. */
ECREG_CSR_WR(EC_CSR_RESET);
delay(160);
/*
* Copy out the board ROM Ethernet address,
* and use the non-vendor-ID part to seed
* our backoff pseudorandom number generator.
*/
bus_space_read_region_1(sc->sc_iot, sc->sc_ioh,
ECREG_AROM, myaddr, ETHER_ADDR_LEN);
sc->sc_backoff_seed =
(myaddr[3] << 16) | (myaddr[4] << 8) | (myaddr[5]) | 1;
/* Now we can attach the interface. */
if_attach(ifp);
if_deferred_start_init(ifp, NULL);
idprom_etheraddr(myaddr);
ether_ifattach(ifp, myaddr);
aprint_normal_dev(self, "address %s\n", ether_sprintf(myaddr));
/* Don't do anything if output is active. */
if (sc->sc_txbusy) {
splx(s);
return;
}
/* Don't do anything if the output queue is empty. */
IFQ_DEQUEUE(&ifp->if_snd, m0);
if (m0 == NULL) {
splx(s);
return;
}
/* The BPF tap. */
bpf_mtap(ifp, m0, BPF_D_OUT);
/* Size the packet. */
count = EC_BUF_SZ - m0->m_pkthdr.len;
/* Copy the packet into the xmit buffer. */
realcount = MIN(count, EC_PKT_MAXTDOFF);
bus_space_write_2(sc->sc_iot, sc->sc_ioh, ECREG_TBUF, realcount);
for (off = realcount, m = m0; m != 0; off += m->m_len, m = m->m_next)
ec_copyout(sc, mtod(m, uint8_t *), ECREG_TBUF + off, m->m_len);
m_freem(m0);
if (count - realcount)
ec_copyout(sc, padding, ECREG_TBUF + off, count - realcount);
/*
* Controller interrupt.
*/
int
ec_intr(void *arg)
{
struct ec_softc *sc = arg;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
int recv_first;
int recv_second;
int retval;
retval = 0;
/* Check for received packet(s). */
recv_first = recv_second = 0;
switch (ECREG_CSR_RD & (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA)) {
case (EC_CSR_BBSW | EC_CSR_ABSW):
case (EC_CSR_BBSW | EC_CSR_ABSW | EC_CSR_RBBA):
/* Neither buffer is full. Is this a transmit interrupt?
* Acknowledge the interrupt ourselves. */
ECREG_CSR_WR(ECREG_CSR_RD &
(EC_CSR_TINT | EC_CSR_JINT | EC_CSR_PAMASK));
ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
EC_CSR_BINT | EC_CSR_AINT);
break;
case EC_CSR_BBSW:
case (EC_CSR_BBSW | EC_CSR_RBBA):
/* Only the A buffer is full. */
recv_first = EC_CSR_AINT;
break;
case EC_CSR_ABSW:
case (EC_CSR_ABSW | EC_CSR_RBBA):
/* Only the B buffer is full. */
recv_first = EC_CSR_BINT;
break;
case 0:
/* Both the A buffer and the B buffer are full, and the A
* buffer is older than the B buffer. */
recv_first = EC_CSR_AINT;
recv_second = EC_CSR_BINT;
break;
case EC_CSR_RBBA:
/* Both the A buffer and the B buffer are full, and the B
* buffer is older than the A buffer. */
recv_first = EC_CSR_BINT;
recv_second = EC_CSR_AINT;
break;
}
/* Give the receive buffer back to the card. */
buf = EC_CSR_INT_BUF(intbit);
bus_space_write_2(sc->sc_iot, sc->sc_ioh, buf, 0);
ECREG_CSR_WR((ECREG_CSR_RD & EC_CSR_INTPA) |
EC_CSR_INT_BSW(intbit) | intbit);
}
/*
* Process an ioctl request. This code needs some work - it looks pretty ugly.
*/
int
ec_ioctl(struct ifnet *ifp, u_long cmd, void *data)
{
struct ifaddr *ifa = (struct ifaddr *)data;
int s, error = 0;
case SIOCSIFFLAGS:
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
break;
switch (ifp->if_flags & (IFF_UP | IFF_RUNNING)) {
case IFF_RUNNING:
/*
* If interface is marked down and it is running, then
* stop it.
*/
ifp->if_flags &= ~IFF_RUNNING;
break;
case IFF_UP:
/*
* If interface is marked up and it is stopped, then
* start it.
*/
ec_init(ifp);
break;
default:
/*
* Some other important flag might have changed, so
* reset.
*/
ec_reset(ifp);
break;
}
break;