/*
* Copyright (c) 1994, Matthew E. Kimmel. Permission is hereby granted
* to use, copy, modify and distribute this software provided that both
* the copyright notice and this permission notice appear in all copies
* of the software, derivative works or modified versions, and any
* portions thereof.
*/
/*
* 3COM Etherlink 3C501 device driver
*/
/*
* Bugs/possible improvements:
* - Does not currently support DMA
* - Does not currently support multicasts
*/
/*
* Probe routine.
*
* See if the card is there and at the right place.
* (XXX - cgd -- needs help)
*/
static int
elprobe(device_t parent, cfdata_t match, void *aux)
{
struct isa_attach_args *ia = aux;
bus_space_tag_t iot = ia->ia_iot;
bus_space_handle_t ioh;
int iobase;
uint8_t station_addr[ETHER_ADDR_LEN];
uint8_t i;
int rval;
rval = 0;
if (ia->ia_nio < 1)
return (0);
if (ia->ia_nirq < 1)
return (0);
if (ISA_DIRECT_CONFIG(ia))
return (0);
iobase = ia->ia_io[0].ir_addr;
if (ia->ia_io[0].ir_addr == ISA_UNKNOWN_PORT)
return (0);
if (ia->ia_irq[0].ir_irq == ISA_UNKNOWN_IRQ)
return (0);
/* First check the base. */
if (iobase < 0x200 || iobase > 0x3f0)
return 0;
/*
* Now attempt to grab the station address from the PROM and see if it
* contains the 3com vendor code.
*/
DPRINTF(("Probing 3c501 at 0x%x...\n", iobase));
/* Now read the address. */
DPRINTF(("Reading station address...\n"));
for (i = 0; i < ETHER_ADDR_LEN; i++) {
bus_space_write_1(iot, ioh, EL_GPBL, i);
station_addr[i] = bus_space_read_1(iot, ioh, EL_EAW);
}
DPRINTF(("Address is %s\n", ether_sprintf(station_addr)));
/*
* If the vendor code is ok, return a 1. We'll assume that whoever
* configured this system is right about the IRQ.
*/
if (station_addr[0] != 0x02 || station_addr[1] != 0x60 ||
station_addr[2] != 0x8c) {
DPRINTF(("Bad vendor code.\n"));
goto out;
}
DPRINTF(("Vendor code ok.\n"));
/*
* Attach the interface to the kernel data structures. By the time this is
* called, we know that the card exists at the given I/O address. We still
* assume that the IRQ given is correct.
*/
static void
elattach(device_t parent, device_t self, void *aux)
{
struct el_softc *sc = device_private(self);
struct isa_attach_args *ia = aux;
bus_space_tag_t iot = ia->ia_iot;
bus_space_handle_t ioh;
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
uint8_t myaddr[ETHER_ADDR_LEN];
uint8_t i;
/* Now read the address. */
for (i = 0; i < ETHER_ADDR_LEN; i++) {
bus_space_write_1(iot, ioh, EL_GPBL, i);
myaddr[i] = bus_space_read_1(iot, ioh, EL_EAW);
}
/*
* Do a hardware reset of the board, and upload the ethernet address again in
* case the board forgets.
*/
static inline void
el_hardreset(struct el_softc *sc)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
int i;
/*
* Start output on interface. Get datagrams from the queue and output them,
* giving the receiver a chance between datagrams. Call only from splnet or
* interrupt level!
*/
static void
elstart(struct ifnet *ifp)
{
struct el_softc *sc = ifp->if_softc;
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
struct mbuf *m, *m0;
int s, i, off, retries;
DPRINTF(("elstart()...\n"));
s = splnet();
/* Don't do anything if output is active. */
if (sc->sc_txbusy) {
splx(s);
return;
}
sc->sc_txbusy = true;
/*
* The main loop. They warned me against endless loops, but would I
* listen? NOOO....
*/
for (;;) {
/* Dequeue the next datagram. */
IFQ_DEQUEUE(&ifp->if_snd, m0);
/* If there's nothing to send, return. */
if (m0 == 0)
break;
/* Give the packet to the bpf, if any. */
bpf_mtap(ifp, m0, BPF_D_OUT);
/* Transfer datagram to board. */
DPRINTF(("el: xfr pkt length=%d...\n", m0->m_pkthdr.len));
off = EL_BUFSIZ - uimax(m0->m_pkthdr.len,
ETHER_MIN_LEN - ETHER_CRC_LEN);
#ifdef DIAGNOSTIC
if ((off & 0xffff) != off)
printf("%s: bogus off 0x%x\n",
device_xname(sc->sc_dev), off);
#endif
bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
/* Copy the datagram to the buffer. */
for (m = m0; m != 0; m = m->m_next)
bus_space_write_multi_1(iot, ioh, EL_BUF,
mtod(m, uint8_t *), m->m_len);
for (i = 0;
i < ETHER_MIN_LEN - ETHER_CRC_LEN - m0->m_pkthdr.len; i++)
bus_space_write_1(iot, ioh, EL_BUF, 0);
m_freem(m0);
/* Now transmit the datagram. */
retries = 0;
for (;;) {
bus_space_write_1(iot, ioh, EL_GPBL, off & 0xff);
bus_space_write_1(iot, ioh, EL_GPBH, (off >> 8) & 0xff);
if (el_xmit(sc)) {
if_statinc(ifp, if_oerrors);
break;
}
/* Check out status. */
i = bus_space_read_1(iot, ioh, EL_TXS);
DPRINTF(("tx status=0x%x\n", i));
if ((i & EL_TXS_READY) == 0) {
DPRINTF(("el: err txs=%x\n", i));
if (i & (EL_TXS_COLL | EL_TXS_COLL16)) {
if_statinc(ifp, if_collisions);
if ((i & EL_TXC_DCOLL16) == 0 &&
retries < 15) {
retries++;
bus_space_write_1(iot, ioh,
EL_AC, EL_AC_HOST);
}
} else {
if_statinc(ifp, if_oerrors);
break;
}
} else {
if_statinc(ifp, if_opackets);
break;
}
}
/*
* Now give the card a chance to receive.
* Gotta love 3c501s...
*/
(void)bus_space_read_1(iot, ioh, EL_AS);
bus_space_write_1(iot, ioh, EL_AC, EL_AC_IRQE | EL_AC_RX);
splx(s);
/* (Maybe) interrupt here. */
s = splnet();
}
/*
* This function actually attempts to transmit a datagram downloaded to the
* board. Call at splnet or interrupt, after downloading data! Returns 0 on
* success, non-0 on failure.
*/
static int
el_xmit(struct el_softc *sc)
{
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
int i;
/*
* XXX
* This busy-waits for the tx completion. Can we get an interrupt
* instead?
*/
DPRINTF(("el: xmit..."));
bus_space_write_1(iot, ioh, EL_AC, EL_AC_TXFRX);
i = 20000;
while ((bus_space_read_1(iot, ioh, EL_AS) & EL_AS_TXBUSY) && (i > 0))
i--;
if (i == 0) {
DPRINTF(("tx not ready\n"));
return -1;
}
DPRINTF(("%d cycles.\n", 20000 - i));
return 0;
}
/* Pull packet off interface. */
m = elget(sc, len);
if (m == 0) {
if_statinc(ifp, if_ierrors);
return;
}
if_percpuq_enqueue(ifp->if_percpuq, m);
}
/*
* Pull read data off a interface. Len is length of data, with local net
* header stripped. We copy the data into mbufs. When full cluster sized
* units are present we copy into clusters.
*/
static struct mbuf *
elget(struct el_softc *sc, int totlen)
{
struct ifnet *ifp = &sc->sc_ethercom.ec_if;
bus_space_tag_t iot = sc->sc_iot;
bus_space_handle_t ioh = sc->sc_ioh;
struct mbuf *m, *m0, *newm;
int len;
MGETHDR(m0, M_DONTWAIT, MT_DATA);
if (m0 == 0)
return (0);
m_set_rcvif(m0, ifp);
m0->m_pkthdr.len = totlen;
len = MHLEN;
m = m0;
case SIOCSIFFLAGS:
if ((error = ifioctl_common(ifp, cmd, data)) != 0)
break;
/* XXX re-use ether_ioctl() */
switch (ifp->if_flags & (IFF_UP|IFF_RUNNING)) {
case IFF_RUNNING:
/*
* If interface is marked down and it is running, then
* stop it.
*/
elstop(sc);
ifp->if_flags &= ~IFF_RUNNING;
break;
case IFF_UP:
/*
* If interface is marked up and it is stopped, then
* start it.
*/
elinit(sc);
break;
default:
/*
* Some other important flag might have changed, so
* reset.
*/
elreset(sc);
break;
}
break;