#include <sys/param.h>
#include <sys/systm.h>
#include <sys/sockio.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/socket.h>
#include <sys/serialize.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/interrupt.h>
#include <net/if.h>
#include <net/ifq_var.h>
#include <net/if_arp.h>
#include <net/ethernet.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/bpf.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include "../mii_layer/mii.h"
#include "../mii_layer/miivar.h"
#include <bus/pci/pcireg.h>
#include <bus/pci/pcivar.h>
#define TL_USEIOSPACE
#include "if_tlreg.h"
#include "miibus_if.h"
static struct tl_type tl_devs[] = {
{ TI_VENDORID, TI_DEVICEID_THUNDERLAN,
"Texas Instruments ThunderLAN" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10,
"Compaq Netelligent 10" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100,
"Compaq Netelligent 10/100" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100_PROLIANT,
"Compaq Netelligent 10/100 Proliant" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100_DUAL,
"Compaq Netelligent 10/100 Dual Port" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETFLEX_3P_INTEGRATED,
"Compaq NetFlex-3/P Integrated" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETFLEX_3P,
"Compaq NetFlex-3/P" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETFLEX_3P_BNC,
"Compaq NetFlex 3/P w/ BNC" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100_EMBEDDED,
"Compaq Netelligent 10/100 TX Embedded UTP" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_T2_UTP_COAX,
"Compaq Netelligent 10 T/2 PCI UTP/Coax" },
{ COMPAQ_VENDORID, COMPAQ_DEVICEID_NETEL_10_100_TX_UTP,
"Compaq Netelligent 10/100 TX UTP" },
{ OLICOM_VENDORID, OLICOM_DEVICEID_OC2183,
"Olicom OC-2183/2185" },
{ OLICOM_VENDORID, OLICOM_DEVICEID_OC2325,
"Olicom OC-2325" },
{ OLICOM_VENDORID, OLICOM_DEVICEID_OC2326,
"Olicom OC-2326 10/100 TX UTP" },
{ 0, 0, NULL }
};
static int tl_probe (device_t);
static int tl_attach (device_t);
static int tl_detach (device_t);
static int tl_intvec_rxeoc (void *, u_int32_t);
static int tl_intvec_txeoc (void *, u_int32_t);
static int tl_intvec_txeof (void *, u_int32_t);
static int tl_intvec_rxeof (void *, u_int32_t);
static int tl_intvec_adchk (void *, u_int32_t);
static int tl_intvec_netsts (void *, u_int32_t);
static int tl_newbuf (struct tl_softc *,
struct tl_chain_onefrag *);
static void tl_stats_update (void *);
static void tl_stats_update_serialized(void *);
static int tl_encap (struct tl_softc *, struct tl_chain *,
struct mbuf *);
static void tl_intr (void *);
static void tl_start (struct ifnet *, struct ifaltq_subque *);
static int tl_ioctl (struct ifnet *, u_long, caddr_t,
struct ucred *);
static void tl_init (void *);
static void tl_stop (struct tl_softc *);
static void tl_watchdog (struct ifnet *);
static void tl_shutdown (device_t);
static int tl_ifmedia_upd (struct ifnet *);
static void tl_ifmedia_sts (struct ifnet *, struct ifmediareq *);
static u_int8_t tl_eeprom_putbyte (struct tl_softc *, int);
static u_int8_t tl_eeprom_getbyte (struct tl_softc *,
int, u_int8_t *);
static int tl_read_eeprom (struct tl_softc *, caddr_t, int, int);
static void tl_mii_sync (struct tl_softc *);
static void tl_mii_send (struct tl_softc *, u_int32_t, int);
static int tl_mii_readreg (struct tl_softc *, struct tl_mii_frame *);
static int tl_mii_writereg (struct tl_softc *, struct tl_mii_frame *);
static int tl_miibus_readreg (device_t, int, int);
static int tl_miibus_writereg (device_t, int, int, int);
static void tl_miibus_statchg (device_t);
static void tl_setmode (struct tl_softc *, int);
static int tl_calchash (caddr_t);
static void tl_setmulti (struct tl_softc *);
static void tl_setfilt (struct tl_softc *, caddr_t, int);
static void tl_softreset (struct tl_softc *, int);
static void tl_hardreset (device_t);
static int tl_list_rx_init (struct tl_softc *);
static int tl_list_tx_init (struct tl_softc *);
static u_int8_t tl_dio_read8 (struct tl_softc *, int);
static u_int16_t tl_dio_read16 (struct tl_softc *, int);
static u_int32_t tl_dio_read32 (struct tl_softc *, int);
static void tl_dio_write8 (struct tl_softc *, int, int);
static void tl_dio_write16 (struct tl_softc *, int, int);
static void tl_dio_write32 (struct tl_softc *, int, int);
static void tl_dio_setbit (struct tl_softc *, int, int);
static void tl_dio_clrbit (struct tl_softc *, int, int);
static void tl_dio_setbit16 (struct tl_softc *, int, int);
static void tl_dio_clrbit16 (struct tl_softc *, int, int);
#ifdef TL_USEIOSPACE
#define TL_RES SYS_RES_IOPORT
#define TL_RID TL_PCI_LOIO
#else
#define TL_RES SYS_RES_MEMORY
#define TL_RID TL_PCI_LOMEM
#endif
static device_method_t tl_methods[] = {
DEVMETHOD(device_probe, tl_probe),
DEVMETHOD(device_attach, tl_attach),
DEVMETHOD(device_detach, tl_detach),
DEVMETHOD(device_shutdown, tl_shutdown),
DEVMETHOD(bus_print_child, bus_generic_print_child),
DEVMETHOD(bus_driver_added, bus_generic_driver_added),
DEVMETHOD(miibus_readreg, tl_miibus_readreg),
DEVMETHOD(miibus_writereg, tl_miibus_writereg),
DEVMETHOD(miibus_statchg, tl_miibus_statchg),
DEVMETHOD_END
};
static driver_t tl_driver = {
"tl",
tl_methods,
sizeof(struct tl_softc)
};
static devclass_t tl_devclass;
DECLARE_DUMMY_MODULE(if_tl);
DRIVER_MODULE(if_tl, pci, tl_driver, tl_devclass, NULL, NULL);
DRIVER_MODULE(miibus, tl, miibus_driver, miibus_devclass, NULL, NULL);
static u_int8_t
tl_dio_read8(struct tl_softc *sc, int reg)
{
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
return(CSR_READ_1(sc, TL_DIO_DATA + (reg & 3)));
}
static u_int16_t
tl_dio_read16(struct tl_softc *sc, int reg)
{
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
return(CSR_READ_2(sc, TL_DIO_DATA + (reg & 3)));
}
static u_int32_t
tl_dio_read32(struct tl_softc *sc, int reg)
{
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
return(CSR_READ_4(sc, TL_DIO_DATA + (reg & 3)));
}
static void
tl_dio_write8(struct tl_softc *sc, int reg, int val)
{
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
CSR_WRITE_1(sc, TL_DIO_DATA + (reg & 3), val);
return;
}
static void
tl_dio_write16(struct tl_softc *sc, int reg, int val)
{
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
CSR_WRITE_2(sc, TL_DIO_DATA + (reg & 3), val);
return;
}
static void
tl_dio_write32(struct tl_softc *sc, int reg, int val)
{
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
CSR_WRITE_4(sc, TL_DIO_DATA + (reg & 3), val);
return;
}
static void
tl_dio_setbit(struct tl_softc *sc, int reg, int bit)
{
u_int8_t f;
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
f = CSR_READ_1(sc, TL_DIO_DATA + (reg & 3));
f |= bit;
CSR_WRITE_1(sc, TL_DIO_DATA + (reg & 3), f);
return;
}
static void
tl_dio_clrbit(struct tl_softc *sc, int reg, int bit)
{
u_int8_t f;
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
f = CSR_READ_1(sc, TL_DIO_DATA + (reg & 3));
f &= ~bit;
CSR_WRITE_1(sc, TL_DIO_DATA + (reg & 3), f);
return;
}
static void
tl_dio_setbit16(struct tl_softc *sc, int reg, int bit)
{
u_int16_t f;
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
f = CSR_READ_2(sc, TL_DIO_DATA + (reg & 3));
f |= bit;
CSR_WRITE_2(sc, TL_DIO_DATA + (reg & 3), f);
return;
}
static void
tl_dio_clrbit16(struct tl_softc *sc, int reg, int bit)
{
u_int16_t f;
CSR_WRITE_2(sc, TL_DIO_ADDR, reg);
f = CSR_READ_2(sc, TL_DIO_DATA + (reg & 3));
f &= ~bit;
CSR_WRITE_2(sc, TL_DIO_DATA + (reg & 3), f);
return;
}
static u_int8_t
tl_eeprom_putbyte(struct tl_softc *sc, int byte)
{
int i, ack = 0;
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_ETXEN);
for (i = 0x80; i; i >>= 1) {
if (byte & i) {
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_EDATA);
} else {
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_EDATA);
}
DELAY(1);
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_ECLOK);
DELAY(1);
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ECLOK);
}
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ETXEN);
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_ECLOK);
ack = tl_dio_read8(sc, TL_NETSIO) & TL_SIO_EDATA;
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ECLOK);
return(ack);
}
static u_int8_t
tl_eeprom_getbyte(struct tl_softc *sc, int addr, u_int8_t *dest)
{
int i;
u_int8_t byte = 0;
tl_dio_write8(sc, TL_NETSIO, 0);
EEPROM_START;
if (tl_eeprom_putbyte(sc, EEPROM_CTL_WRITE)) {
if_printf(&sc->arpcom.ac_if, "failed to send write command, "
"status: %x\n", tl_dio_read8(sc, TL_NETSIO));
return(1);
}
if (tl_eeprom_putbyte(sc, addr)) {
if_printf(&sc->arpcom.ac_if, "failed to send address, "
"status: %x\n", tl_dio_read8(sc, TL_NETSIO));
return(1);
}
EEPROM_STOP;
EEPROM_START;
if (tl_eeprom_putbyte(sc, EEPROM_CTL_READ)) {
if_printf(&sc->arpcom.ac_if, "failed to send write command, "
"status: %x\n", tl_dio_read8(sc, TL_NETSIO));
return(1);
}
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ETXEN);
for (i = 0x80; i; i >>= 1) {
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_ECLOK);
DELAY(1);
if (tl_dio_read8(sc, TL_NETSIO) & TL_SIO_EDATA)
byte |= i;
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_ECLOK);
DELAY(1);
}
EEPROM_STOP;
*dest = byte;
return(0);
}
static int
tl_read_eeprom(struct tl_softc *sc, caddr_t dest, int off, int cnt)
{
int err = 0, i;
u_int8_t byte = 0;
for (i = 0; i < cnt; i++) {
err = tl_eeprom_getbyte(sc, off + i, &byte);
if (err)
break;
*(dest + i) = byte;
}
return(err ? 1 : 0);
}
static void
tl_mii_sync(struct tl_softc *sc)
{
int i;
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MTXEN);
for (i = 0; i < 32; i++) {
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
}
return;
}
static void
tl_mii_send(struct tl_softc *sc, u_int32_t bits, int cnt)
{
int i;
for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
if (bits & i) {
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MDATA);
} else {
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MDATA);
}
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
}
}
static int
tl_mii_readreg(struct tl_softc *sc, struct tl_mii_frame *frame)
{
int i, ack;
int minten = 0;
tl_mii_sync(sc);
frame->mii_stdelim = TL_MII_STARTDELIM;
frame->mii_opcode = TL_MII_READOP;
frame->mii_turnaround = 0;
frame->mii_data = 0;
minten = tl_dio_read8(sc, TL_NETSIO) & TL_SIO_MINTEN;
if (minten) {
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MINTEN);
}
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MTXEN);
tl_mii_send(sc, frame->mii_stdelim, 2);
tl_mii_send(sc, frame->mii_opcode, 2);
tl_mii_send(sc, frame->mii_phyaddr, 5);
tl_mii_send(sc, frame->mii_regaddr, 5);
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MTXEN);
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
ack = tl_dio_read8(sc, TL_NETSIO) & TL_SIO_MDATA;
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
if (ack) {
for(i = 0; i < 16; i++) {
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
}
goto fail;
}
for (i = 0x8000; i; i >>= 1) {
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
if (!ack) {
if (tl_dio_read8(sc, TL_NETSIO) & TL_SIO_MDATA)
frame->mii_data |= i;
}
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
}
fail:
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
if (minten) {
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MINTEN);
}
if (ack)
return(1);
return(0);
}
static int
tl_mii_writereg(struct tl_softc *sc, struct tl_mii_frame *frame)
{
int minten;
tl_mii_sync(sc);
frame->mii_stdelim = TL_MII_STARTDELIM;
frame->mii_opcode = TL_MII_WRITEOP;
frame->mii_turnaround = TL_MII_TURNAROUND;
minten = tl_dio_read8(sc, TL_NETSIO) & TL_SIO_MINTEN;
if (minten) {
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MINTEN);
}
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MTXEN);
tl_mii_send(sc, frame->mii_stdelim, 2);
tl_mii_send(sc, frame->mii_opcode, 2);
tl_mii_send(sc, frame->mii_phyaddr, 5);
tl_mii_send(sc, frame->mii_regaddr, 5);
tl_mii_send(sc, frame->mii_turnaround, 2);
tl_mii_send(sc, frame->mii_data, 16);
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MCLK);
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MCLK);
tl_dio_clrbit(sc, TL_NETSIO, TL_SIO_MTXEN);
if (minten)
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_MINTEN);
return(0);
}
static int
tl_miibus_readreg(device_t dev, int phy, int reg)
{
struct tl_softc *sc;
struct tl_mii_frame frame;
sc = device_get_softc(dev);
bzero((char *)&frame, sizeof(frame));
frame.mii_phyaddr = phy;
frame.mii_regaddr = reg;
tl_mii_readreg(sc, &frame);
return(frame.mii_data);
}
static int
tl_miibus_writereg(device_t dev, int phy, int reg, int data)
{
struct tl_softc *sc;
struct tl_mii_frame frame;
sc = device_get_softc(dev);
bzero((char *)&frame, sizeof(frame));
frame.mii_phyaddr = phy;
frame.mii_regaddr = reg;
frame.mii_data = data;
tl_mii_writereg(sc, &frame);
return(0);
}
static void
tl_miibus_statchg(device_t dev)
{
struct tl_softc *sc;
struct mii_data *mii;
sc = device_get_softc(dev);
mii = device_get_softc(sc->tl_miibus);
if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
tl_dio_setbit(sc, TL_NETCMD, TL_CMD_DUPLEX);
} else {
tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_DUPLEX);
}
return;
}
static void
tl_setmode(struct tl_softc *sc, int media)
{
if (IFM_SUBTYPE(media) == IFM_10_5)
tl_dio_setbit(sc, TL_ACOMMIT, TL_AC_MTXD1);
if (IFM_SUBTYPE(media) == IFM_10_T) {
tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_MTXD1);
if ((media & IFM_GMASK) == IFM_FDX) {
tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_MTXD3);
tl_dio_setbit(sc, TL_NETCMD, TL_CMD_DUPLEX);
} else {
tl_dio_setbit(sc, TL_ACOMMIT, TL_AC_MTXD3);
tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_DUPLEX);
}
}
return;
}
static int
tl_calchash(caddr_t addr)
{
int t;
t = (addr[0] ^ addr[3]) << 16 | (addr[1] ^ addr[4]) << 8 |
(addr[2] ^ addr[5]);
return ((t >> 18) ^ (t >> 12) ^ (t >> 6) ^ t) & 0x3f;
}
static void
tl_setfilt(struct tl_softc *sc, caddr_t addr, int slot)
{
int i;
u_int16_t regaddr;
regaddr = TL_AREG0_B5 + (slot * ETHER_ADDR_LEN);
for (i = 0; i < ETHER_ADDR_LEN; i++)
tl_dio_write8(sc, regaddr + i, *(addr + i));
return;
}
static void
tl_setmulti(struct tl_softc *sc)
{
struct ifnet *ifp;
u_int32_t hashes[2] = { 0, 0 };
int h, i;
struct ifmultiaddr *ifma;
u_int8_t dummy[] = { 0, 0, 0, 0, 0 ,0 };
ifp = &sc->arpcom.ac_if;
for (i = 1; i < 4; i++)
tl_setfilt(sc, (caddr_t)&dummy, i);
tl_dio_write32(sc, TL_HASH1, 0);
tl_dio_write32(sc, TL_HASH2, 0);
if (ifp->if_flags & IFF_ALLMULTI) {
hashes[0] = 0xFFFFFFFF;
hashes[1] = 0xFFFFFFFF;
} else {
i = 1;
TAILQ_FOREACH_REVERSE(ifma, &ifp->if_multiaddrs, ifmultihead, ifma_link) {
if (ifma->ifma_addr->sa_family != AF_LINK)
continue;
if (i < 4) {
tl_setfilt(sc,
LLADDR((struct sockaddr_dl *)ifma->ifma_addr), i);
i++;
continue;
}
h = tl_calchash(
LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
if (h < 32)
hashes[0] |= (1 << h);
else
hashes[1] |= (1 << (h - 32));
}
}
tl_dio_write32(sc, TL_HASH1, hashes[0]);
tl_dio_write32(sc, TL_HASH2, hashes[1]);
return;
}
static void
tl_hardreset(device_t dev)
{
struct tl_softc *sc;
int i;
u_int16_t flags;
sc = device_get_softc(dev);
tl_mii_sync(sc);
flags = BMCR_LOOP|BMCR_ISO|BMCR_PDOWN;
for (i = 0; i < MII_NPHY; i++)
tl_miibus_writereg(dev, i, MII_BMCR, flags);
tl_miibus_writereg(dev, 31, MII_BMCR, BMCR_ISO);
DELAY(50000);
tl_miibus_writereg(dev, 31, MII_BMCR, BMCR_LOOP|BMCR_ISO);
tl_mii_sync(sc);
while(tl_miibus_readreg(dev, 31, MII_BMCR) & BMCR_RESET);
DELAY(50000);
return;
}
static void
tl_softreset(struct tl_softc *sc, int internal)
{
u_int32_t cmd, i;
CMD_SET(sc, TL_CMD_ADRST);
CMD_SET(sc, TL_CMD_INTSOFF);
for (i = 0; i < 5; i++)
tl_dio_read32(sc, TL_TXGOODFRAMES);
for (i = 0; i < 8; i++)
tl_dio_write32(sc, TL_AREG0_B5, 0x00000000);
tl_dio_setbit16(sc, TL_NETCONFIG, TL_CFG_ONECHAN|TL_CFG_ONEFRAG);
if (internal && !sc->tl_bitrate) {
tl_dio_setbit16(sc, TL_NETCONFIG, TL_CFG_PHYEN);
} else {
tl_dio_clrbit16(sc, TL_NETCONFIG, TL_CFG_PHYEN);
}
if (sc->tl_bitrate)
tl_dio_setbit16(sc, TL_NETCONFIG, TL_CFG_BITRATE);
cmd = CSR_READ_4(sc, TL_HOSTCMD);
cmd |= TL_CMD_NES;
cmd &= ~(TL_CMD_RT|TL_CMD_EOC|TL_CMD_ACK_MASK|TL_CMD_CHSEL_MASK);
CMD_PUT(sc, cmd | (TL_CMD_LDTHR | TX_THR));
CMD_PUT(sc, cmd | (TL_CMD_LDTMR | 0x00000003));
tl_dio_setbit(sc, TL_NETSIO, TL_SIO_NMRST);
tl_dio_setbit(sc, TL_NETCMD, TL_CMD_NRESET|TL_CMD_NWRAP);
DELAY(500);
return;
}
static int
tl_probe(device_t dev)
{
struct tl_type *t;
t = tl_devs;
while(t->tl_name != NULL) {
if ((pci_get_vendor(dev) == t->tl_vid) &&
(pci_get_device(dev) == t->tl_did)) {
device_set_desc(dev, t->tl_name);
return(0);
}
t++;
}
return(ENXIO);
}
static int
tl_attach(device_t dev)
{
int i;
u_int16_t did, vid;
struct tl_type *t;
struct ifnet *ifp;
struct tl_softc *sc;
int error = 0, rid;
uint8_t eaddr[ETHER_ADDR_LEN];
vid = pci_get_vendor(dev);
did = pci_get_device(dev);
sc = device_get_softc(dev);
t = tl_devs;
while(t->tl_name != NULL) {
if (vid == t->tl_vid && did == t->tl_did)
break;
t++;
}
KKASSERT(t->tl_name != NULL);
pci_enable_busmaster(dev);
#ifdef TL_USEIOSPACE
rid = TL_PCI_LOIO;
sc->tl_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
RF_ACTIVE);
if (sc->tl_res == NULL) {
rid = TL_PCI_LOMEM;
sc->tl_res = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
RF_ACTIVE);
}
#else
rid = TL_PCI_LOMEM;
sc->tl_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
RF_ACTIVE);
if (sc->tl_res == NULL) {
rid = TL_PCI_LOIO;
sc->tl_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
RF_ACTIVE);
}
#endif
if (sc->tl_res == NULL) {
device_printf(dev, "couldn't map ports/memory\n");
error = ENXIO;
return(error);
}
sc->tl_btag = rman_get_bustag(sc->tl_res);
sc->tl_bhandle = rman_get_bushandle(sc->tl_res);
#ifdef notdef
command = pci_read_config(dev, TL_PCI_LATENCY_TIMER, 4);
command |= 0x0000FF00;
pci_write_config(dev, TL_PCI_LATENCY_TIMER, command, 4);
#endif
rid = 0;
sc->tl_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
RF_SHAREABLE | RF_ACTIVE);
if (sc->tl_irq == NULL) {
device_printf(dev, "couldn't map interrupt\n");
error = ENXIO;
goto fail;
}
sc->tl_ldata = contigmalloc(sizeof(struct tl_list_data), M_DEVBUF,
M_WAITOK | M_ZERO, 0, 0xffffffff, PAGE_SIZE, 0);
if (sc->tl_ldata == NULL) {
device_printf(dev, "no memory for list buffers!\n");
error = ENXIO;
goto fail;
}
sc->tl_dinfo = t;
if (t->tl_vid == COMPAQ_VENDORID || t->tl_vid == TI_VENDORID)
sc->tl_eeaddr = TL_EEPROM_EADDR;
if (t->tl_vid == OLICOM_VENDORID)
sc->tl_eeaddr = TL_EEPROM_EADDR_OC;
tl_softreset(sc, 1);
tl_hardreset(dev);
tl_softreset(sc, 1);
ifp = &sc->arpcom.ac_if;
if_initname(ifp, device_get_name(dev), device_get_unit(dev));
if (tl_read_eeprom(sc, eaddr, sc->tl_eeaddr, ETHER_ADDR_LEN)) {
device_printf(dev, "failed to read station address\n");
error = ENXIO;
goto fail;
}
if (sc->tl_dinfo->tl_vid == OLICOM_VENDORID) {
for (i = 0; i < ETHER_ADDR_LEN; i += 2) {
u_int16_t *p;
p = (u_int16_t *)&eaddr[i];
*p = ntohs(*p);
}
}
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = tl_ioctl;
ifp->if_start = tl_start;
ifp->if_watchdog = tl_watchdog;
ifp->if_init = tl_init;
ifp->if_mtu = ETHERMTU;
ifq_set_maxlen(&ifp->if_snd, TL_TX_LIST_CNT - 1);
ifq_set_ready(&ifp->if_snd);
callout_init(&sc->tl_stat_timer);
tl_softreset(sc, 1);
tl_hardreset(dev);
tl_softreset(sc, 1);
if (mii_phy_probe(dev, &sc->tl_miibus,
tl_ifmedia_upd, tl_ifmedia_sts)) {
struct ifmedia *ifm;
sc->tl_bitrate = 1;
ifmedia_init(&sc->ifmedia, 0, tl_ifmedia_upd, tl_ifmedia_sts);
ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T, 0, NULL);
ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_HDX, 0, NULL);
ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
ifmedia_add(&sc->ifmedia, IFM_ETHER|IFM_10_5, 0, NULL);
ifmedia_set(&sc->ifmedia, IFM_ETHER|IFM_10_T);
tl_softreset(sc, 1);
ifm = &sc->ifmedia;
ifm->ifm_media = ifm->ifm_cur->ifm_media;
tl_ifmedia_upd(ifp);
}
ether_ifattach(ifp, eaddr, NULL);
ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->tl_irq));
error = bus_setup_intr(dev, sc->tl_irq, INTR_MPSAFE,
tl_intr, sc, &sc->tl_intrhand,
ifp->if_serializer);
if (error) {
ether_ifdetach(ifp);
device_printf(dev, "couldn't set up irq\n");
goto fail;
}
return(0);
fail:
tl_detach(dev);
return(error);
}
static int
tl_detach(device_t dev)
{
struct tl_softc *sc = device_get_softc(dev);
struct ifnet *ifp = &sc->arpcom.ac_if;
if (device_is_attached(dev)) {
lwkt_serialize_enter(ifp->if_serializer);
tl_stop(sc);
bus_teardown_intr(dev, sc->tl_irq, sc->tl_intrhand);
lwkt_serialize_exit(ifp->if_serializer);
ether_ifdetach(ifp);
}
if (sc->tl_miibus)
device_delete_child(dev, sc->tl_miibus);
bus_generic_detach(dev);
if (sc->tl_ldata)
contigfree(sc->tl_ldata, sizeof(struct tl_list_data), M_DEVBUF);
if (sc->tl_bitrate)
ifmedia_removeall(&sc->ifmedia);
if (sc->tl_irq)
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->tl_irq);
if (sc->tl_res)
bus_release_resource(dev, TL_RES, TL_RID, sc->tl_res);
return(0);
}
static int
tl_list_tx_init(struct tl_softc *sc)
{
struct tl_chain_data *cd;
struct tl_list_data *ld;
int i;
cd = &sc->tl_cdata;
ld = sc->tl_ldata;
for (i = 0; i < TL_TX_LIST_CNT; i++) {
cd->tl_tx_chain[i].tl_ptr = &ld->tl_tx_list[i];
if (i == (TL_TX_LIST_CNT - 1))
cd->tl_tx_chain[i].tl_next = NULL;
else
cd->tl_tx_chain[i].tl_next = &cd->tl_tx_chain[i + 1];
}
cd->tl_tx_free = &cd->tl_tx_chain[0];
cd->tl_tx_tail = cd->tl_tx_head = NULL;
sc->tl_txeoc = 1;
return(0);
}
static int
tl_list_rx_init(struct tl_softc *sc)
{
struct tl_chain_data *cd;
struct tl_list_data *ld;
int i;
cd = &sc->tl_cdata;
ld = sc->tl_ldata;
for (i = 0; i < TL_RX_LIST_CNT; i++) {
cd->tl_rx_chain[i].tl_ptr =
(struct tl_list_onefrag *)&ld->tl_rx_list[i];
if (tl_newbuf(sc, &cd->tl_rx_chain[i]) == ENOBUFS)
return(ENOBUFS);
if (i == (TL_RX_LIST_CNT - 1)) {
cd->tl_rx_chain[i].tl_next = NULL;
ld->tl_rx_list[i].tlist_fptr = 0;
} else {
cd->tl_rx_chain[i].tl_next = &cd->tl_rx_chain[i + 1];
ld->tl_rx_list[i].tlist_fptr =
vtophys(&ld->tl_rx_list[i + 1]);
}
}
cd->tl_rx_head = &cd->tl_rx_chain[0];
cd->tl_rx_tail = &cd->tl_rx_chain[TL_RX_LIST_CNT - 1];
return(0);
}
static int
tl_newbuf(struct tl_softc *sc, struct tl_chain_onefrag *c)
{
struct mbuf *m_new;
m_new = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
if (m_new == NULL)
return (ENOBUFS);
c->tl_mbuf = m_new;
c->tl_next = NULL;
c->tl_ptr->tlist_frsize = MCLBYTES;
c->tl_ptr->tlist_fptr = 0;
c->tl_ptr->tl_frag.tlist_dadr = vtophys(mtod(m_new, caddr_t));
c->tl_ptr->tl_frag.tlist_dcnt = MCLBYTES;
c->tl_ptr->tlist_cstat = TL_CSTAT_READY;
return(0);
}
static int
tl_intvec_rxeof(void *xsc, u_int32_t type)
{
struct tl_softc *sc;
int r = 0, total_len = 0;
struct ether_header *eh;
struct mbuf *m;
struct ifnet *ifp;
struct tl_chain_onefrag *cur_rx;
sc = xsc;
ifp = &sc->arpcom.ac_if;
while(sc->tl_cdata.tl_rx_head != NULL) {
cur_rx = sc->tl_cdata.tl_rx_head;
if (!(cur_rx->tl_ptr->tlist_cstat & TL_CSTAT_FRAMECMP))
break;
r++;
sc->tl_cdata.tl_rx_head = cur_rx->tl_next;
m = cur_rx->tl_mbuf;
total_len = cur_rx->tl_ptr->tlist_frsize;
if (tl_newbuf(sc, cur_rx) == ENOBUFS) {
IFNET_STAT_INC(ifp, ierrors, 1);
cur_rx->tl_ptr->tlist_frsize = MCLBYTES;
cur_rx->tl_ptr->tlist_cstat = TL_CSTAT_READY;
cur_rx->tl_ptr->tl_frag.tlist_dcnt = MCLBYTES;
continue;
}
sc->tl_cdata.tl_rx_tail->tl_ptr->tlist_fptr =
vtophys(cur_rx->tl_ptr);
sc->tl_cdata.tl_rx_tail->tl_next = cur_rx;
sc->tl_cdata.tl_rx_tail = cur_rx;
eh = mtod(m, struct ether_header *);
m->m_pkthdr.rcvif = ifp;
m->m_pkthdr.len = m->m_len = total_len;
if (!bcmp(eh->ether_shost, sc->arpcom.ac_enaddr,
ETHER_ADDR_LEN)) {
m_freem(m);
continue;
}
ifp->if_input(ifp, m, NULL, -1);
}
return(r);
}
static int
tl_intvec_rxeoc(void *xsc, u_int32_t type)
{
struct tl_softc *sc;
int r;
struct tl_chain_data *cd;
sc = xsc;
cd = &sc->tl_cdata;
r = tl_intvec_rxeof(xsc, type);
CMD_PUT(sc, TL_CMD_ACK | r | (type & ~(0x00100000)));
r = 1;
cd->tl_rx_head = &cd->tl_rx_chain[0];
cd->tl_rx_tail = &cd->tl_rx_chain[TL_RX_LIST_CNT - 1];
CSR_WRITE_4(sc, TL_CH_PARM, vtophys(sc->tl_cdata.tl_rx_head->tl_ptr));
r |= (TL_CMD_GO|TL_CMD_RT);
return(r);
}
static int
tl_intvec_txeof(void *xsc, u_int32_t type)
{
struct tl_softc *sc;
int r = 0;
struct tl_chain *cur_tx;
sc = xsc;
while (sc->tl_cdata.tl_tx_head != NULL) {
cur_tx = sc->tl_cdata.tl_tx_head;
if (!(cur_tx->tl_ptr->tlist_cstat & TL_CSTAT_FRAMECMP))
break;
sc->tl_cdata.tl_tx_head = cur_tx->tl_next;
r++;
m_freem(cur_tx->tl_mbuf);
cur_tx->tl_mbuf = NULL;
cur_tx->tl_next = sc->tl_cdata.tl_tx_free;
sc->tl_cdata.tl_tx_free = cur_tx;
if (!cur_tx->tl_ptr->tlist_fptr)
break;
}
return(r);
}
static int
tl_intvec_txeoc(void *xsc, u_int32_t type)
{
struct tl_softc *sc;
struct ifnet *ifp;
u_int32_t cmd;
sc = xsc;
ifp = &sc->arpcom.ac_if;
ifp->if_timer = 0;
if (sc->tl_cdata.tl_tx_head == NULL) {
ifq_clr_oactive(&ifp->if_snd);
sc->tl_cdata.tl_tx_tail = NULL;
sc->tl_txeoc = 1;
} else {
sc->tl_txeoc = 0;
CMD_PUT(sc, TL_CMD_ACK | 0x00000001 | type);
CSR_WRITE_4(sc, TL_CH_PARM,
vtophys(sc->tl_cdata.tl_tx_head->tl_ptr));
cmd = CSR_READ_4(sc, TL_HOSTCMD);
cmd &= ~TL_CMD_RT;
cmd |= TL_CMD_GO|TL_CMD_INTSON;
CMD_PUT(sc, cmd);
return(0);
}
return(1);
}
static int
tl_intvec_adchk(void *xsc, u_int32_t type)
{
struct tl_softc *sc;
sc = xsc;
if (type) {
if_printf(&sc->arpcom.ac_if, "adapter check: %x\n",
(unsigned int)CSR_READ_4(sc, TL_CH_PARM));
}
tl_softreset(sc, 1);
tl_stop(sc);
tl_init(sc);
CMD_SET(sc, TL_CMD_INTSON);
return(0);
}
static int
tl_intvec_netsts(void *xsc, u_int32_t type)
{
struct tl_softc *sc;
u_int16_t netsts;
sc = xsc;
netsts = tl_dio_read16(sc, TL_NETSTS);
tl_dio_write16(sc, TL_NETSTS, netsts);
if_printf(&sc->arpcom.ac_if, "network status: %x\n", netsts);
return(1);
}
static void
tl_intr(void *xsc)
{
struct tl_softc *sc;
struct ifnet *ifp;
int r = 0;
u_int32_t type = 0;
u_int16_t ints = 0;
u_int8_t ivec = 0;
sc = xsc;
ints = CSR_READ_2(sc, TL_HOST_INT);
CSR_WRITE_2(sc, TL_HOST_INT, ints);
type = (ints << 16) & 0xFFFF0000;
ivec = (ints & TL_VEC_MASK) >> 5;
ints = (ints & TL_INT_MASK) >> 2;
ifp = &sc->arpcom.ac_if;
switch(ints) {
case (TL_INTR_INVALID):
#ifdef DIAGNOSTIC
if_printf(ifp, "got an invalid interrupt!\n");
#endif
CMD_PUT(sc, type);
r = 0;
break;
case (TL_INTR_TXEOF):
r = tl_intvec_txeof(sc, type);
break;
case (TL_INTR_TXEOC):
r = tl_intvec_txeoc(sc, type);
break;
case (TL_INTR_STATOFLOW):
tl_stats_update_serialized(sc);
r = 1;
break;
case (TL_INTR_RXEOF):
r = tl_intvec_rxeof(sc, type);
break;
case (TL_INTR_DUMMY):
if_printf(ifp, "got a dummy interrupt\n");
r = 1;
break;
case (TL_INTR_ADCHK):
if (ivec)
r = tl_intvec_adchk(sc, type);
else
r = tl_intvec_netsts(sc, type);
break;
case (TL_INTR_RXEOC):
r = tl_intvec_rxeoc(sc, type);
break;
default:
if_printf(ifp, "bogus interrupt type\n");
break;
}
if (r) {
CMD_PUT(sc, TL_CMD_ACK | r | type);
}
if (!ifq_is_empty(&ifp->if_snd))
if_devstart(ifp);
}
static
void
tl_stats_update(void *xsc)
{
struct tl_softc *sc = xsc;
struct ifnet *ifp = &sc->arpcom.ac_if;
lwkt_serialize_enter(ifp->if_serializer);
tl_stats_update_serialized(xsc);
lwkt_serialize_exit(ifp->if_serializer);
}
static
void
tl_stats_update_serialized(void *xsc)
{
struct tl_softc *sc;
struct ifnet *ifp;
struct tl_stats tl_stats;
struct mii_data *mii;
u_int32_t *p;
bzero((char *)&tl_stats, sizeof(struct tl_stats));
sc = xsc;
ifp = &sc->arpcom.ac_if;
p = (u_int32_t *)&tl_stats;
CSR_WRITE_2(sc, TL_DIO_ADDR, TL_TXGOODFRAMES|TL_DIO_ADDR_INC);
*p++ = CSR_READ_4(sc, TL_DIO_DATA);
*p++ = CSR_READ_4(sc, TL_DIO_DATA);
*p++ = CSR_READ_4(sc, TL_DIO_DATA);
*p++ = CSR_READ_4(sc, TL_DIO_DATA);
*p++ = CSR_READ_4(sc, TL_DIO_DATA);
IFNET_STAT_INC(ifp, opackets, tl_tx_goodframes(tl_stats));
IFNET_STAT_INC(ifp, collisions, tl_stats.tl_tx_single_collision +
tl_stats.tl_tx_multi_collision);
IFNET_STAT_INC(ifp, ipackets, tl_rx_goodframes(tl_stats));
IFNET_STAT_INC(ifp, ierrors, tl_stats.tl_crc_errors +
tl_stats.tl_code_errors + tl_rx_overrun(tl_stats));
IFNET_STAT_INC(ifp, oerrors, tl_tx_underrun(tl_stats));
if (tl_tx_underrun(tl_stats)) {
u_int8_t tx_thresh;
tx_thresh = tl_dio_read8(sc, TL_ACOMMIT) & TL_AC_TXTHRESH;
if (tx_thresh != TL_AC_TXTHRESH_WHOLEPKT) {
tx_thresh >>= 4;
tx_thresh++;
if_printf(ifp, "tx underrun -- increasing "
"tx threshold to %d bytes\n",
(64 * (tx_thresh * 4)));
tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_TXTHRESH);
tl_dio_setbit(sc, TL_ACOMMIT, tx_thresh << 4);
}
}
callout_reset(&sc->tl_stat_timer, hz, tl_stats_update, sc);
if (!sc->tl_bitrate) {
mii = device_get_softc(sc->tl_miibus);
mii_tick(mii);
}
}
static int
tl_encap(struct tl_softc *sc, struct tl_chain *c, struct mbuf *m_head)
{
int frag = 0;
struct tl_frag *f = NULL;
int total_len;
struct mbuf *m;
total_len = 0;
for (m = m_head, frag = 0; m != NULL; m = m->m_next) {
if (m->m_len != 0) {
if (frag == TL_MAXFRAGS)
break;
total_len+= m->m_len;
c->tl_ptr->tl_frag[frag].tlist_dadr =
vtophys(mtod(m, vm_offset_t));
c->tl_ptr->tl_frag[frag].tlist_dcnt = m->m_len;
frag++;
}
}
if (m != NULL) {
struct mbuf *m_new;
m_new = m_getl(m_head->m_pkthdr.len, M_NOWAIT, MT_DATA,
M_PKTHDR, NULL);
if (m_new == NULL) {
if_printf(&sc->arpcom.ac_if, "no memory for tx list\n");
return (1);
}
m_copydata(m_head, 0, m_head->m_pkthdr.len,
mtod(m_new, void *));
m_new->m_pkthdr.len = m_new->m_len = m_head->m_pkthdr.len;
m_freem(m_head);
m_head = m_new;
f = &c->tl_ptr->tl_frag[0];
f->tlist_dadr = vtophys(mtod(m_new, caddr_t));
f->tlist_dcnt = total_len = m_new->m_len;
frag = 1;
}
if (total_len < TL_MIN_FRAMELEN) {
if (frag == TL_MAXFRAGS) {
if_printf(&sc->arpcom.ac_if, "all frags filled but "
"frame still to small!\n");
}
f = &c->tl_ptr->tl_frag[frag];
f->tlist_dcnt = TL_MIN_FRAMELEN - total_len;
f->tlist_dadr = vtophys(&sc->tl_ldata->tl_pad);
total_len += f->tlist_dcnt;
frag++;
}
c->tl_mbuf = m_head;
c->tl_ptr->tl_frag[frag - 1].tlist_dcnt |= TL_LAST_FRAG;
c->tl_ptr->tlist_frsize = total_len;
c->tl_ptr->tlist_cstat = TL_CSTAT_READY;
c->tl_ptr->tlist_fptr = 0;
return(0);
}
static void
tl_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
{
struct tl_softc *sc;
struct mbuf *m_head = NULL;
u_int32_t cmd;
struct tl_chain *prev = NULL, *cur_tx = NULL, *start_tx;
ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
sc = ifp->if_softc;
if (sc->tl_cdata.tl_tx_free == NULL) {
ifq_set_oactive(&ifp->if_snd);
return;
}
start_tx = sc->tl_cdata.tl_tx_free;
while(sc->tl_cdata.tl_tx_free != NULL) {
m_head = ifq_dequeue(&ifp->if_snd);
if (m_head == NULL)
break;
cur_tx = sc->tl_cdata.tl_tx_free;
sc->tl_cdata.tl_tx_free = cur_tx->tl_next;
cur_tx->tl_next = NULL;
tl_encap(sc, cur_tx, m_head);
if (prev != NULL) {
prev->tl_next = cur_tx;
prev->tl_ptr->tlist_fptr = vtophys(cur_tx->tl_ptr);
}
prev = cur_tx;
BPF_MTAP(ifp, cur_tx->tl_mbuf);
}
if (cur_tx == NULL)
return;
if (sc->tl_cdata.tl_tx_head == NULL) {
sc->tl_cdata.tl_tx_head = start_tx;
sc->tl_cdata.tl_tx_tail = cur_tx;
if (sc->tl_txeoc) {
sc->tl_txeoc = 0;
CSR_WRITE_4(sc, TL_CH_PARM, vtophys(start_tx->tl_ptr));
cmd = CSR_READ_4(sc, TL_HOSTCMD);
cmd &= ~TL_CMD_RT;
cmd |= TL_CMD_GO|TL_CMD_INTSON;
CMD_PUT(sc, cmd);
}
} else {
sc->tl_cdata.tl_tx_tail->tl_next = start_tx;
sc->tl_cdata.tl_tx_tail = cur_tx;
}
ifp->if_timer = 5;
return;
}
static void
tl_init(void *xsc)
{
struct tl_softc *sc = xsc;
struct ifnet *ifp = &sc->arpcom.ac_if;
struct mii_data *mii;
tl_stop(sc);
tl_dio_clrbit(sc, TL_ACOMMIT, TL_AC_TXTHRESH);
tl_dio_setbit(sc, TL_ACOMMIT, TL_AC_TXTHRESH_16LONG);
tl_dio_write8(sc, TL_BSIZEREG, TL_RXBURST_16LONG|TL_TXBURST_16LONG);
if (ifp->if_flags & IFF_PROMISC)
tl_dio_setbit(sc, TL_NETCMD, TL_CMD_CAF);
else
tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_CAF);
if (ifp->if_flags & IFF_BROADCAST)
tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_NOBRX);
else
tl_dio_setbit(sc, TL_NETCMD, TL_CMD_NOBRX);
tl_dio_write16(sc, TL_MAXRX, MCLBYTES);
tl_setfilt(sc, (caddr_t)&sc->arpcom.ac_enaddr, 0);
tl_setmulti(sc);
if (tl_list_rx_init(sc) == ENOBUFS) {
if_printf(ifp, "initialization failed: no "
"memory for rx buffers\n");
tl_stop(sc);
return;
}
tl_list_tx_init(sc);
CMD_SET(sc, TL_CMD_INTSON);
CMD_SET(sc, TL_CMD_RT);
CSR_WRITE_4(sc, TL_CH_PARM, vtophys(&sc->tl_ldata->tl_rx_list[0]));
if (!sc->tl_bitrate) {
if (sc->tl_miibus != NULL) {
mii = device_get_softc(sc->tl_miibus);
mii_mediachg(mii);
}
}
CMD_SET(sc, TL_CMD_GO|TL_CMD_NES|TL_CMD_RT);
ifp->if_flags |= IFF_RUNNING;
ifq_clr_oactive(&ifp->if_snd);
callout_reset(&sc->tl_stat_timer, hz, tl_stats_update, sc);
}
static int
tl_ifmedia_upd(struct ifnet *ifp)
{
struct tl_softc *sc;
struct mii_data *mii = NULL;
sc = ifp->if_softc;
if (sc->tl_bitrate)
tl_setmode(sc, sc->ifmedia.ifm_media);
else {
mii = device_get_softc(sc->tl_miibus);
mii_mediachg(mii);
}
return(0);
}
static void
tl_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
{
struct tl_softc *sc;
struct mii_data *mii;
sc = ifp->if_softc;
ifmr->ifm_active = IFM_ETHER;
if (sc->tl_bitrate) {
if (tl_dio_read8(sc, TL_ACOMMIT) & TL_AC_MTXD1)
ifmr->ifm_active = IFM_ETHER|IFM_10_5;
else
ifmr->ifm_active = IFM_ETHER|IFM_10_T;
if (tl_dio_read8(sc, TL_ACOMMIT) & TL_AC_MTXD3)
ifmr->ifm_active |= IFM_HDX;
else
ifmr->ifm_active |= IFM_FDX;
return;
} else {
mii = device_get_softc(sc->tl_miibus);
mii_pollstat(mii);
ifmr->ifm_active = mii->mii_media_active;
ifmr->ifm_status = mii->mii_media_status;
}
return;
}
static int
tl_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
{
struct tl_softc *sc = ifp->if_softc;
struct ifreq *ifr = (struct ifreq *) data;
int error = 0;
switch(command) {
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_UP) {
if (ifp->if_flags & IFF_RUNNING &&
ifp->if_flags & IFF_PROMISC &&
!(sc->tl_if_flags & IFF_PROMISC)) {
tl_dio_setbit(sc, TL_NETCMD, TL_CMD_CAF);
tl_setmulti(sc);
} else if (ifp->if_flags & IFF_RUNNING &&
!(ifp->if_flags & IFF_PROMISC) &&
sc->tl_if_flags & IFF_PROMISC) {
tl_dio_clrbit(sc, TL_NETCMD, TL_CMD_CAF);
tl_setmulti(sc);
} else
tl_init(sc);
} else {
if (ifp->if_flags & IFF_RUNNING) {
tl_stop(sc);
}
}
sc->tl_if_flags = ifp->if_flags;
error = 0;
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
tl_setmulti(sc);
error = 0;
break;
case SIOCSIFMEDIA:
case SIOCGIFMEDIA:
if (sc->tl_bitrate)
error = ifmedia_ioctl(ifp, ifr, &sc->ifmedia, command);
else {
struct mii_data *mii;
mii = device_get_softc(sc->tl_miibus);
error = ifmedia_ioctl(ifp, ifr,
&mii->mii_media, command);
}
break;
default:
error = ether_ioctl(ifp, command, data);
break;
}
return(error);
}
static void
tl_watchdog(struct ifnet *ifp)
{
struct tl_softc *sc;
sc = ifp->if_softc;
if_printf(ifp, "device timeout\n");
IFNET_STAT_INC(ifp, oerrors, 1);
tl_softreset(sc, 1);
tl_init(sc);
return;
}
static void
tl_stop(struct tl_softc *sc)
{
int i;
struct ifnet *ifp;
ifp = &sc->arpcom.ac_if;
callout_stop(&sc->tl_stat_timer);
CMD_CLR(sc, TL_CMD_RT);
CMD_SET(sc, TL_CMD_STOP);
CSR_WRITE_4(sc, TL_CH_PARM, 0);
CMD_SET(sc, TL_CMD_RT);
CMD_SET(sc, TL_CMD_STOP);
CSR_WRITE_4(sc, TL_CH_PARM, 0);
CMD_SET(sc, TL_CMD_INTSOFF);
CSR_WRITE_4(sc, TL_CH_PARM, 0);
for (i = 0; i < TL_RX_LIST_CNT; i++) {
if (sc->tl_cdata.tl_rx_chain[i].tl_mbuf != NULL) {
m_freem(sc->tl_cdata.tl_rx_chain[i].tl_mbuf);
sc->tl_cdata.tl_rx_chain[i].tl_mbuf = NULL;
}
}
bzero((char *)&sc->tl_ldata->tl_rx_list,
sizeof(sc->tl_ldata->tl_rx_list));
for (i = 0; i < TL_TX_LIST_CNT; i++) {
if (sc->tl_cdata.tl_tx_chain[i].tl_mbuf != NULL) {
m_freem(sc->tl_cdata.tl_tx_chain[i].tl_mbuf);
sc->tl_cdata.tl_tx_chain[i].tl_mbuf = NULL;
}
}
bzero((char *)&sc->tl_ldata->tl_tx_list,
sizeof(sc->tl_ldata->tl_tx_list));
ifp->if_flags &= ~IFF_RUNNING;
ifq_clr_oactive(&ifp->if_snd);
return;
}
static void
tl_shutdown(device_t dev)
{
struct tl_softc *sc;
sc = device_get_softc(dev);
tl_stop(sc);
return;
}