#include "opt_ifpoll.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/mbuf.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/interrupt.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/ifq_var.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/bpf.h>
#include <sys/sockio.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <net/ethernet.h>
#include <net/if_arp.h>
#include <net/if_poll.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <net/if_types.h>
#include <net/vlan/if_vlan_var.h>
#include <bus/pci/pcivar.h>
#include <bus/pci/pcireg.h>
#include "../mii_layer/mii.h"
#include "../mii_layer/miivar.h"
#include "if_fxpreg.h"
#include "if_fxpvar.h"
#include "rcvbundl.h"
#include "miibus_if.h"
#define RFA_ALIGNMENT_FUDGE 2
static int tx_threshold = 64;
static u_char fxp_cb_config_template[] = {
0x0, 0x0,
0x0, 0x0,
0x0, 0x0, 0x0, 0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x0,
0x32,
0x0,
0x0,
0x0,
0x6,
0x0,
0x0,
0x0,
0xf2,
0x48,
0x0,
0x40,
0xf0,
0x0,
0x3f,
0x5
};
struct fxp_ident {
u_int16_t devid;
int16_t revid;
char *name;
};
static struct fxp_ident fxp_ident_table[] = {
{ 0x1029, -1, "Intel 82559 PCI/CardBus Pro/100" },
{ 0x1030, -1, "Intel 82559 Pro/100 Ethernet" },
{ 0x1031, -1, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
{ 0x1032, -1, "Intel 82801CAM (ICH3) Pro/100 VE Ethernet" },
{ 0x1033, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
{ 0x1034, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
{ 0x1035, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
{ 0x1036, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
{ 0x1037, -1, "Intel 82801CAM (ICH3) Pro/100 Ethernet" },
{ 0x1038, -1, "Intel 82801CAM (ICH3) Pro/100 VM Ethernet" },
{ 0x1039, -1, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
{ 0x103A, -1, "Intel 82801DB (ICH4) Pro/100 Ethernet" },
{ 0x103B, -1, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
{ 0x103C, -1, "Intel 82801DB (ICH4) Pro/100 Ethernet" },
{ 0x103D, -1, "Intel 82801DB (ICH4) Pro/100 VE Ethernet" },
{ 0x103E, -1, "Intel 82801DB (ICH4) Pro/100 VM Ethernet" },
{ 0x1050, -1, "Intel 82801BA (D865) Pro/100 VE Ethernet" },
{ 0x1051, -1, "Intel 82562ET (ICH5/ICH5R) Pro/100 VE Ethernet" },
{ 0x1059, -1, "Intel 82551QM Pro/100 M Mobile Connection" },
{ 0x1064, -1, "Intel 82562ET/EZ/GT/GZ (ICH6/ICH6R) Pro/100 VE Ethernet" },
{ 0x1065, -1, "Intel 82562ET/EZ/GT/GZ PRO/100 VE Ethernet" },
{ 0x1068, -1, "Intel 82801FBM (ICH6-M) Pro/100 VE Ethernet" },
{ 0x1069, -1, "Intel 82562EM/EX/GX Pro/100 Ethernet" },
{ 0x1091, -1, "Intel 82562GX Pro/100 Ethernet" },
{ 0x1092, -1, "Intel Pro/100 VE Network Connection" },
{ 0x1093, -1, "Intel Pro/100 VM Network Connection" },
{ 0x1094, -1, "Intel Pro/100 946GZ (ICH7) Network Connection" },
{ 0x1209, -1, "Intel 82559ER Embedded 10/100 Ethernet" },
{ 0x1229, 0x01, "Intel 82557 Pro/100 Ethernet" },
{ 0x1229, 0x02, "Intel 82557 Pro/100 Ethernet" },
{ 0x1229, 0x03, "Intel 82557 Pro/100 Ethernet" },
{ 0x1229, 0x04, "Intel 82558 Pro/100 Ethernet" },
{ 0x1229, 0x05, "Intel 82558 Pro/100 Ethernet" },
{ 0x1229, 0x06, "Intel 82559 Pro/100 Ethernet" },
{ 0x1229, 0x07, "Intel 82559 Pro/100 Ethernet" },
{ 0x1229, 0x08, "Intel 82559 Pro/100 Ethernet" },
{ 0x1229, 0x09, "Intel 82559ER Pro/100 Ethernet" },
{ 0x1229, 0x0c, "Intel 82550 Pro/100 Ethernet" },
{ 0x1229, 0x0d, "Intel 82550 Pro/100 Ethernet" },
{ 0x1229, 0x0e, "Intel 82550 Pro/100 Ethernet" },
{ 0x1229, 0x0f, "Intel 82551 Pro/100 Ethernet" },
{ 0x1229, 0x10, "Intel 82551 Pro/100 Ethernet" },
{ 0x1229, -1, "Intel 82557/8/9 Pro/100 Ethernet" },
{ 0x2449, -1, "Intel 82801BA/CAM (ICH2/3) Pro/100 Ethernet" },
{ 0x27dc, -1, "Intel 82801GB (ICH7) 10/100 Ethernet" },
{ 0, -1, NULL },
};
static int fxp_probe(device_t dev);
static int fxp_attach(device_t dev);
static int fxp_detach(device_t dev);
static int fxp_shutdown(device_t dev);
static int fxp_suspend(device_t dev);
static int fxp_resume(device_t dev);
static void fxp_intr(void *xsc);
static void fxp_intr_body(struct fxp_softc *sc,
u_int8_t statack, int count);
static void fxp_init(void *xsc);
static void fxp_tick(void *xsc);
static void fxp_powerstate_d0(device_t dev);
static void fxp_start(struct ifnet *ifp, struct ifaltq_subque *);
static void fxp_stop(struct fxp_softc *sc);
static void fxp_release(device_t dev);
static int fxp_ioctl(struct ifnet *ifp, u_long command,
caddr_t data, struct ucred *);
static void fxp_watchdog(struct ifnet *ifp);
static int fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm);
static int fxp_mc_addrs(struct fxp_softc *sc);
static void fxp_mc_setup(struct fxp_softc *sc);
static u_int16_t fxp_eeprom_getword(struct fxp_softc *sc, int offset,
int autosize);
static void fxp_eeprom_putword(struct fxp_softc *sc, int offset,
u_int16_t data);
static void fxp_autosize_eeprom(struct fxp_softc *sc);
static void fxp_read_eeprom(struct fxp_softc *sc, u_short *data,
int offset, int words);
static void fxp_write_eeprom(struct fxp_softc *sc, u_short *data,
int offset, int words);
static int fxp_ifmedia_upd(struct ifnet *ifp);
static void fxp_ifmedia_sts(struct ifnet *ifp,
struct ifmediareq *ifmr);
static int fxp_serial_ifmedia_upd(struct ifnet *ifp);
static void fxp_serial_ifmedia_sts(struct ifnet *ifp,
struct ifmediareq *ifmr);
static int fxp_miibus_readreg(device_t dev, int phy, int reg);
static void fxp_miibus_writereg(device_t dev, int phy, int reg,
int value);
static void fxp_load_ucode(struct fxp_softc *sc);
static int sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS);
static int sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS);
#ifdef IFPOLL_ENABLE
static void fxp_npoll(struct ifnet *, struct ifpoll_info *);
static void fxp_npoll_compat(struct ifnet *, void *, int);
#endif
static void fxp_lwcopy(volatile u_int32_t *src,
volatile u_int32_t *dst);
static void fxp_scb_wait(struct fxp_softc *sc);
static void fxp_scb_cmd(struct fxp_softc *sc, int cmd);
static void fxp_dma_wait(volatile u_int16_t *status,
struct fxp_softc *sc);
static device_method_t fxp_methods[] = {
DEVMETHOD(device_probe, fxp_probe),
DEVMETHOD(device_attach, fxp_attach),
DEVMETHOD(device_detach, fxp_detach),
DEVMETHOD(device_shutdown, fxp_shutdown),
DEVMETHOD(device_suspend, fxp_suspend),
DEVMETHOD(device_resume, fxp_resume),
DEVMETHOD(miibus_readreg, fxp_miibus_readreg),
DEVMETHOD(miibus_writereg, fxp_miibus_writereg),
DEVMETHOD_END
};
static driver_t fxp_driver = {
"fxp",
fxp_methods,
sizeof(struct fxp_softc),
};
static devclass_t fxp_devclass;
DECLARE_DUMMY_MODULE(if_fxp);
MODULE_DEPEND(if_fxp, miibus, 1, 1, 1);
DRIVER_MODULE(if_fxp, pci, fxp_driver, fxp_devclass, NULL, NULL);
DRIVER_MODULE(if_fxp, cardbus, fxp_driver, fxp_devclass, NULL, NULL);
DRIVER_MODULE(miibus, fxp, miibus_driver, miibus_devclass, NULL, NULL);
static int fxp_rnr;
SYSCTL_INT(_hw, OID_AUTO, fxp_rnr, CTLFLAG_RW, &fxp_rnr, 0, "fxp rnr events");
static void
fxp_lwcopy(volatile u_int32_t *src, volatile u_int32_t *dst)
{
volatile u_int16_t *a = (volatile u_int16_t *)src;
volatile u_int16_t *b = (volatile u_int16_t *)dst;
b[0] = a[0];
b[1] = a[1];
}
static void
fxp_scb_wait(struct fxp_softc *sc)
{
int i = 10000;
while (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) && --i)
DELAY(2);
if (i == 0) {
if_printf(&sc->arpcom.ac_if,
"SCB timeout: 0x%x 0x%x 0x%x 0x%x\n",
CSR_READ_1(sc, FXP_CSR_SCB_COMMAND),
CSR_READ_1(sc, FXP_CSR_SCB_STATACK),
CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS),
CSR_READ_2(sc, FXP_CSR_FLOWCONTROL));
}
}
static void
fxp_scb_cmd(struct fxp_softc *sc, int cmd)
{
if (cmd == FXP_SCB_COMMAND_CU_RESUME && sc->cu_resume_bug) {
CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, FXP_CB_COMMAND_NOP);
fxp_scb_wait(sc);
}
CSR_WRITE_1(sc, FXP_CSR_SCB_COMMAND, cmd);
}
static void
fxp_dma_wait(volatile u_int16_t *status, struct fxp_softc *sc)
{
int i = 10000;
while (!(*status & FXP_CB_STATUS_C) && --i)
DELAY(2);
if (i == 0)
if_printf(&sc->arpcom.ac_if, "DMA timeout\n");
}
static int
fxp_probe(device_t dev)
{
u_int16_t devid;
u_int8_t revid;
struct fxp_ident *ident;
if (pci_get_vendor(dev) == FXP_VENDORID_INTEL) {
devid = pci_get_device(dev);
revid = pci_get_revid(dev);
for (ident = fxp_ident_table; ident->name != NULL; ident++) {
if (ident->devid == devid &&
(ident->revid == revid || ident->revid == -1)) {
device_set_desc(dev, ident->name);
return (0);
}
}
}
return (ENXIO);
}
static void
fxp_powerstate_d0(device_t dev)
{
u_int32_t iobase, membase, irq;
if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
iobase = pci_read_config(dev, FXP_PCI_IOBA, 4);
membase = pci_read_config(dev, FXP_PCI_MMBA, 4);
irq = pci_read_config(dev, PCIR_INTLINE, 4);
device_printf(dev, "chip is in %s power mode "
"-- setting to D0\n",
pci_powerstate_to_str(pci_get_powerstate(dev)));
pci_set_powerstate(dev, PCI_POWERSTATE_D0);
pci_write_config(dev, FXP_PCI_IOBA, iobase, 4);
pci_write_config(dev, FXP_PCI_MMBA, membase, 4);
pci_write_config(dev, PCIR_INTLINE, irq, 4);
}
}
static int
fxp_attach(device_t dev)
{
int error = 0;
struct fxp_softc *sc = device_get_softc(dev);
struct ifnet *ifp;
struct sysctl_ctx_list *ctx;
struct sysctl_oid *tree;
u_int32_t val;
u_int16_t data;
int i, rid, m1, m2, prefer_iomap;
callout_init(&sc->fxp_stat_timer);
pci_enable_busmaster(dev);
pci_enable_io(dev, SYS_RES_MEMORY);
val = pci_read_config(dev, PCIR_COMMAND, 2);
fxp_powerstate_d0(dev);
m1 = PCIM_CMD_MEMEN;
m2 = PCIM_CMD_PORTEN;
prefer_iomap = 0;
if (resource_int_value(device_get_name(dev), device_get_unit(dev),
"prefer_iomap", &prefer_iomap) == 0 && prefer_iomap != 0) {
m1 = PCIM_CMD_PORTEN;
m2 = PCIM_CMD_MEMEN;
}
if (val & m1) {
sc->rtp =
(m1 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
sc->rgd = (m1 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
sc->mem = bus_alloc_resource_any(dev, sc->rtp, &sc->rgd,
RF_ACTIVE);
}
if (sc->mem == NULL && (val & m2)) {
sc->rtp =
(m2 == PCIM_CMD_MEMEN)? SYS_RES_MEMORY : SYS_RES_IOPORT;
sc->rgd = (m2 == PCIM_CMD_MEMEN)? FXP_PCI_MMBA : FXP_PCI_IOBA;
sc->mem = bus_alloc_resource_any(dev, sc->rtp, &sc->rgd,
RF_ACTIVE);
}
if (!sc->mem) {
device_printf(dev, "could not map device registers\n");
error = ENXIO;
goto fail;
}
if (bootverbose) {
device_printf(dev, "using %s space register mapping\n",
sc->rtp == SYS_RES_MEMORY? "memory" : "I/O");
}
sc->sc_st = rman_get_bustag(sc->mem);
sc->sc_sh = rman_get_bushandle(sc->mem);
rid = 0;
sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
RF_SHAREABLE | RF_ACTIVE);
if (sc->irq == NULL) {
device_printf(dev, "could not map interrupt\n");
error = ENXIO;
goto fail;
}
CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
DELAY(10);
sc->cbl_base = kmalloc(sizeof(struct fxp_cb_tx) * FXP_NTXCB,
M_DEVBUF, M_WAITOK | M_ZERO);
sc->fxp_stats = kmalloc(sizeof(struct fxp_stats), M_DEVBUF,
M_WAITOK | M_ZERO);
sc->mcsp = kmalloc(sizeof(struct fxp_cb_mcs), M_DEVBUF, M_WAITOK);
for (i = 0; i < FXP_NRFABUFS; i++) {
if (fxp_add_rfabuf(sc, NULL) != 0) {
goto failmem;
}
}
fxp_autosize_eeprom(sc);
fxp_read_eeprom(sc, &data, 6, 1);
if ((data & FXP_PHY_DEVICE_MASK) != 0 &&
(data & FXP_PHY_SERIAL_ONLY))
sc->flags |= FXP_FLAG_SERIAL_MEDIA;
ctx = device_get_sysctl_ctx(dev);
tree = device_get_sysctl_tree(dev);
SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
OID_AUTO, "int_delay", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
&sc->tunable_int_delay, 0, &sysctl_hw_fxp_int_delay, "I",
"FXP driver receive interrupt microcode bundling delay");
SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree),
OID_AUTO, "bundle_max", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_PRISON,
&sc->tunable_bundle_max, 0, &sysctl_hw_fxp_bundle_max, "I",
"FXP driver receive interrupt microcode bundle size limit");
sc->tunable_int_delay = TUNABLE_INT_DELAY;
sc->tunable_bundle_max = TUNABLE_BUNDLE_MAX;
resource_int_value(device_get_name(dev), device_get_unit(dev),
"int_delay", &sc->tunable_int_delay);
resource_int_value(device_get_name(dev), device_get_unit(dev),
"bundle_max", &sc->tunable_bundle_max);
fxp_read_eeprom(sc, &data, 5, 1);
if ((data >> 8) == 1)
sc->revision = FXP_REV_82557;
else
sc->revision = pci_get_revid(dev);
i = pci_get_device(dev);
if (i == 0x2449 || (i > 0x1030 && i < 0x1039) ||
sc->revision >= FXP_REV_82559_A0) {
fxp_read_eeprom(sc, &data, 10, 1);
if (data & 0x02) {
u_int16_t cksum;
int i;
device_printf(dev,
"Disabling dynamic standby mode in EEPROM\n");
data &= ~0x02;
fxp_write_eeprom(sc, &data, 10, 1);
device_printf(dev, "New EEPROM ID: 0x%x\n", data);
cksum = 0;
for (i = 0; i < (1 << sc->eeprom_size) - 1; i++) {
fxp_read_eeprom(sc, &data, i, 1);
cksum += data;
}
i = (1 << sc->eeprom_size) - 1;
cksum = 0xBABA - cksum;
fxp_read_eeprom(sc, &data, i, 1);
fxp_write_eeprom(sc, &cksum, i, 1);
device_printf(dev,
"EEPROM checksum @ 0x%x: 0x%x -> 0x%x\n",
i, data, cksum);
#if 1
sc->flags |= FXP_FLAG_CU_RESUME_BUG;
#endif
}
}
if (sc->revision != FXP_REV_82557) {
if (val & PCIM_CMD_MWRICEN &&
pci_read_config(dev, PCIR_CACHELNSZ, 1) != 0)
sc->flags |= FXP_FLAG_MWI_ENABLE;
sc->flags |= FXP_FLAG_EXT_TXCB;
sc->flags |= FXP_FLAG_LONG_PKT_EN;
}
fxp_read_eeprom(sc, (u_int16_t *)sc->arpcom.ac_enaddr, 0, 3);
if (sc->flags & FXP_FLAG_SERIAL_MEDIA)
device_printf(dev, "10Mbps\n");
if (bootverbose) {
device_printf(dev, "PCI IDs: %04x %04x %04x %04x %04x\n",
pci_get_vendor(dev), pci_get_device(dev),
pci_get_subvendor(dev), pci_get_subdevice(dev),
pci_get_revid(dev));
fxp_read_eeprom(sc, &data, 10, 1);
device_printf(dev, "Dynamic Standby mode is %s\n",
data & 0x02 ? "enabled" : "disabled");
}
if (sc->flags & FXP_FLAG_SERIAL_MEDIA) {
ifmedia_init(&sc->sc_media, 0, fxp_serial_ifmedia_upd,
fxp_serial_ifmedia_sts);
ifmedia_add(&sc->sc_media, IFM_ETHER|IFM_MANUAL, 0, NULL);
ifmedia_set(&sc->sc_media, IFM_ETHER|IFM_MANUAL);
} else {
if (mii_phy_probe(dev, &sc->miibus, fxp_ifmedia_upd,
fxp_ifmedia_sts)) {
device_printf(dev, "MII without any PHY!\n");
error = ENXIO;
goto fail;
}
}
ifp = &sc->arpcom.ac_if;
if_initname(ifp, device_get_name(dev), device_get_unit(dev));
ifp->if_baudrate = 100000000;
ifp->if_init = fxp_init;
ifp->if_softc = sc;
ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
ifp->if_ioctl = fxp_ioctl;
ifp->if_start = fxp_start;
#ifdef IFPOLL_ENABLE
ifp->if_npoll = fxp_npoll;
#endif
ifp->if_watchdog = fxp_watchdog;
ether_ifattach(ifp, sc->arpcom.ac_enaddr, NULL);
#ifdef IFPOLL_ENABLE
ifpoll_compat_setup(&sc->fxp_npoll, ctx, (struct sysctl_oid *)tree,
device_get_unit(dev), ifp->if_serializer);
#endif
ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
ifq_set_maxlen(&ifp->if_snd, FXP_USABLE_TXCB);
ifq_set_ready(&ifp->if_snd);
ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->irq));
error = bus_setup_intr(dev, sc->irq, INTR_MPSAFE,
fxp_intr, sc, &sc->ih,
ifp->if_serializer);
if (error) {
ether_ifdetach(ifp);
if (sc->flags & FXP_FLAG_SERIAL_MEDIA)
ifmedia_removeall(&sc->sc_media);
device_printf(dev, "could not setup irq\n");
goto fail;
}
return (0);
failmem:
device_printf(dev, "Failed to malloc memory\n");
error = ENOMEM;
fail:
fxp_release(dev);
return (error);
}
static void
fxp_release(device_t dev)
{
struct fxp_softc *sc = device_get_softc(dev);
if (sc->miibus)
device_delete_child(dev, sc->miibus);
bus_generic_detach(dev);
if (sc->cbl_base)
kfree(sc->cbl_base, M_DEVBUF);
if (sc->fxp_stats)
kfree(sc->fxp_stats, M_DEVBUF);
if (sc->mcsp)
kfree(sc->mcsp, M_DEVBUF);
if (sc->rfa_headm)
m_freem(sc->rfa_headm);
if (sc->irq)
bus_release_resource(dev, SYS_RES_IRQ, 0, sc->irq);
if (sc->mem)
bus_release_resource(dev, sc->rtp, sc->rgd, sc->mem);
}
static int
fxp_detach(device_t dev)
{
struct fxp_softc *sc = device_get_softc(dev);
lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
fxp_stop(sc);
CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
if (sc->flags & FXP_FLAG_SERIAL_MEDIA)
ifmedia_removeall(&sc->sc_media);
if (sc->ih)
bus_teardown_intr(dev, sc->irq, sc->ih);
lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
ether_ifdetach(&sc->arpcom.ac_if);
fxp_release(dev);
return (0);
}
static int
fxp_shutdown(device_t dev)
{
struct fxp_softc *sc = device_get_softc(dev);
struct ifnet *ifp = &sc->arpcom.ac_if;
lwkt_serialize_enter(ifp->if_serializer);
fxp_stop(sc);
lwkt_serialize_exit(ifp->if_serializer);
return (0);
}
static int
fxp_suspend(device_t dev)
{
struct fxp_softc *sc = device_get_softc(dev);
int i;
lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
fxp_stop(sc);
for (i = 0; i < 5; i++)
sc->saved_maps[i] = pci_read_config(dev, PCIR_BAR(i), 4);
sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
sc->suspended = 1;
lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
return (0);
}
static int
fxp_resume(device_t dev)
{
struct fxp_softc *sc = device_get_softc(dev);
struct ifnet *ifp = &sc->arpcom.ac_if;
int i;
lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
fxp_powerstate_d0(dev);
for (i = 0; i < 5; i++)
pci_write_config(dev, PCIR_BAR(i), sc->saved_maps[i], 4);
pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
pci_enable_busmaster(dev);
pci_enable_io(dev, SYS_RES_MEMORY);
CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SELECTIVE_RESET);
DELAY(10);
if (ifp->if_flags & IFF_UP)
fxp_init(sc);
sc->suspended = 0;
lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
return (0);
}
static void
fxp_eeprom_shiftin(struct fxp_softc *sc, int data, int length)
{
u_int16_t reg;
int x;
for (x = 1 << (length - 1); x; x >>= 1) {
if (data & x)
reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
else
reg = FXP_EEPROM_EECS;
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
DELAY(1);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
DELAY(1);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
DELAY(1);
}
}
static u_int16_t
fxp_eeprom_getword(struct fxp_softc *sc, int offset, int autosize)
{
u_int16_t reg, data;
int x;
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_READ, 3);
data = 0;
for (x = 1 << (sc->eeprom_size - 1); x; x >>= 1) {
if (offset & x)
reg = FXP_EEPROM_EECS | FXP_EEPROM_EEDI;
else
reg = FXP_EEPROM_EECS;
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
DELAY(1);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
DELAY(1);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
DELAY(1);
reg = CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO;
data++;
if (autosize && reg == 0) {
sc->eeprom_size = data;
break;
}
}
data = 0;
reg = FXP_EEPROM_EECS;
for (x = 1 << 15; x; x >>= 1) {
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg | FXP_EEPROM_EESK);
DELAY(1);
if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
data |= x;
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, reg);
DELAY(1);
}
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
DELAY(1);
return (data);
}
static void
fxp_eeprom_putword(struct fxp_softc *sc, int offset, u_int16_t data)
{
int i;
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
fxp_eeprom_shiftin(sc, 0x4, 3);
fxp_eeprom_shiftin(sc, 0x03 << (sc->eeprom_size - 2), sc->eeprom_size);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
DELAY(1);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
fxp_eeprom_shiftin(sc, FXP_EEPROM_OPC_WRITE, 3);
fxp_eeprom_shiftin(sc, offset, sc->eeprom_size);
fxp_eeprom_shiftin(sc, data, 16);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
DELAY(1);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
DELAY(1);
for (i = 0; i < 1000; i++) {
if (CSR_READ_2(sc, FXP_CSR_EEPROMCONTROL) & FXP_EEPROM_EEDO)
break;
DELAY(50);
}
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
DELAY(1);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, FXP_EEPROM_EECS);
fxp_eeprom_shiftin(sc, 0x4, 3);
fxp_eeprom_shiftin(sc, 0, sc->eeprom_size);
CSR_WRITE_2(sc, FXP_CSR_EEPROMCONTROL, 0);
DELAY(1);
}
static void
fxp_autosize_eeprom(struct fxp_softc *sc)
{
sc->eeprom_size = 8;
fxp_eeprom_getword(sc, 0, 1);
}
static void
fxp_read_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
{
int i;
for (i = 0; i < words; i++)
data[i] = fxp_eeprom_getword(sc, offset + i, 0);
}
static void
fxp_write_eeprom(struct fxp_softc *sc, u_short *data, int offset, int words)
{
int i;
for (i = 0; i < words; i++)
fxp_eeprom_putword(sc, offset + i, data[i]);
}
static void
fxp_start(struct ifnet *ifp, struct ifaltq_subque *ifsq)
{
struct fxp_softc *sc = ifp->if_softc;
struct fxp_cb_tx *txp;
ASSERT_ALTQ_SQ_DEFAULT(ifp, ifsq);
ASSERT_SERIALIZED(ifp->if_serializer);
if (sc->need_mcsetup) {
ifq_purge(&ifp->if_snd);
return;
}
if ((ifp->if_flags & IFF_RUNNING) == 0 || ifq_is_oactive(&ifp->if_snd))
return;
txp = NULL;
while (!ifq_is_empty(&ifp->if_snd) && sc->tx_queued < FXP_USABLE_TXCB) {
struct mbuf *m, *mb_head;
int segment, ntries = 0;
mb_head = ifq_dequeue(&ifp->if_snd);
if (mb_head == NULL)
break;
tbdinit:
segment = 0;
for (m = mb_head; m != NULL; m = m->m_next) {
if (m->m_len != 0) {
++segment;
if (segment >= FXP_NTXSEG)
break;
}
}
if (segment >= FXP_NTXSEG) {
struct mbuf *mn;
if (ntries) {
m_freem(mb_head);
IFNET_STAT_INC(ifp, oerrors, 1);
continue;
}
mn = m_dup(mb_head, M_NOWAIT);
if (mn == NULL) {
m_freem(mb_head);
IFNET_STAT_INC(ifp, oerrors, 1);
continue;
}
m_freem(mb_head);
mb_head = mn;
ntries = 1;
goto tbdinit;
}
txp = sc->cbl_last->next;
for (m = mb_head, segment = 0; m != NULL; m = m->m_next) {
if (m->m_len != 0) {
KKASSERT(segment < FXP_NTXSEG);
txp->tbd[segment].tb_addr =
vtophys(mtod(m, vm_offset_t));
txp->tbd[segment].tb_size = m->m_len;
segment++;
}
}
KKASSERT(m == NULL);
txp->tbd_number = segment;
txp->mb_head = mb_head;
txp->cb_status = 0;
if (sc->tx_queued != FXP_CXINT_THRESH - 1) {
txp->cb_command =
FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
FXP_CB_COMMAND_S;
} else {
txp->cb_command =
FXP_CB_COMMAND_XMIT | FXP_CB_COMMAND_SF |
FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
}
txp->tx_threshold = tx_threshold;
sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
sc->cbl_last = txp;
if (sc->tx_queued == 0)
sc->cbl_first = txp;
sc->tx_queued++;
ifp->if_timer = 5;
BPF_MTAP(ifp, mb_head);
}
if (sc->tx_queued >= FXP_USABLE_TXCB)
ifq_set_oactive(&ifp->if_snd);
if (txp != NULL) {
fxp_scb_wait(sc);
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
}
}
#ifdef IFPOLL_ENABLE
static void
fxp_npoll_compat(struct ifnet *ifp, void *arg __unused, int count)
{
struct fxp_softc *sc = ifp->if_softc;
u_int8_t statack;
ASSERT_SERIALIZED(ifp->if_serializer);
statack = FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA |
FXP_SCB_STATACK_FR;
if (sc->fxp_npoll.ifpc_stcount-- == 0) {
u_int8_t tmp;
sc->fxp_npoll.ifpc_stcount = sc->fxp_npoll.ifpc_stfrac;
tmp = CSR_READ_1(sc, FXP_CSR_SCB_STATACK);
if (tmp == 0xff || tmp == 0)
return;
tmp &= ~statack;
if (tmp != 0)
CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, tmp);
statack |= tmp;
}
fxp_intr_body(sc, statack, count);
}
static void
fxp_npoll(struct ifnet *ifp, struct ifpoll_info *info)
{
struct fxp_softc *sc = ifp->if_softc;
ASSERT_SERIALIZED(ifp->if_serializer);
if (info != NULL) {
int cpuid = sc->fxp_npoll.ifpc_cpuid;
info->ifpi_rx[cpuid].poll_func = fxp_npoll_compat;
info->ifpi_rx[cpuid].arg = NULL;
info->ifpi_rx[cpuid].serializer = ifp->if_serializer;
if (ifp->if_flags & IFF_RUNNING) {
CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL,
FXP_SCB_INTR_DISABLE);
sc->fxp_npoll.ifpc_stcount = 0;
}
ifq_set_cpuid(&ifp->if_snd, cpuid);
} else {
if (ifp->if_flags & IFF_RUNNING) {
CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
}
ifq_set_cpuid(&ifp->if_snd, rman_get_cpuid(sc->irq));
}
}
#endif
static void
fxp_intr(void *xsc)
{
struct fxp_softc *sc = xsc;
u_int8_t statack;
ASSERT_SERIALIZED(sc->arpcom.ac_if.if_serializer);
if (sc->suspended) {
return;
}
while ((statack = CSR_READ_1(sc, FXP_CSR_SCB_STATACK)) != 0) {
if (statack == 0xff)
return;
CSR_WRITE_1(sc, FXP_CSR_SCB_STATACK, statack);
fxp_intr_body(sc, statack, -1);
}
}
static void
fxp_intr_body(struct fxp_softc *sc, u_int8_t statack, int count)
{
struct ifnet *ifp = &sc->arpcom.ac_if;
struct mbuf *m;
struct fxp_rfa *rfa;
int rnr = (statack & FXP_SCB_STATACK_RNR) ? 1 : 0;
if (rnr)
fxp_rnr++;
#ifdef IFPOLL_ENABLE
if (sc->flags & FXP_FLAG_DEFERRED_RNR) {
sc->flags &= ~FXP_FLAG_DEFERRED_RNR;
rnr = 1;
}
#endif
if (statack & (FXP_SCB_STATACK_CXTNO | FXP_SCB_STATACK_CNA)) {
struct fxp_cb_tx *txp;
for (txp = sc->cbl_first; sc->tx_queued &&
(txp->cb_status & FXP_CB_STATUS_C) != 0;
txp = txp->next) {
if ((m = txp->mb_head) != NULL) {
txp->mb_head = NULL;
sc->tx_queued--;
m_freem(m);
} else {
sc->tx_queued--;
}
}
sc->cbl_first = txp;
if (sc->tx_queued < FXP_USABLE_TXCB)
ifq_clr_oactive(&ifp->if_snd);
if (sc->tx_queued == 0) {
ifp->if_timer = 0;
if (sc->need_mcsetup)
fxp_mc_setup(sc);
}
if (!ifq_is_empty(&ifp->if_snd))
if_devstart(ifp);
}
if (!rnr && (statack & FXP_SCB_STATACK_FR) == 0)
return;
for (;;) {
m = sc->rfa_headm;
rfa = (struct fxp_rfa *)(m->m_ext.ext_buf +
RFA_ALIGNMENT_FUDGE);
#ifdef IFPOLL_ENABLE
if (count >= 0 && count-- == 0) {
if (rnr) {
sc->flags |= FXP_FLAG_DEFERRED_RNR;
rnr = 0;
}
break;
}
#endif
if ( (rfa->rfa_status & FXP_RFA_STATUS_C) == 0)
break;
sc->rfa_headm = m->m_next;
if (sc->rfa_headm == NULL)
sc->rfa_tailm = NULL;
m->m_next = NULL;
if (fxp_add_rfabuf(sc, m) == 0) {
int total_len;
total_len = rfa->actual_size & 0x3fff;
if (total_len < sizeof(struct ether_header) ||
total_len > MCLBYTES - RFA_ALIGNMENT_FUDGE -
sizeof(struct fxp_rfa) ||
(rfa->rfa_status & FXP_RFA_STATUS_CRC)) {
m_freem(m);
continue;
}
m->m_pkthdr.len = m->m_len = total_len;
ifp->if_input(ifp, m, NULL, -1);
}
}
if (rnr) {
fxp_scb_wait(sc);
CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
vtophys(sc->rfa_headm->m_ext.ext_buf) +
RFA_ALIGNMENT_FUDGE);
fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
}
}
static void
fxp_tick(void *xsc)
{
struct fxp_softc *sc = xsc;
struct ifnet *ifp = &sc->arpcom.ac_if;
struct fxp_stats *sp = sc->fxp_stats;
struct fxp_cb_tx *txp;
struct mbuf *m;
lwkt_serialize_enter(sc->arpcom.ac_if.if_serializer);
IFNET_STAT_INC(ifp, opackets, sp->tx_good);
IFNET_STAT_INC(ifp, collisions, sp->tx_total_collisions);
if (sp->rx_good) {
IFNET_STAT_INC(ifp, ipackets, sp->rx_good);
sc->rx_idle_secs = 0;
} else {
sc->rx_idle_secs++;
}
IFNET_STAT_INC(ifp, ierrors,
sp->rx_crc_errors +
sp->rx_alignment_errors +
sp->rx_rnr_errors +
sp->rx_overrun_errors);
if (sp->tx_underruns) {
IFNET_STAT_INC(ifp, oerrors, sp->tx_underruns);
if (tx_threshold < 192)
tx_threshold += 64;
}
for (txp = sc->cbl_first; sc->tx_queued &&
(txp->cb_status & FXP_CB_STATUS_C) != 0;
txp = txp->next) {
if ((m = txp->mb_head) != NULL) {
txp->mb_head = NULL;
sc->tx_queued--;
m_freem(m);
} else {
sc->tx_queued--;
}
}
sc->cbl_first = txp;
if (sc->tx_queued < FXP_USABLE_TXCB)
ifq_clr_oactive(&ifp->if_snd);
if (sc->tx_queued == 0)
ifp->if_timer = 0;
if (!ifq_is_empty(&ifp->if_snd))
if_devstart(ifp);
if (sc->rx_idle_secs > FXP_MAX_RX_IDLE) {
sc->rx_idle_secs = 0;
fxp_mc_setup(sc);
}
if (CSR_READ_1(sc, FXP_CSR_SCB_COMMAND) == 0) {
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMPRESET);
} else {
sp->tx_good = 0;
sp->tx_underruns = 0;
sp->tx_total_collisions = 0;
sp->rx_good = 0;
sp->rx_crc_errors = 0;
sp->rx_alignment_errors = 0;
sp->rx_rnr_errors = 0;
sp->rx_overrun_errors = 0;
}
if (sc->miibus != NULL)
mii_tick(device_get_softc(sc->miibus));
callout_reset(&sc->fxp_stat_timer, hz, fxp_tick, sc);
lwkt_serialize_exit(sc->arpcom.ac_if.if_serializer);
}
static void
fxp_stop(struct fxp_softc *sc)
{
struct ifnet *ifp = &sc->arpcom.ac_if;
struct fxp_cb_tx *txp;
int i;
ASSERT_SERIALIZED(ifp->if_serializer);
ifp->if_flags &= ~IFF_RUNNING;
ifq_clr_oactive(&ifp->if_snd);
ifp->if_timer = 0;
callout_stop(&sc->fxp_stat_timer);
sc->flags &= ~FXP_FLAG_UCODE;
CSR_WRITE_4(sc, FXP_CSR_PORT, FXP_PORT_SOFTWARE_RESET);
DELAY(50);
txp = sc->cbl_base;
if (txp != NULL) {
for (i = 0; i < FXP_NTXCB; i++) {
if (txp[i].mb_head != NULL) {
m_freem(txp[i].mb_head);
txp[i].mb_head = NULL;
}
}
}
sc->tx_queued = 0;
if (sc->rfa_headm != NULL)
m_freem(sc->rfa_headm);
sc->rfa_headm = NULL;
sc->rfa_tailm = NULL;
for (i = 0; i < FXP_NRFABUFS; i++) {
if (fxp_add_rfabuf(sc, NULL) != 0) {
panic("fxp_stop: no buffers!");
}
}
}
static void
fxp_watchdog(struct ifnet *ifp)
{
ASSERT_SERIALIZED(ifp->if_serializer);
if_printf(ifp, "device timeout\n");
IFNET_STAT_INC(ifp, oerrors, 1);
fxp_init(ifp->if_softc);
}
static void
fxp_init(void *xsc)
{
struct fxp_softc *sc = xsc;
struct ifnet *ifp = &sc->arpcom.ac_if;
struct fxp_cb_config *cbp;
struct fxp_cb_ias *cb_ias;
struct fxp_cb_tx *txp;
struct fxp_cb_mcs *mcsp;
int i, prm;
ASSERT_SERIALIZED(ifp->if_serializer);
fxp_stop(sc);
prm = (ifp->if_flags & IFF_PROMISC) ? 1 : 0;
CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, 0);
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_BASE);
fxp_scb_wait(sc);
fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_BASE);
fxp_scb_wait(sc);
CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(sc->fxp_stats));
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_DUMP_ADR);
if (ifp->if_flags & IFF_LINK0 && (sc->flags & FXP_FLAG_UCODE) == 0)
fxp_load_ucode(sc);
if (fxp_mc_addrs(sc)) {
mcsp = sc->mcsp;
mcsp->cb_status = 0;
mcsp->cb_command = FXP_CB_COMMAND_MCAS | FXP_CB_COMMAND_EL;
mcsp->link_addr = -1;
fxp_scb_wait(sc);
CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
fxp_dma_wait(&mcsp->cb_status, sc);
}
cbp = (struct fxp_cb_config *) sc->cbl_base;
bcopy(fxp_cb_config_template,
(void *)(uintptr_t)(volatile void *)&cbp->cb_status,
sizeof(fxp_cb_config_template));
cbp->cb_status = 0;
cbp->cb_command = FXP_CB_COMMAND_CONFIG | FXP_CB_COMMAND_EL;
cbp->link_addr = -1;
cbp->byte_count = 22;
cbp->rx_fifo_limit = 8;
cbp->tx_fifo_limit = 0;
cbp->adaptive_ifs = 0;
cbp->mwi_enable = sc->flags & FXP_FLAG_MWI_ENABLE ? 1 : 0;
cbp->type_enable = 0;
cbp->read_align_en = sc->flags & FXP_FLAG_READ_ALIGN ? 1 : 0;
cbp->end_wr_on_cl = sc->flags & FXP_FLAG_WRITE_ALIGN ? 1 : 0;
cbp->rx_dma_bytecount = 0;
cbp->tx_dma_bytecount = 0;
cbp->dma_mbce = 0;
cbp->late_scb = 0;
cbp->direct_dma_dis = 1;
cbp->tno_int_or_tco_en =0;
cbp->ci_int = 1;
cbp->ext_txcb_dis = sc->flags & FXP_FLAG_EXT_TXCB ? 0 : 1;
cbp->ext_stats_dis = 1;
cbp->keep_overrun_rx = 0;
cbp->save_bf = sc->revision == FXP_REV_82557 ? 1 : prm;
cbp->disc_short_rx = !prm;
cbp->underrun_retry = 1;
cbp->two_frames = 0;
cbp->dyn_tbd = 0;
cbp->mediatype = sc->flags & FXP_FLAG_SERIAL_MEDIA ? 0 : 1;
cbp->csma_dis = 0;
cbp->tcp_udp_cksum = 0;
cbp->vlan_tco = 0;
cbp->link_wake_en = 0;
cbp->arp_wake_en = 0;
cbp->mc_wake_en = 0;
cbp->nsai = 1;
cbp->preamble_length = 2;
cbp->loopback = 0;
cbp->linear_priority = 0;
cbp->linear_pri_mode = 0;
cbp->interfrm_spacing = 6;
cbp->promiscuous = prm;
cbp->bcast_disable = 0;
cbp->wait_after_win = 0;
cbp->ignore_ul = 0;
cbp->crc16_en = 0;
cbp->crscdt = sc->flags & FXP_FLAG_SERIAL_MEDIA ? 1 : 0;
cbp->stripping = !prm;
cbp->padding = 1;
cbp->rcv_crc_xfer = 0;
cbp->long_rx_en = sc->flags & FXP_FLAG_LONG_PKT_EN ? 1 : 0;
cbp->ia_wake_en = 0;
cbp->magic_pkt_dis = 0;
cbp->force_fdx = 0;
cbp->fdx_pin_en = 1;
cbp->multi_ia = 0;
cbp->mc_all = sc->flags & FXP_FLAG_ALL_MCAST ? 1 : 0;
if (sc->revision == FXP_REV_82557) {
cbp->fc_delay_lsb = 0;
cbp->fc_delay_msb = 0x40;
cbp->pri_fc_thresh = 3;
cbp->tx_fc_dis = 0;
cbp->rx_fc_restop = 0;
cbp->rx_fc_restart = 0;
cbp->fc_filter = 0;
cbp->pri_fc_loc = 1;
} else {
cbp->fc_delay_lsb = 0x1f;
cbp->fc_delay_msb = 0x01;
cbp->pri_fc_thresh = 3;
cbp->tx_fc_dis = 0;
cbp->rx_fc_restop = 1;
cbp->rx_fc_restart = 1;
cbp->fc_filter = !prm;
cbp->pri_fc_loc = 1;
}
fxp_scb_wait(sc);
CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
fxp_dma_wait(&cbp->cb_status, sc);
cb_ias = (struct fxp_cb_ias *) sc->cbl_base;
cb_ias->cb_status = 0;
cb_ias->cb_command = FXP_CB_COMMAND_IAS | FXP_CB_COMMAND_EL;
cb_ias->link_addr = -1;
bcopy(sc->arpcom.ac_enaddr,
(void *)(uintptr_t)(volatile void *)cb_ias->macaddr,
sizeof(sc->arpcom.ac_enaddr));
fxp_scb_wait(sc);
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
fxp_dma_wait(&cb_ias->cb_status, sc);
txp = sc->cbl_base;
bzero(txp, sizeof(struct fxp_cb_tx) * FXP_NTXCB);
for (i = 0; i < FXP_NTXCB; i++) {
txp[i].cb_status = FXP_CB_STATUS_C | FXP_CB_STATUS_OK;
txp[i].cb_command = FXP_CB_COMMAND_NOP;
txp[i].link_addr =
vtophys(&txp[(i + 1) & FXP_TXCB_MASK].cb_status);
if (sc->flags & FXP_FLAG_EXT_TXCB)
txp[i].tbd_array_addr = vtophys(&txp[i].tbd[2]);
else
txp[i].tbd_array_addr = vtophys(&txp[i].tbd[0]);
txp[i].next = &txp[(i + 1) & FXP_TXCB_MASK];
}
txp->cb_command = FXP_CB_COMMAND_NOP | FXP_CB_COMMAND_S;
sc->cbl_first = sc->cbl_last = txp;
sc->tx_queued = 1;
fxp_scb_wait(sc);
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
fxp_scb_wait(sc);
CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL,
vtophys(sc->rfa_headm->m_ext.ext_buf) + RFA_ALIGNMENT_FUDGE);
fxp_scb_cmd(sc, FXP_SCB_COMMAND_RU_START);
if (sc->miibus != NULL)
mii_mediachg(device_get_softc(sc->miibus));
ifp->if_flags |= IFF_RUNNING;
ifq_clr_oactive(&ifp->if_snd);
#ifdef IFPOLL_ENABLE
if (ifp->if_flags & IFF_NPOLLING) {
CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, FXP_SCB_INTR_DISABLE);
sc->fxp_npoll.ifpc_stcount = 0;
} else
#endif
CSR_WRITE_1(sc, FXP_CSR_SCB_INTRCNTL, 0);
callout_reset(&sc->fxp_stat_timer, hz, fxp_tick, sc);
}
static int
fxp_serial_ifmedia_upd(struct ifnet *ifp)
{
ASSERT_SERIALIZED(ifp->if_serializer);
return (0);
}
static void
fxp_serial_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
{
ASSERT_SERIALIZED(ifp->if_serializer);
ifmr->ifm_active = IFM_ETHER|IFM_MANUAL;
}
static int
fxp_ifmedia_upd(struct ifnet *ifp)
{
struct fxp_softc *sc = ifp->if_softc;
struct mii_data *mii;
ASSERT_SERIALIZED(ifp->if_serializer);
mii = device_get_softc(sc->miibus);
mii_mediachg(mii);
return (0);
}
static void
fxp_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
{
struct fxp_softc *sc = ifp->if_softc;
struct mii_data *mii;
ASSERT_SERIALIZED(ifp->if_serializer);
mii = device_get_softc(sc->miibus);
mii_pollstat(mii);
ifmr->ifm_active = mii->mii_media_active;
ifmr->ifm_status = mii->mii_media_status;
if (ifmr->ifm_status & IFM_10_T && sc->flags & FXP_FLAG_CU_RESUME_BUG)
sc->cu_resume_bug = 1;
else
sc->cu_resume_bug = 0;
}
static int
fxp_add_rfabuf(struct fxp_softc *sc, struct mbuf *oldm)
{
u_int32_t v;
struct mbuf *m;
struct fxp_rfa *rfa, *p_rfa;
m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
if (m == NULL) {
if (oldm == NULL)
return 1;
m = oldm;
m->m_data = m->m_ext.ext_buf;
}
m->m_data += RFA_ALIGNMENT_FUDGE;
rfa = mtod(m, struct fxp_rfa *);
m->m_data += sizeof(struct fxp_rfa);
rfa->size = (u_int16_t)(MCLBYTES - sizeof(struct fxp_rfa) -
RFA_ALIGNMENT_FUDGE);
rfa->rfa_status = 0;
rfa->rfa_control = FXP_RFA_CONTROL_EL;
rfa->actual_size = 0;
v = -1;
fxp_lwcopy(&v, (volatile u_int32_t *) rfa->link_addr);
fxp_lwcopy(&v, (volatile u_int32_t *) rfa->rbd_addr);
if (sc->rfa_headm != NULL) {
p_rfa = (struct fxp_rfa *)(sc->rfa_tailm->m_ext.ext_buf +
RFA_ALIGNMENT_FUDGE);
sc->rfa_tailm->m_next = m;
v = vtophys(rfa);
fxp_lwcopy(&v, (volatile u_int32_t *) p_rfa->link_addr);
p_rfa->rfa_control = 0;
} else {
sc->rfa_headm = m;
}
sc->rfa_tailm = m;
return (m == oldm);
}
static int
fxp_miibus_readreg(device_t dev, int phy, int reg)
{
struct fxp_softc *sc = device_get_softc(dev);
int count = 10000;
int value;
CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
(FXP_MDI_READ << 26) | (reg << 16) | (phy << 21));
while (((value = CSR_READ_4(sc, FXP_CSR_MDICONTROL)) & 0x10000000) == 0
&& count--)
DELAY(10);
if (count <= 0)
device_printf(dev, "fxp_miibus_readreg: timed out\n");
return (value & 0xffff);
}
static void
fxp_miibus_writereg(device_t dev, int phy, int reg, int value)
{
struct fxp_softc *sc = device_get_softc(dev);
int count = 10000;
CSR_WRITE_4(sc, FXP_CSR_MDICONTROL,
(FXP_MDI_WRITE << 26) | (reg << 16) | (phy << 21) |
(value & 0xffff));
while ((CSR_READ_4(sc, FXP_CSR_MDICONTROL) & 0x10000000) == 0 &&
count--)
DELAY(10);
if (count <= 0)
device_printf(dev, "fxp_miibus_writereg: timed out\n");
}
static int
fxp_ioctl(struct ifnet *ifp, u_long command, caddr_t data, struct ucred *cr)
{
struct fxp_softc *sc = ifp->if_softc;
struct ifreq *ifr = (struct ifreq *)data;
struct mii_data *mii;
int error = 0;
ASSERT_SERIALIZED(ifp->if_serializer);
switch (command) {
case SIOCSIFFLAGS:
if (ifp->if_flags & IFF_ALLMULTI)
sc->flags |= FXP_FLAG_ALL_MCAST;
else
sc->flags &= ~FXP_FLAG_ALL_MCAST;
if (ifp->if_flags & IFF_UP) {
fxp_init(sc);
} else {
if (ifp->if_flags & IFF_RUNNING)
fxp_stop(sc);
}
break;
case SIOCADDMULTI:
case SIOCDELMULTI:
if (ifp->if_flags & IFF_ALLMULTI)
sc->flags |= FXP_FLAG_ALL_MCAST;
else
sc->flags &= ~FXP_FLAG_ALL_MCAST;
if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0)
fxp_mc_setup(sc);
if (sc->flags & FXP_FLAG_ALL_MCAST)
fxp_init(sc);
error = 0;
break;
case SIOCSIFMEDIA:
case SIOCGIFMEDIA:
if (sc->miibus != NULL) {
mii = device_get_softc(sc->miibus);
error = ifmedia_ioctl(ifp, ifr,
&mii->mii_media, command);
} else {
error = ifmedia_ioctl(ifp, ifr, &sc->sc_media, command);
}
break;
default:
error = ether_ioctl(ifp, command, data);
break;
}
return (error);
}
static int
fxp_mc_addrs(struct fxp_softc *sc)
{
struct fxp_cb_mcs *mcsp = sc->mcsp;
struct ifnet *ifp = &sc->arpcom.ac_if;
struct ifmultiaddr *ifma;
int nmcasts;
nmcasts = 0;
if ((sc->flags & FXP_FLAG_ALL_MCAST) == 0) {
TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
if (ifma->ifma_addr->sa_family != AF_LINK)
continue;
if (nmcasts >= MAXMCADDR) {
sc->flags |= FXP_FLAG_ALL_MCAST;
nmcasts = 0;
break;
}
bcopy(LLADDR((struct sockaddr_dl *)ifma->ifma_addr),
(void *)(uintptr_t)(volatile void *)
&sc->mcsp->mc_addr[nmcasts][0], 6);
nmcasts++;
}
}
mcsp->mc_cnt = nmcasts * 6;
return (nmcasts);
}
static void
fxp_mc_setup(struct fxp_softc *sc)
{
struct fxp_cb_mcs *mcsp = sc->mcsp;
struct ifnet *ifp = &sc->arpcom.ac_if;
int count;
if (sc->tx_queued) {
struct fxp_cb_tx *txp;
if (sc->need_mcsetup)
return;
sc->need_mcsetup = 1;
txp = sc->cbl_last->next;
txp->mb_head = NULL;
txp->cb_status = 0;
txp->cb_command = FXP_CB_COMMAND_NOP |
FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
sc->cbl_last->cb_command &= ~FXP_CB_COMMAND_S;
sc->cbl_last = txp;
sc->tx_queued++;
fxp_scb_wait(sc);
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_RESUME);
ifp->if_timer = 5;
return;
}
sc->need_mcsetup = 0;
mcsp->next = sc->cbl_base;
mcsp->mb_head = NULL;
mcsp->cb_status = 0;
mcsp->cb_command = FXP_CB_COMMAND_MCAS |
FXP_CB_COMMAND_S | FXP_CB_COMMAND_I;
mcsp->link_addr = vtophys(&sc->cbl_base->cb_status);
fxp_mc_addrs(sc);
sc->cbl_first = sc->cbl_last = (struct fxp_cb_tx *) mcsp;
sc->tx_queued = 1;
count = 100;
while ((CSR_READ_1(sc, FXP_CSR_SCB_RUSCUS) >> 6) ==
FXP_SCB_CUS_ACTIVE && --count)
DELAY(10);
if (count == 0) {
if_printf(&sc->arpcom.ac_if, "command queue timeout\n");
return;
}
fxp_scb_wait(sc);
CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&mcsp->cb_status));
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
ifp->if_timer = 2;
return;
}
static u_int32_t fxp_ucode_d101a[] = D101_A_RCVBUNDLE_UCODE;
static u_int32_t fxp_ucode_d101b0[] = D101_B0_RCVBUNDLE_UCODE;
static u_int32_t fxp_ucode_d101ma[] = D101M_B_RCVBUNDLE_UCODE;
static u_int32_t fxp_ucode_d101s[] = D101S_RCVBUNDLE_UCODE;
static u_int32_t fxp_ucode_d102[] = D102_B_RCVBUNDLE_UCODE;
static u_int32_t fxp_ucode_d102c[] = D102_C_RCVBUNDLE_UCODE;
#define UCODE(x) x, sizeof(x)
struct ucode {
u_int32_t revision;
u_int32_t *ucode;
int length;
u_short int_delay_offset;
u_short bundle_max_offset;
} ucode_table[] = {
{ FXP_REV_82558_A4, UCODE(fxp_ucode_d101a), D101_CPUSAVER_DWORD, 0 },
{ FXP_REV_82558_B0, UCODE(fxp_ucode_d101b0), D101_CPUSAVER_DWORD, 0 },
{ FXP_REV_82559_A0, UCODE(fxp_ucode_d101ma),
D101M_CPUSAVER_DWORD, D101M_CPUSAVER_BUNDLE_MAX_DWORD },
{ FXP_REV_82559S_A, UCODE(fxp_ucode_d101s),
D101S_CPUSAVER_DWORD, D101S_CPUSAVER_BUNDLE_MAX_DWORD },
{ FXP_REV_82550, UCODE(fxp_ucode_d102),
D102_B_CPUSAVER_DWORD, D102_B_CPUSAVER_BUNDLE_MAX_DWORD },
{ FXP_REV_82550_C, UCODE(fxp_ucode_d102c),
D102_C_CPUSAVER_DWORD, D102_C_CPUSAVER_BUNDLE_MAX_DWORD },
{ 0, NULL, 0, 0, 0 }
};
static void
fxp_load_ucode(struct fxp_softc *sc)
{
struct ucode *uc;
struct fxp_cb_ucode *cbp;
for (uc = ucode_table; uc->ucode != NULL; uc++)
if (sc->revision == uc->revision)
break;
if (uc->ucode == NULL)
return;
cbp = (struct fxp_cb_ucode *)sc->cbl_base;
cbp->cb_status = 0;
cbp->cb_command = FXP_CB_COMMAND_UCODE | FXP_CB_COMMAND_EL;
cbp->link_addr = -1;
memcpy(cbp->ucode, uc->ucode, uc->length);
if (uc->int_delay_offset)
*(u_short *)&cbp->ucode[uc->int_delay_offset] =
sc->tunable_int_delay + sc->tunable_int_delay / 2;
if (uc->bundle_max_offset)
*(u_short *)&cbp->ucode[uc->bundle_max_offset] =
sc->tunable_bundle_max;
fxp_scb_wait(sc);
CSR_WRITE_4(sc, FXP_CSR_SCB_GENERAL, vtophys(&cbp->cb_status));
fxp_scb_cmd(sc, FXP_SCB_COMMAND_CU_START);
fxp_dma_wait(&cbp->cb_status, sc);
if_printf(&sc->arpcom.ac_if,
"Microcode loaded, int_delay: %d usec bundle_max: %d\n",
sc->tunable_int_delay,
uc->bundle_max_offset == 0 ? 0 : sc->tunable_bundle_max);
sc->flags |= FXP_FLAG_UCODE;
}
static int
sysctl_hw_fxp_int_delay(SYSCTL_HANDLER_ARGS)
{
return (sysctl_int_range(oidp, arg1, arg2, req, 300, 3000));
}
static int
sysctl_hw_fxp_bundle_max(SYSCTL_HANDLER_ARGS)
{
return (sysctl_int_range(oidp, arg1, arg2, req, 1, 0xffff));
}