/*
* Copyright (c) 1993 Adam Glass
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Adam Glass.
* 4. The name of the Author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Adam Glass ``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.
*/
#ifdef LE_DEBUG
if (le_debug)
printf("le%d: DIO=%x regs=%x mem=%x\n",
i, sc->sc_r0, sc->sc_r1, sc->sc_mem);
#endif
/*
* Read the ethernet address off the board, one nibble at a time.
*/
cp = (char *)(sels->le_nvram + (int)hw->hw_kva);
for (n = 0; n < sizeof(sc->sc_addr); n++) {
sc->sc_addr[n] = (*++cp & 0xF) << 4;
cp++;
sc->sc_addr[n] |= *++cp & 0xF;
cp++;
}
#ifdef LE_DEBUG
if (le_debug)
printf("le%d at sc%d physical address %s\n",
i, hw->hw_sc, ether_sprintf(sc->sc_addr));
#endif
hw->hw_pa = (void *) i; /* XXX for autoconfig */
i++;
}
}
static int
le_match(struct netif *nif, void *machdep_hint)
{
struct le_sel *sels;
char *name = machdep_hint;
int rv = 0;
if (nif->nif_sel < le_ifs[nif->nif_unit].dif_nsel) {
sels = (struct le_sel *)le_ifs[nif->nif_unit].dif_private;
rv = sels[nif->nif_sel].le_heat;
if (name && !strncmp(le_driver.netif_bname, name, 2))
rv += sels[nif->nif_sel].le_bonus;
}
#ifdef LE_DEBUG
if (le_debug)
printf("le%d: sel %d --> %d\n", nif->nif_unit, nif->nif_sel,
rv);
#endif
return rv;
}
static int
le_probe(struct netif *nif, void *machdep_hint)
{
#if 0
char *cp;
int i;
#endif
/* the set unit is the current unit */
#ifdef LE_DEBUG
if (le_debug)
printf("le%d.%d: le_probe called\n", nif->nif_unit, nif->nif_sel);
#endif
/* XXX reset controller */
return 0;
}
printf("le%d: ler1 = %x\n", unit, ler1);
printf("le%d: ler2 = %x\n", unit, ler2);
#if 0
ler1->ler1_rap = LE_CSR0;
ler1->ler1_rdp = LE_STOP;
printf("le%d: csr0 = %x\n", unit, ler1->ler1_rdp);
ler1->ler1_rap = LE_CSR1;
printf("le%d: csr1 = %x\n", unit, ler1->ler1_rdp);
ler1->ler1_rap = LE_CSR2;
printf("le%d: csr2 = %x\n", unit, ler1->ler1_rdp);
ler1->ler1_rap = LE_CSR3;
printf("le%d: csr3 = %x\n", unit, ler1->ler1_rdp);
#endif
printf("le%d: ladrf[0] = %x\n", unit, ler2->ler2_ladrf[0]);
printf("le%d: ladrf[1] = %x\n", unit, ler2->ler2_ladrf[1]);
printf("le%d: ler2_rdra = %x\n", unit, ler2->ler2_rdra);
printf("le%d: ler2_rlen = %x\n", unit, ler2->ler2_rlen);
printf("le%d: ler2_tdra = %x\n", unit, ler2->ler2_tdra);
printf("le%d: ler2_tlen = %x\n", unit, ler2->ler2_tlen);
for (i = 0; i < LERBUF; i++) {
printf("le%d: ler2_rmd[%d].rmd0 (ladr) = %x\n", unit, i,
ler2->ler2_rmd[i].rmd0);
printf("le%d: ler2_rmd[%d].rmd1 = %x\n", unit, i,
ler2->ler2_rmd[i].rmd1);
printf("le%d: ler2_rmd[%d].rmd2 (-bcnt) = %x\n", unit, i,
ler2->ler2_rmd[i].rmd2);
printf("le%d: ler2_rmd[%d].rmd3 (mcnt) = %x\n", unit, i,
ler2->ler2_rmd[i].rmd3);
printf("le%d: ler2_rbuf[%d] addr = %x\n", unit, i,
&ler2->ler2_rbuf[i]);
}
for (i = 0; i < LETBUF; i++) {
printf("le%d: ler2_tmd[%d].tmd0 = %x\n", unit, i,
ler2->ler2_tmd[i].tmd0);
printf("le%d: ler2_tmd[%d].tmd1 = %x\n", unit, i,
ler2->ler2_tmd[i].tmd1);
printf("le%d: ler2_tmd[%d].tmd2 (bcnt) = %x\n", unit, i,
ler2->ler2_tmd[i].tmd2);
printf("le%d: ler2_tmd[%d].tmd3 = %x\n", unit, i,
ler2->ler2_tmd[i].tmd3);
printf("le%d: ler2_tbuf[%d] addr = %x\n", unit, i,
&ler2->ler2_tbuf[i]);
}
}
#else
#define le_mem_summary(u)
#endif
static void
le_error(int unit, char *str, uint16_t stat)
{
if (stat & LE_BABL)
panic("le%d: been babbling, found by '%s'", unit, str);
if (stat & LE_CERR)
le_stats[unit].collision_error++;
if (stat & LE_MISS)
le_stats[unit].missed++;
if (stat & LE_MERR) {
printf("le%d: memory error in '%s'\n", unit, str);
le_mem_summary(unit);
panic("bye");
}
}
#define LANCE_ADDR(sc, a) \
((u_long)(a) - (u_long)sc->sc_mem)
/* LANCE initialization block set up. */
static void
lememinit(struct le_softc *sc)
{
int i;
u_char *mem;
u_long a;
/*
* At this point we assume that the memory allocated to the Lance is
* quadword aligned. If it isn't then the initialisation is going
* fail later on.
*/
mem = sc->sc_mem;
sc->sc_init = (void *)mem;
sc->sc_init->mode = LE_NORMAL;
for (i = 0; i < ETHER_ADDR_LEN; i++)
sc->sc_init->padr[i] = sc->sc_addr[i^1];
sc->sc_init->ladrf[0] = sc->sc_init->ladrf[1] = 0;
mem += sizeof(struct init_block);
if (myea)
memcpy(myea, sc->sc_addr, ETHER_ADDR_LEN);
/* Turn on byte swapping. */
lewrcsr(sc, 3, LE_BSWP);
/* Give LANCE the physical address of its init block. */
a = LANCE_ADDR(sc, sc->sc_init);
lewrcsr(sc, 1, a);
lewrcsr(sc, 2, (a >> 16) & 0xff);
#ifdef LE_DEBUG
if (le_debug)
printf("le%d: before init\n", unit);
#endif
/* Try to initialize the LANCE. */
lewrcsr(sc, 0, LE_INIT);
/* Wait for initialization to finish. */
for (timo = 100000; timo; timo--)
if (lerdcsr(sc, 0) & LE_IDON)
break;
if (lerdcsr(sc, 0) & LE_IDON) {
/* Start the LANCE. */
lewrcsr(sc, 0, LE_INEA | LE_STRT | LE_IDON);
} else
printf("le%d: card failed to initialize\n", unit);
#ifdef LE_DEBUG
if (le_debug)
printf("le%d: after init\n", unit);
#endif
le_mem_summary(unit);
}
static int
le_poll(struct iodesc *desc, void *pkt, int len)
{
int unit = /*nif->nif_unit*/0;
struct le_softc *sc = &le_softc[unit];
int length;
volatile struct mds *cdm;
int stat;
#ifdef LE_DEBUG
if (/*le_debug*/0)
printf("le%d: le_poll called. next_rd=%d\n", unit, sc->sc_next_rd);
#endif
stat = lerdcsr(sc, 0);
lewrcsr(sc, 0, stat & (LE_BABL | LE_MISS | LE_MERR | LE_RINT));
cdm = &sc->sc_rd[sc->sc_next_rd];
if (cdm->flags & LE_OWN)
return 0;
#ifdef LE_DEBUG
if (le_debug) {
printf("next_rd %d\n", sc->sc_next_rd);
printf("cdm->flags %x\n", cdm->flags);
printf("cdm->bcnt %x, cdm->mcnt %x\n", cdm->bcnt, cdm->mcnt);
printf("cdm->rbuf msg %d buf %d\n", cdm->mcnt, -cdm->bcnt );
}
#endif
if (stat & (LE_BABL | LE_CERR | LE_MISS | LE_MERR))
le_error(unit, "le_poll", stat);
if (cdm->flags & (LE_FRAM | LE_OFLO | LE_CRC | LE_RBUFF)) {
printf("le%d_poll: rmd status 0x%x\n", unit, cdm->flags);
length = 0;
goto cleanup;
}
if ((cdm->flags & (LE_STP|LE_ENP)) != (LE_STP|LE_ENP))
panic("le_poll: chained packet");
length = cdm->mcnt;
#ifdef LE_DEBUG
if (le_debug)
printf("le_poll: length %d\n", length);
#endif
if (length >= BUFSIZE) {
printf("le%d_poll: invalid length %d, status 0x%x\n",
unit, length, stat);
length = 0;
goto cleanup;
}
if (!length)
goto cleanup;
length -= 4;
if (length > 0) {
/*
* If the length of the packet is greater than the size of the
* buffer, we have to truncate it, to avoid Bad Things.
* XXX Is this the right thing to do?
*/
if (length > len)
length = len;